Class: BookingSync::API::Response
- Inherits:
-
Object
- Object
- BookingSync::API::Response
- Defined in:
- lib/bookingsync/api/response.rb
Constant Summary collapse
- SPECIAL_JSONAPI_FIELDS =
[:links, :linked, :meta]
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#relations ⇒ Hash
readonly
Return a Hash of relations to other pages built from ‘Link’ response header.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#initialize(client, res) ⇒ Response
constructor
Build a Response after a completed request.
-
#meta ⇒ Hash
Returns a Hash of meta information taken from the response body.
-
#process_data(hash) ⇒ Array
Turn parsed contents from an API response into a Resource or collection of Resources.
-
#resource_relations ⇒ Hash
Returns a Hash of relations built from given links templates.
-
#resources ⇒ Array<BookingSync::API::Resource>
Return an array of Resources from the response body.
-
#resources_key ⇒ Symbol
Return name of the key in the response body hash where fetched resources are, like bookings or rentals.
Constructor Details
#initialize(client, res) ⇒ Response
Build a Response after a completed request.
11 12 13 14 15 16 17 |
# 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 |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
4 5 6 |
# File 'lib/bookingsync/api/response.rb', line 4 def body @body end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
4 5 6 |
# File 'lib/bookingsync/api/response.rb', line 4 def client @client end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
4 5 6 |
# File 'lib/bookingsync/api/response.rb', line 4 def data @data end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
4 5 6 |
# File 'lib/bookingsync/api/response.rb', line 4 def headers @headers end |
#relations ⇒ Hash (readonly)
Return a Hash of relations to other pages built from ‘Link’ response header
61 62 63 |
# File 'lib/bookingsync/api/response.rb', line 61 def relations @relations end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
4 5 6 |
# File 'lib/bookingsync/api/response.rb', line 4 def status @status end |
Instance Method Details
#meta ⇒ Hash
Returns a Hash of meta information taken from the response body
68 69 70 |
# File 'lib/bookingsync/api/response.rb', line 68 def @meta ||= decoded_body[:meta] end |
#process_data(hash) ⇒ Array
Turn parsed contents from an API response into a Resource or collection of Resources.
24 25 26 27 28 |
# 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 |
#resource_relations ⇒ Hash
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.
52 53 54 55 |
# File 'lib/bookingsync/api/response.rb', line 52 def resource_relations @resource_relations ||= Relation.from_links(client, decoded_body[:links]) end |
#resources ⇒ Array<BookingSync::API::Resource>
Return an array of Resources from the response body
43 44 45 |
# File 'lib/bookingsync/api/response.rb', line 43 def resources @resources ||= process_data(decoded_body[resources_key]) end |
#resources_key ⇒ Symbol
Return name of the key in the response body hash where fetched resources are, like bookings or rentals
34 35 36 37 38 |
# File 'lib/bookingsync/api/response.rb', line 34 def resources_key decoded_body.keys.delete_if { |key| SPECIAL_JSONAPI_FIELDS.include?(key) }.pop end |