Class: CDNConnect::APIResponse

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(http_response = "base_merge") ⇒ APIResponse

Returns a new instance of APIResponse.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cdnconnect_api/response.rb', line 26

def initialize(http_response="base_merge")

  if http_response == nil or http_response == ""
    @http_response = nil
    @status = 503
    @data = { "results" => {}, "msgs" => [{"text" => "http_response was nil", "status" => "error"}] }
    @body = nil

  elsif http_response == "base_merge"
    @http_response = nil
    @status = 200
    @data = { "results" => {}, "msgs" => [] }
    @body = nil

  else
    @http_response = http_response
    @status = http_response.status
    @data = nil
    @body = nil
  end

  return self
end

Instance Method Details

#bodyString

Content body of the http response. This will be read by the data method so the body can be parsed into a hash.

Returns:

  • (String)


82
83
84
85
86
87
# File 'lib/cdnconnect_api/response.rb', line 82

def body
  if @http_response != nil 
    @body = @http_response.body
  end
  @body
end

#dataHash

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.

Returns:

  • (Hash)


96
97
98
99
100
101
102
103
104
# File 'lib/cdnconnect_api/response.rb', line 96

def data
  if @data == nil and body != nil
    begin
      @data = JSON.parse(body)
    rescue
    end
  end
  @data
end

#filesarray

A list of all the files that were uploaded. Each file in the array is a hash.

Returns:

  • (array)

    An array of hashes each representing a CDN Connect object.



55
56
57
# File 'lib/cdnconnect_api/response.rb', line 55

def files
  return get_result('files')
end

#get_result(key) ⇒ Object

A helper method to read a key within the results.



122
123
124
125
126
127
128
# File 'lib/cdnconnect_api/response.rb', line 122

def get_result(key)
  r = results
  if r != nil
    return r.fetch(key, nil)
  end
  nil
end

#has_errorsbool

Returns A boolean indicating if this response has messages or not.

Returns:

  • (bool)

    A boolean indicating if this response has messages or not.



155
156
157
158
159
160
161
162
163
164
# File 'lib/cdnconnect_api/response.rb', line 155

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_msgsbool

Returns A boolean indicating if this response has messages or not.

Returns:

  • (bool)

    A boolean indicating if this response has messages or not.



148
149
150
# File 'lib/cdnconnect_api/response.rb', line 148

def has_msgs
  msgs and msgs.length > 0
end

#is_bad_gatewaybool

Returns:

  • (bool)


271
272
273
# File 'lib/cdnconnect_api/response.rb', line 271

def is_bad_gateway
  status == 502
end

#is_bad_requestbool

Returns:

  • (bool)


235
236
237
# File 'lib/cdnconnect_api/response.rb', line 235

def is_bad_request
  status == 400
end

#is_client_errorbool

Returns:

  • (bool)


229
230
231
# File 'lib/cdnconnect_api/response.rb', line 229

def is_client_error
  status >= 400 and status < 500
end

#is_errorbool

Returns:

  • (bool)


223
224
225
# File 'lib/cdnconnect_api/response.rb', line 223

def is_error
  status >= 400 or has_errors
end

#is_forbiddenbool

Returns:

  • (bool)


247
248
249
# File 'lib/cdnconnect_api/response.rb', line 247

def is_forbidden
  status == 403
end

#is_method_not_allowedbool

Returns:

  • (bool)


259
260
261
# File 'lib/cdnconnect_api/response.rb', line 259

def is_method_not_allowed
  status == 405
end

#is_not_foundbool

Returns:

  • (bool)


253
254
255
# File 'lib/cdnconnect_api/response.rb', line 253

def is_not_found
  status == 404
end

#is_server_errorbool

Returns:

  • (bool)


265
266
267
# File 'lib/cdnconnect_api/response.rb', line 265

def is_server_error
  status >= 500
end

#is_service_unavailablebool

Returns:

  • (bool)


277
278
279
# File 'lib/cdnconnect_api/response.rb', line 277

def is_service_unavailable
  status == 503
end

#is_successbool

Returns:

  • (bool)


217
218
219
# File 'lib/cdnconnect_api/response.rb', line 217

def is_success
  @body != nil and status >= 200 and status < 300 and not has_errors
end

#is_unauthorizedbool

Returns:

  • (bool)


241
242
243
# File 'lib/cdnconnect_api/response.rb', line 241

def is_unauthorized
  status == 401
end

#merge(updating_response) ⇒ APIResponse

Used to merge two responses into one APIResponse. This is done automatically when uploading numerous files.

Returns:



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
197
198
199
200
201
202
203
204
205
206
# File 'lib/cdnconnect_api/response.rb', line 172

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

#msgsarray

An array of messages, and each message is a hash. Example message within the msgs array: “text” => “info about the message”, “status” => “error”

Returns:

  • (array)

    A response object with helper methods to read the response.



137
138
139
140
141
142
143
# File 'lib/cdnconnect_api/response.rb', line 137

def msgs
  d = data
  if d != nil
    return d.fetch('msgs', [])
  end
  return []
end

#objecthash

Can be either a file or folder object, or the first file in the files array.

Returns:

  • (hash)

    A hash representing a CDN Connect object.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cdnconnect_api/response.rb', line 64

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

#resultsHash

Helper method to read the results hash.

Returns:

  • (Hash)


111
112
113
114
115
116
117
# File 'lib/cdnconnect_api/response.rb', line 111

def results
  d = data
  if d != nil and d.has_key?('results')
    return d['results']
  end
  nil
end

#statusint

Returns The HTTP response status.

Returns:

  • (int)

    The HTTP response status.



211
212
213
# File 'lib/cdnconnect_api/response.rb', line 211

def status
  @status
end

#to_sObject

called with print / puts



281
282
283
# File 'lib/cdnconnect_api/response.rb', line 281

def to_s # called with print / puts
  "Status: #{status}\nBody: #{body}"
end