Class: RemoteResource::Response

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

Instance Method Summary collapse

Constructor Details

#initialize(response, connection_options = {}) ⇒ Response

Returns a new instance of Response.



7
8
9
10
11
# File 'lib/remote_resource/response.rb', line 7

def initialize(response, connection_options = {})
  @original_response  = response
  @original_request   = response.request
  @connection_options = connection_options
end

Instance Method Details

#error_messages_response_bodyObject



45
46
47
48
49
50
51
52
53
# File 'lib/remote_resource/response.rb', line 45

def error_messages_response_body
  return empty_hash if response_body.blank?
  return empty_hash if parsed_response_body.blank?

  return parsed_response_body["errors"]          if parsed_response_body.try :has_key?, "errors"
  return unpacked_parsed_response_body["errors"] if unpacked_parsed_response_body.try :has_key?, "errors"

  empty_hash
end

#parsed_response_bodyObject



55
56
57
58
59
# File 'lib/remote_resource/response.rb', line 55

def parsed_response_body
  @parsed_response_body ||= JSON.parse response_body
rescue JSON::ParserError
  nil
end

#response_bodyObject



21
22
23
# File 'lib/remote_resource/response.rb', line 21

def response_body
  original_response.body
end

#response_codeObject



25
26
27
# File 'lib/remote_resource/response.rb', line 25

def response_code
  original_response.response_code
end

#sanitized_response_bodyObject



29
30
31
32
33
34
# File 'lib/remote_resource/response.rb', line 29

def sanitized_response_body
  return empty_hash if response_body.blank?
  return empty_hash if parsed_response_body.blank?

  unpacked_parsed_response_body
end

#sanitized_response_metaObject



36
37
38
39
40
41
42
43
# File 'lib/remote_resource/response.rb', line 36

def sanitized_response_meta
  return empty_hash if response_body.blank?
  return empty_hash if parsed_response_body.blank?

  return parsed_response_body['meta'] if parsed_response_body.try :has_key?, 'meta'

  empty_hash
end

#success?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/remote_resource/response.rb', line 13

def success?
  original_response.success?
end

#unprocessable_entity?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/remote_resource/response.rb', line 17

def unprocessable_entity?
  response_code == 422
end