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 = 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

#bodyString

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

#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.



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

#filesarray

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_errorsbool



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_msgsbool



138
139
140
# File 'lib/cdnconnect_api/response.rb', line 138

def has_msgs
  msgs and msgs.length > 0
end

#is_bad_gatewaybool



261
262
263
# File 'lib/cdnconnect_api/response.rb', line 261

def is_bad_gateway
  status == 502
end

#is_bad_requestbool



225
226
227
# File 'lib/cdnconnect_api/response.rb', line 225

def is_bad_request
  status == 400
end

#is_client_errorbool



219
220
221
# File 'lib/cdnconnect_api/response.rb', line 219

def is_client_error
  status >= 400 and status < 500
end

#is_errorbool



213
214
215
# File 'lib/cdnconnect_api/response.rb', line 213

def is_error
  status >= 400 or has_errors
end

#is_forbiddenbool



237
238
239
# File 'lib/cdnconnect_api/response.rb', line 237

def is_forbidden
  status == 403
end

#is_method_not_allowedbool



249
250
251
# File 'lib/cdnconnect_api/response.rb', line 249

def is_method_not_allowed
  status == 405
end

#is_not_foundbool



243
244
245
# File 'lib/cdnconnect_api/response.rb', line 243

def is_not_found
  status == 404
end

#is_server_errorbool



255
256
257
# File 'lib/cdnconnect_api/response.rb', line 255

def is_server_error
  status >= 500
end

#is_service_unavailablebool



267
268
269
# File 'lib/cdnconnect_api/response.rb', line 267

def is_service_unavailable
  status == 503
end

#is_successbool



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_unauthorizedbool



231
232
233
# File 'lib/cdnconnect_api/response.rb', line 231

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.



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

#msgsarray

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

#objecthash

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

#resultsHash

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

#statusint



201
202
203
# File 'lib/cdnconnect_api/response.rb', line 201

def status
  @status
end

#to_sObject

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