class Sawyer::Response
Attributes
agent[R]
body[R]
env[R]
headers[R]
rels[R]
status[R]
Public Class Methods
new(agent, res, options = {})
click to toggle source
Builds a Response
after a completed request.
agent - The Sawyer::Agent
that is managing the API connection. res - A Faraday::Response.
# File lib/sawyer/response.rb, line 14 def initialize(agent, res, options = {}) @agent = agent @status = res.status @headers = res.headers @env = res.env @body = res.body @rels = process_rels @started = options[:sawyer_started] @ended = options[:sawyer_ended] end
Public Instance Methods
data()
click to toggle source
# 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()
click to toggle source
# File lib/sawyer/response.rb, line 68 def inspect %(#<#{self.class}: #{@status} @rels=#{@rels.inspect} @data=#{data.inspect}>) end
process_data(data)
click to toggle source
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.
# 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()
click to toggle source
Finds link relations from 'Link' response header
Returns an array of Relations
# 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()
click to toggle source
# File lib/sawyer/response.rb, line 64 def time @ended end
timing()
click to toggle source
# File lib/sawyer/response.rb, line 60 def timing @timing ||= @ended - @started end