Class: BookingSync::API::Response
- Inherits:
-
Object
- Object
- BookingSync::API::Response
- Defined in:
- lib/bookingsync/api/response.rb
Constant Summary collapse
- SPECIAL_JSONAPI_FIELDS =
%w(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.
-
#rels ⇒ Array
readonly
Return link relations 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.
-
#links ⇒ Hash
Return an array of links templates from the response body, it's the contents of links hash {'rentals.photos':'https://www.bookingsync.com/api/v3/photos/{rentals.photos'}}.
-
#process_data(hash, links) ⇒ Array
Turn parsed contents from an API response into a Resource or collection of Resources.
-
#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.
13 14 15 16 17 18 19 |
# File 'lib/bookingsync/api/response.rb', line 13 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.
6 7 8 |
# File 'lib/bookingsync/api/response.rb', line 6 def body @body end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
6 7 8 |
# File 'lib/bookingsync/api/response.rb', line 6 def client @client end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
6 7 8 |
# File 'lib/bookingsync/api/response.rb', line 6 def data @data end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
6 7 8 |
# File 'lib/bookingsync/api/response.rb', line 6 def headers @headers end |
#rels ⇒ Array (readonly)
Return link relations from 'Link' response header
61 62 63 |
# File 'lib/bookingsync/api/response.rb', line 61 def rels @rels end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
6 7 8 |
# File 'lib/bookingsync/api/response.rb', line 6 def status @status end |
Instance Method Details
#links ⇒ Hash
Return an array of links templates from the response body, it's the contents of links hash {'rentals.photos':'https://www.bookingsync.com/api/v3/photos/{rentals.photos'}}
54 55 56 |
# File 'lib/bookingsync/api/response.rb', line 54 def links @links ||= decoded_body[:links] end |
#process_data(hash, links) ⇒ Array
Turn parsed contents from an API response into a Resource or collection of Resources.
26 27 28 29 30 |
# File 'lib/bookingsync/api/response.rb', line 26 def process_data(hash, links) Array(hash).map do |hash| Resource.new(client, hash, links, resources_key) end end |
#resources ⇒ Array<BookingSync::API::Resource>
Return an array of Resources from the response body
45 46 47 |
# File 'lib/bookingsync/api/response.rb', line 45 def resources @resources ||= process_data(decoded_body[resources_key], links) end |
#resources_key ⇒ Symbol
Return name of the key in the response body hash where fetched resources are, like bookings or rentals
36 37 38 39 40 |
# File 'lib/bookingsync/api/response.rb', line 36 def resources_key decoded_body.keys.delete_if { |k| SPECIAL_JSONAPI_FIELDS.include?(k) }.pop end |