Class: Api::Response

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

Overview

Api::Response instances wrap Typhoeus responses as an abstraction layer, but also in order to provide lazy evaluation of headers and JSON decoding of the body.

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



68
69
70
71
72
# File 'lib/ocean/api.rb', line 68

def initialize(response)
  @response = response
  @headers = nil
  @body = nil
end

Instance Method Details

#bodyObject

Returns the HTTP response body parsed from JSON. This is done lazily and only once.



109
110
111
# File 'lib/ocean/api.rb', line 109

def body
  @body ||= @response.response_body.blank? ? nil : JSON.parse(@response.response_body)
end

#headersObject

Returns a hash of HTTP response headers.



98
99
100
101
102
103
104
# File 'lib/ocean/api.rb', line 98

def headers
  @headers ||= (@response.response_headers || "").split("\r\n").inject({}) do |acc, h|
                 k, v = h.split(": ")
                 acc[k] = v
                 acc
               end
end

#messageObject

The status message of the HTTP response.



91
92
93
# File 'lib/ocean/api.rb', line 91

def message
  @response.status_message
end

#modified?Boolean

Returns true if the HTTP response was a success and not a 304.

Returns:

  • (Boolean)


137
138
139
# File 'lib/ocean/api.rb', line 137

def modified?
  @response.modified?
end

#raw_bodyObject

Returns the original HTTP response body as a String.



116
117
118
# File 'lib/ocean/api.rb', line 116

def raw_body
  @response.response_body
end

#requestObject

The request which yielded this response.



77
78
79
# File 'lib/ocean/api.rb', line 77

def request
  @response.request
end

#statusObject

The status code of the HTTP response.



84
85
86
# File 'lib/ocean/api.rb', line 84

def status
  @response.response_code
end

#success?Boolean

Returns true if the HTTP request was a success and status == 2xx.

Returns:

  • (Boolean)


123
124
125
# File 'lib/ocean/api.rb', line 123

def success?
  @response.success?
end

#timed_out?Boolean

Returns true if the HTTP request timed out.

Returns:

  • (Boolean)


130
131
132
# File 'lib/ocean/api.rb', line 130

def timed_out?
  @response.timed_out?
end