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.
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#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
- #data ⇒ Object
-
#initialize(agent, res, options = {}) ⇒ 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.
-
#process_rels ⇒ Object
Finds link relations from ‘Link’ response header.
- #time ⇒ Object
- #timing ⇒ Object
Constructor Details
#initialize(agent, res, options = {}) ⇒ Response
Builds a Response after a completed request.
agent - The Sawyer::Agent that is managing the API connection. res - A Faraday::Response.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/sawyer/response.rb', line 14 def initialize(agent, res, = {}) @agent = agent @status = res.status @headers = res.headers @env = res.env @body = res.body @rels = process_rels @started = [:sawyer_started] @ended = [:sawyer_ended] 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 |
#body ⇒ Object (readonly)
Returns the value of attribute body.
3 4 5 |
# File 'lib/sawyer/response.rb', line 3 def body @body end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
3 4 5 |
# File 'lib/sawyer/response.rb', line 3 def env @env 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
#data ⇒ Object
25 26 27 28 29 30 |
# File 'lib/sawyer/response.rb', line 25 def data @data ||= begin return(body) unless (headers[:content_type] =~ /json|msgpack/) process_data(agent.decode_body(body)) end end |
#inspect ⇒ Object
68 69 70 |
# File 'lib/sawyer/response.rb', line 68 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.
38 39 40 41 42 43 44 45 |
# File 'lib/sawyer/response.rb', line 38 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 |
#process_rels ⇒ Object
Finds link relations from ‘Link’ response header
Returns an array of Relations
50 51 52 53 54 55 56 57 58 |
# File 'lib/sawyer/response.rb', line 50 def process_rels links = ( @headers["Link"] || "" ).split(', ').map do |link| href, name = link.match(/<(.*?)>; rel="(\w+)"/).captures [name.to_sym, Relation.from_link(@agent, name, :href => href)] end Hash[*links.flatten] end |
#time ⇒ Object
64 65 66 |
# File 'lib/sawyer/response.rb', line 64 def time @ended end |
#timing ⇒ Object
60 61 62 |
# File 'lib/sawyer/response.rb', line 60 def timing @timing ||= @ended - @started end |