Class: CDNConnect::APIResponse
- Inherits:
-
Object
- Object
- CDNConnect::APIResponse
- Defined in:
- lib/cdnconnect_api/response.rb
Overview
Used to simpilfy things with helper functions to read response data. Responses from the API are all structured the same way, and this class is used as a wrapper to make it easier to read resposne data.
Instance Method Summary collapse
-
#body ⇒ String
Content body of the http response.
-
#data ⇒ Hash
Decode the response body from JSON into a hash.
-
#files ⇒ array
A list of all the files that were uploaded.
-
#get_result(key) ⇒ Object
A helper method to read a key within the results.
-
#has_errors ⇒ bool
A boolean indicating if this response has messages or not.
-
#has_msgs ⇒ bool
A boolean indicating if this response has messages or not.
-
#initialize(http_response = nil) ⇒ APIResponse
constructor
A new instance of APIResponse.
- #is_bad_gateway ⇒ bool
- #is_bad_request ⇒ bool
- #is_client_error ⇒ bool
- #is_error ⇒ bool
- #is_forbidden ⇒ bool
- #is_method_not_allowed ⇒ bool
- #is_not_found ⇒ bool
- #is_server_error ⇒ bool
- #is_service_unavailable ⇒ bool
- #is_success ⇒ bool
- #is_unauthorized ⇒ bool
-
#merge(updating_response) ⇒ APIResponse
Used to merge two responses into one APIResponse.
-
#msgs ⇒ array
An array of messages, and each message is a hash.
-
#object ⇒ hash
Can be either a file or folder object, or the first file in the files array.
-
#results ⇒ Hash
Helper method to read the results hash.
-
#status ⇒ int
The HTTP response status.
-
#to_s ⇒ Object
called with print / puts.
Constructor Details
#initialize(http_response = nil) ⇒ APIResponse
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/cdnconnect_api/response.rb', line 26 def initialize(http_response=nil) @http_response = http_response if http_response != nil @status = http_response.status @data = nil @body = nil else @status = 0 @data = {"results" => {}, "msgs" => []} @body = nil end return self end |
Instance Method Details
#body ⇒ String
Content body of the http response. This will be read by the data method so the body can be parsed into a hash.
72 73 74 75 76 77 |
# File 'lib/cdnconnect_api/response.rb', line 72 def body if @http_response != nil @body = @http_response.body end @body end |
#data ⇒ Hash
Decode the response body from JSON into a hash. Store the parsed data in an instance variable (@data) so we don’t keep parsing it every time we reference data.
86 87 88 89 90 91 92 93 94 |
# File 'lib/cdnconnect_api/response.rb', line 86 def data if @data == nil and body != nil begin @data = JSON.parse(body) rescue end end @data end |
#files ⇒ array
A list of all the files that were uploaded. Each file in the array is a hash.
45 46 47 |
# File 'lib/cdnconnect_api/response.rb', line 45 def files return get_result('files') end |
#get_result(key) ⇒ Object
A helper method to read a key within the results.
112 113 114 115 116 117 118 |
# File 'lib/cdnconnect_api/response.rb', line 112 def get_result(key) r = results if r != nil return r.fetch(key, nil) end nil end |
#has_errors ⇒ bool
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/cdnconnect_api/response.rb', line 145 def has_errors if msgs and msgs.length > 0 for msg in msgs if msg["status"] == "error" return true end end end false end |
#has_msgs ⇒ bool
138 139 140 |
# File 'lib/cdnconnect_api/response.rb', line 138 def has_msgs msgs and msgs.length > 0 end |
#is_bad_gateway ⇒ bool
261 262 263 |
# File 'lib/cdnconnect_api/response.rb', line 261 def is_bad_gateway status == 502 end |
#is_bad_request ⇒ bool
225 226 227 |
# File 'lib/cdnconnect_api/response.rb', line 225 def is_bad_request status == 400 end |
#is_client_error ⇒ bool
219 220 221 |
# File 'lib/cdnconnect_api/response.rb', line 219 def is_client_error status >= 400 and status < 500 end |
#is_error ⇒ bool
213 214 215 |
# File 'lib/cdnconnect_api/response.rb', line 213 def is_error status >= 400 or has_errors end |
#is_forbidden ⇒ bool
237 238 239 |
# File 'lib/cdnconnect_api/response.rb', line 237 def is_forbidden status == 403 end |
#is_method_not_allowed ⇒ bool
249 250 251 |
# File 'lib/cdnconnect_api/response.rb', line 249 def is_method_not_allowed status == 405 end |
#is_not_found ⇒ bool
243 244 245 |
# File 'lib/cdnconnect_api/response.rb', line 243 def is_not_found status == 404 end |
#is_server_error ⇒ bool
255 256 257 |
# File 'lib/cdnconnect_api/response.rb', line 255 def is_server_error status >= 500 end |
#is_service_unavailable ⇒ bool
267 268 269 |
# File 'lib/cdnconnect_api/response.rb', line 267 def is_service_unavailable status == 503 end |
#is_success ⇒ bool
207 208 209 |
# File 'lib/cdnconnect_api/response.rb', line 207 def is_success @body != nil and status >= 200 and status < 300 and not has_errors end |
#is_unauthorized ⇒ bool
231 232 233 |
# File 'lib/cdnconnect_api/response.rb', line 231 def status == 401 end |
#merge(updating_response) ⇒ APIResponse
Used to merge two responses into one APIResponse. This is done automatically when uploading numerous files.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/cdnconnect_api/response.rb', line 162 def merge(updating_response) if updating_response == nil or updating_response.data == nil return self end if updating_response.status > status @status = updating_response.status end if data == nil @data = updating_response.data return self end if updating_response.body != nil if @body == nil @body = updating_response.body else @body += updating_response.body end end @data['msgs'] += updating_response.msgs if updating_response.files != nil f = files if f != nil @data['results']['files'] += updating_response.files else @data['results']['files'] = updating_response.files end end return self end |
#msgs ⇒ array
An array of messages, and each message is a hash. Example message within the msgs array: “text” => “info about the message”, “status” => “error”
127 128 129 130 131 132 133 |
# File 'lib/cdnconnect_api/response.rb', line 127 def msgs d = data if d != nil return d.fetch('msgs', []) end return [] end |
#object ⇒ hash
Can be either a file or folder object, or the first file in the files array.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cdnconnect_api/response.rb', line 54 def object obj = get_result('object') if obj != nil return obj end fs = files if fs != nil and fs.length > 0 return fs[0] end nil end |
#results ⇒ Hash
Helper method to read the results hash.
101 102 103 104 105 106 107 |
# File 'lib/cdnconnect_api/response.rb', line 101 def results d = data if d != nil and d.has_key?('results') return d['results'] end nil end |
#status ⇒ int
201 202 203 |
# File 'lib/cdnconnect_api/response.rb', line 201 def status @status end |
#to_s ⇒ Object
called with print / puts
271 272 273 |
# File 'lib/cdnconnect_api/response.rb', line 271 def to_s # called with print / puts "Status: #{status}\nBody: #{body}" end |