class BookingSync::API::Response
Constants
- SPECIAL_JSONAPI_FIELDS
Attributes
Public Class Methods
Build a Response
after a completed request.
@param client [BookingSync::API::Client] The client that is
managing the API connection.
@param res [Faraday::Response] Faraday response object
# File lib/bookingsync/api/response.rb, line 11 def initialize(client, res) @client = client @status = res.status @headers = res.headers @env = res.env @body = res.body end
Public Instance Methods
Returns a Hash of meta information taken from the response body
@return [Hash] Meta hash
# File lib/bookingsync/api/response.rb, line 68 def meta @meta ||= decoded_body[:meta] end
Turn parsed contents from an API
response into a Resource
or collection of Resources.
@param hash [Hash] A Hash of resources parsed from JSON. @return [Array] An Array of Resources.
# File lib/bookingsync/api/response.rb, line 24 def process_data(hash) Array(hash).map do |hash| Resource.new(client, hash, resource_relations, resources_key) end end
Returns a Hash of relations built from given links templates. These relations are the same for each resource, so we calculate them once here and pass to every top level resource.
@return [Hash] Hash of relations to associated resources
# File lib/bookingsync/api/response.rb, line 52 def resource_relations @resource_relations ||= Relation.from_links(client, decoded_body[:links]) end
Return an array of Resources from the response body
@return [Array<BookingSync::API::Resource>]
# File lib/bookingsync/api/response.rb, line 43 def resources @resources ||= process_data(decoded_body[resources_key]) end
Return name of the key in the response body hash where fetched resources are, like bookings or rentals
@return [Symbol] Key name in the body hash
# File lib/bookingsync/api/response.rb, line 34 def resources_key decoded_body.keys.delete_if { |key| SPECIAL_JSONAPI_FIELDS.include?(key) }.pop end
Private Instance Methods
# File lib/bookingsync/api/response.rb, line 74 def decoded_body @decoded_body ||= @client.decode_body(body) || {} end
# File lib/bookingsync/api/response.rb, line 78 def process_rels links = (@headers["Link"] || "").split(", ").map do |link| href, name = link.match(/<(.*?)>; rel="(\w+)"/).captures [name.to_sym, Relation.from_link(@client, name, href: href)] end Hash[*links.flatten] end