Class: Sawyer::Response
- Inherits:
-
Object
- Object
- Sawyer::Response
- Defined in:
- lib/sawyer/response.rb
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#rels ⇒ Object
readonly
Returns the value of attribute rels.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#initialize(agent, res) ⇒ Response
constructor
Builds a Response after a completed request.
- #inspect ⇒ Object
-
#process_data(data) ⇒ Object
Turns parsed contents from an API response into a Resource or collection of Resources.
- #time ⇒ Object
- #timing ⇒ Object
Constructor Details
#initialize(agent, res) ⇒ Response
Builds a Response after a completed request.
agent - The Sawyer::Agent that is managing the API connection. res - A Faraday::Response.
13 14 15 16 17 18 19 |
# File 'lib/sawyer/response.rb', line 13 def initialize(agent, res) @agent = agent @status = res.status @headers = res.headers @env = res.env @data = process_data(@agent.decode_body(res.body)) end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
3 4 5 |
# File 'lib/sawyer/response.rb', line 3 def agent @agent end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
3 4 5 |
# File 'lib/sawyer/response.rb', line 3 def data @data end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
3 4 5 |
# File 'lib/sawyer/response.rb', line 3 def headers @headers end |
#rels ⇒ Object (readonly)
Returns the value of attribute rels.
3 4 5 |
# File 'lib/sawyer/response.rb', line 3 def rels @rels end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
3 4 5 |
# File 'lib/sawyer/response.rb', line 3 def status @status end |
Instance Method Details
#inspect ⇒ Object
44 45 46 |
# File 'lib/sawyer/response.rb', line 44 def inspect %(#<#{self.class}: #{@status} @rels=#{@rels.inspect} @data=#{@data.inspect}>) end |
#process_data(data) ⇒ Object
Turns parsed contents from an API response into a Resource or collection of Resources.
data - Either an Array or Hash parsed from JSON.
Returns either a Resource or Array of Resources.
27 28 29 30 31 32 33 34 |
# File 'lib/sawyer/response.rb', line 27 def process_data(data) case data when Hash then Resource.new(agent, data) when Array then data.map { |hash| process_data(hash) } when nil then nil else data end end |
#time ⇒ Object
40 41 42 |
# File 'lib/sawyer/response.rb', line 40 def time @env[:sawyer_ended] end |
#timing ⇒ Object
36 37 38 |
# File 'lib/sawyer/response.rb', line 36 def timing @timing ||= @env[:sawyer_ended] - @env[:sawyer_started] end |