Class: Urtak::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/urtak.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/urtak.rb', line 264

def initialize(response)
  @raw    = response
  @format = nil

  # TODO case statement
  if response.headers[:content_type].nil? || response.body.empty?
    @body = response
  elsif response.headers[:content_type].match(/json/)
    @body = JSON.parse(response)
    @format = "JSON"
  elsif response.headers[:content_type].match(/xml/)
    raise Urtak::Errors::Unimplemented
    @format = "XML"
  else
    @body = response
  end
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



262
263
264
# File 'lib/urtak.rb', line 262

def body
  @body
end

#formatObject

Returns the value of attribute format.



262
263
264
# File 'lib/urtak.rb', line 262

def format
  @format
end

#rawObject

Returns the value of attribute raw.



262
263
264
# File 'lib/urtak.rb', line 262

def raw
  @raw
end

Instance Method Details

#codeObject



282
283
284
# File 'lib/urtak.rb', line 282

def code
  raw.code
end

#errorObject



286
287
288
# File 'lib/urtak.rb', line 286

def error
  response.body['error'] ? response.body['error']['message'] : nil
end

#failure?Boolean

Returns:

  • (Boolean)


298
299
300
# File 'lib/urtak.rb', line 298

def failure?
  code >= 400
end

#found?Boolean

naive

Returns:

  • (Boolean)


307
308
309
# File 'lib/urtak.rb', line 307

def found?
  code == 200 || code == 304
end

#headersObject



290
291
292
# File 'lib/urtak.rb', line 290

def headers
  raw.headers
end

#not_found?Boolean

Returns:

  • (Boolean)


302
303
304
# File 'lib/urtak.rb', line 302

def not_found?
  code == 404
end

#success?Boolean

Returns:

  • (Boolean)


294
295
296
# File 'lib/urtak.rb', line 294

def success?
  code >= 200 && code < 400
end