Class: BookingSync::API::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/bookingsync/api/response.rb

Constant Summary collapse

SPECIAL_JSONAPI_FIELDS =
[:links, :linked, :meta]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, res) ⇒ Response

Build a Response after a completed request.

Parameters:

  • client (BookingSync::API::Client)

    The client that is managing the API connection.

  • res (Faraday::Response)

    Faraday response object



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

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/bookingsync/api/response.rb', line 6

def body
  @body
end

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/bookingsync/api/response.rb', line 6

def client
  @client
end

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/bookingsync/api/response.rb', line 6

def data
  @data
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/bookingsync/api/response.rb', line 6

def headers
  @headers
end

#relationsHash (readonly)

Return a Hash of relations to other pages built from ‘Link’ response header

Returns:

  • (Hash)

    Hash of relations to first, last, next and prev pages



63
64
65
# File 'lib/bookingsync/api/response.rb', line 63

def relations
  @relations
end

#statusObject (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

#metaHash

Returns a Hash of meta information taken from the response body

Returns:

  • (Hash)

    Meta hash



70
71
72
# File 'lib/bookingsync/api/response.rb', line 70

def meta
  @meta ||= decoded_body[:meta]
end

#process_data(hash) ⇒ Array

Turn parsed contents from an API response into a Resource or collection of Resources.

Parameters:

  • hash (Hash)

    A Hash of resources parsed from JSON.

Returns:

  • (Array)

    An Array of Resources.



26
27
28
29
30
# File 'lib/bookingsync/api/response.rb', line 26

def process_data(hash)
  Array(hash).map do |hash|
    Resource.new(client, hash, resource_relations, resources_key)
  end
end

#resource_relationsHash

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.

Returns:

  • (Hash)

    Hash of relations to associated resources



54
55
56
57
# File 'lib/bookingsync/api/response.rb', line 54

def resource_relations
  @resource_relations ||= Relation.from_links(client,
    decoded_body[:links])
end

#resourcesArray<BookingSync::API::Resource>

Return an array of Resources from the response body

Returns:



45
46
47
# File 'lib/bookingsync/api/response.rb', line 45

def resources
  @resources ||= process_data(decoded_body[resources_key])
end

#resources_keySymbol

Return name of the key in the response body hash where fetched resources are, like bookings or rentals

Returns:

  • (Symbol)

    Key name in the body hash



36
37
38
39
40
# File 'lib/bookingsync/api/response.rb', line 36

def resources_key
  decoded_body.keys.delete_if { |key|
    SPECIAL_JSONAPI_FIELDS.include?(key)
  }.pop
end