Class: RemoteResource::Response

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Response.



4
5
6
7
8
9
# File 'lib/remote_resource/response.rb', line 4

def initialize(connection_response, connection_options = {})
  @connection_response = connection_response
  @connection_request  = connection_options[:connection_request]
  @request             = connection_options[:request]
  @connection_options  = connection_options
end

Instance Method Details

#attributesObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/remote_resource/response.rb', line 53

def attributes
  @attributes ||= begin
    if @connection_options[:json_spec] == :json_api
      data = parsed_body.fetch("data", {})
      if data.is_a?(Array)
        data.map { |row| row["attributes"].merge({ "id" => row["id"] }) }
      elsif data.key?("attributes")
        data["attributes"].merge({ "id" => data["id"] })
      else
        data
      end
    else
      root_element = @connection_options[:root_element].to_s

      if root_element.present?
        data = parsed_body.try(:key?, root_element) && parsed_body[root_element]
      else
        data = parsed_body
      end

      if data.is_a?(Array)
        data
      else
        data.presence || {}
      end
    end
  end
end

#bodyObject



41
42
43
# File 'lib/remote_resource/response.rb', line 41

def body
  @body ||= @connection_response.body
end

#errorsObject



82
83
84
# File 'lib/remote_resource/response.rb', line 82

def errors
  @errors ||= parsed_body.try(:key?, 'errors') && parsed_body['errors'].presence || attributes.try(:key?, 'errors') && attributes['errors'].presence || {}
end

#headersObject



37
38
39
# File 'lib/remote_resource/response.rb', line 37

def headers
  @headers ||= @connection_response.headers
end

#metaObject



86
87
88
# File 'lib/remote_resource/response.rb', line 86

def meta
  @meta ||= parsed_body.try(:key?, 'meta') && parsed_body['meta'].presence || {}
end

#parsed_bodyObject



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

def parsed_body
  @parsed_body ||= begin
    JSON.parse(body.to_s)
  rescue JSON::ParserError
    {}
  end
end

#requestObject



11
12
13
# File 'lib/remote_resource/response.rb', line 11

def request
  @request
end

#response_codeObject Also known as: code



27
28
29
# File 'lib/remote_resource/response.rb', line 27

def response_code
  @response_code ||= @connection_response.response_code
end

#return_codeObject



33
34
35
# File 'lib/remote_resource/response.rb', line 33

def return_code
  @return_code ||= @connection_response.return_code
end

#success?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/remote_resource/response.rb', line 15

def success?
  @connection_response.success?
end

#timed_out?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/remote_resource/response.rb', line 19

def timed_out?
  @connection_response.timed_out?
end

#unprocessable_entity?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/remote_resource/response.rb', line 23

def unprocessable_entity?
  response_code == 422
end