bookingsync-api 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 657a1fe08c85da356e964602580b31d892e9a0c9
4
- data.tar.gz: 09bd00b34418d232944dcd0f790b19233cffb735
3
+ metadata.gz: 880e050165fd6662866b2727aa066a6df376a609
4
+ data.tar.gz: bb7c1426aab6968106fae175fca59d8a4faa95bb
5
5
  SHA512:
6
- metadata.gz: eec8bc8666f9b3c79f1a2771f671df6a0c3fbfbcf1795ca85400128751c3e2c33dd3272c03d017c9300a156f19b89eff9a68e37200eed507b39e531c45ee2d6e
7
- data.tar.gz: 71251e0ff150dfc367788e047a0038794634d14a0fe1065c61938fb4d3544df20a1cc1977a98c26534307c42a5b469fb119f68b88ed9f725c5d609aa5664d485
6
+ metadata.gz: afaf6373157977f8af580f7d360761b440d54e53a1a08426ea267971bc27ca378730f31a6eaacf2c2cdb5a98954f9a0ec24d7e95d4bc2548bc3c0917c09d7b4b
7
+ data.tar.gz: eddb23e23bb4c566592c60553287ed47329fbe054ab7c1e065f519a31dabc6a4d490dc19af973153465a354aef9e596f29831416c544fc7b425e61f0ccf9a0e1
@@ -170,7 +170,7 @@ module BookingSync::API
170
170
  response = call(:get, path, query: options)
171
171
  data = response.resources.dup
172
172
 
173
- if (block_given? or auto_paginate) && response.rels[:next]
173
+ if (block_given? or auto_paginate) && response.relations[:next]
174
174
  first_request = true
175
175
  loop do
176
176
  if block_given?
@@ -179,8 +179,8 @@ module BookingSync::API
179
179
  data.concat(response.resources) unless first_request
180
180
  first_request = false
181
181
  end
182
- break unless response.rels[:next]
183
- response = response.rels[:next].get
182
+ break unless response.relations[:next]
183
+ response = response.relations[:next].get
184
184
  end
185
185
  end
186
186
 
@@ -3,22 +3,21 @@ require "hashie"
3
3
  module BookingSync::API
4
4
  class Resource < Hash
5
5
  include Hashie::Extensions::MethodAccess
6
- attr_reader :_client, :_rels, :_links, :_resources_key
6
+ attr_reader :_client, :_rels, :_resources_key
7
7
 
8
- # Initialize a Resource with the given links and data.
8
+ # Initialize a Resource with the given relations and data.
9
9
  #
10
10
  # @param client [BookingSync::API::Client] The client that made the API request.
11
11
  # @param data [Hash] Hash of key/value properties.
12
- # @param links [Hash] Hash of link templates for this resource.
12
+ # @param rels [Hash] Hash of built relations for this resource.
13
13
  # @param resources_key [Symbol|String] Key in response body under which
14
- def initialize(client, data = {}, links = {}, resources_key = nil)
15
- @_links = links
14
+ def initialize(client, data = {}, rels = {}, resources_key = nil)
16
15
  @_client = client
17
16
  @_resources_key = resources_key
18
17
  data.each do |key, value|
19
18
  self[key.to_sym] = process_value(value)
20
19
  end
21
- @_rels = Relation.from_links(client, links)
20
+ @_rels = rels
22
21
  end
23
22
 
24
23
  # Process an individual value of this resource. Hashes get exploded
@@ -28,7 +27,7 @@ module BookingSync::API
28
27
  # @return [Object] An Object to set as the value of a Resource key.
29
28
  def process_value(value)
30
29
  case value
31
- when Hash then self.class.new(@_client, value, @_links)
30
+ when Hash then self.class.new(@_client, value)
32
31
  when Array then value.map { |v| process_value(v) }
33
32
  else value
34
33
  end
@@ -3,7 +3,7 @@ require "addressable/template"
3
3
  module BookingSync::API
4
4
  class Response
5
5
  SPECIAL_JSONAPI_FIELDS = %w(links linked meta)
6
- attr_reader :client, :status, :headers, :data, :rels, :body
6
+ attr_reader :client, :status, :headers, :data, :relations, :body
7
7
 
8
8
  # Build a Response after a completed request.
9
9
  #
@@ -23,9 +23,9 @@ module BookingSync::API
23
23
  #
24
24
  # @param hash [Hash] A Hash of resources parsed from JSON.
25
25
  # @return [Array] An Array of Resources.
26
- def process_data(hash, links)
26
+ def process_data(hash)
27
27
  Array(hash).map do |hash|
28
- Resource.new(client, hash, links, resources_key)
28
+ Resource.new(client, hash, resource_relations, resources_key)
29
29
  end
30
30
  end
31
31
 
@@ -43,23 +43,25 @@ module BookingSync::API
43
43
  #
44
44
  # @return [Array<BookingSync::API::Resource>]
45
45
  def resources
46
- @resources ||= process_data(decoded_body[resources_key], links)
46
+ @resources ||= process_data(decoded_body[resources_key])
47
47
  end
48
48
 
49
- # Return an array of links templates from the response body,
50
- # it's the contents of links hash
51
- # {'links': {'rentals.photos':'https://www.bookingsync.com/api/v3/photos/{rentals.photos}'}}
49
+ # Returns a Hash of relations built from given links templates.
50
+ # These relations are the same for each resource, so we calculate
51
+ # them once here and pass to every top level resource.
52
52
  #
53
- # @return [Hash] Hash of links to associated resources
54
- def links
55
- @links ||= decoded_body[:links]
53
+ # @return [Hash] Hash of relations to associated resources
54
+ def resource_relations
55
+ @resource_relations ||= Relation.from_links(client,
56
+ decoded_body[:links])
56
57
  end
57
58
 
58
- # Return link relations from 'Link' response header
59
+ # Return a Hash of relations to other pages built from 'Link'
60
+ # response header
59
61
  #
60
- # @return [Array] An array of Relations
61
- def rels
62
- @rels ||= process_rels
62
+ # @return [Hash] Hash of relations to first,last,next and prev pages
63
+ def relations
64
+ @relations ||= process_rels
63
65
  end
64
66
 
65
67
  private
@@ -1,5 +1,5 @@
1
1
  module BookingSync
2
2
  module API
3
- VERSION = "0.0.13"
3
+ VERSION = "0.0.14"
4
4
  end
5
5
  end
@@ -12,10 +12,10 @@ describe BookingSync::API::Resource do
12
12
  details: {count: 1},
13
13
  id: 10
14
14
  },
15
- {
15
+ BookingSync::API::Relation.from_links(client, {
16
16
  :"foo.photos" => "http://foo.com/photos/{foo.photos}",
17
17
  :"foo.category" => "http://foo.com/categories/{foo.category}"
18
- },
18
+ }),
19
19
  "foo"
20
20
  )
21
21
  }
@@ -8,13 +8,14 @@ describe BookingSync::API::Response do
8
8
  let(:links) {
9
9
  {:'rentals.photos' => 'https://www.bookingsync.com/api/v3/photos/{rentals.photos}'}
10
10
  }
11
- let(:rentals) { [
12
- {id: 1, name: 'rental 1', photos: [1, 43]},
13
- {id: 2, name: 'rental 2'}
14
- ] }
15
- let(:response) do
16
- stubs = Faraday::Adapter::Test::Stubs.new
17
- client = BookingSync::API::Client.new(test_access_token,
11
+ let(:resource_relations) { BookingSync::API::Relation.from_links(client,
12
+ links) }
13
+ let(:rentals) do
14
+ [{id: 1, name: 'rental 1', photos: [1, 43]},
15
+ {id: 2, name: 'rental 2'}]
16
+ end
17
+ let(:client) do
18
+ BookingSync::API::Client.new(test_access_token,
18
19
  base_url: "http://foo.com") do |conn|
19
20
  conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
20
21
  conn.adapter :test, @stubs do |stub|
@@ -24,6 +25,9 @@ describe BookingSync::API::Response do
24
25
  end
25
26
  end
26
27
  end
28
+ end
29
+ let(:response) do
30
+ stubs = Faraday::Adapter::Test::Stubs.new
27
31
  client.call(:get, '/rentals')
28
32
  end
29
33
 
@@ -39,9 +43,11 @@ describe BookingSync::API::Response do
39
43
  end
40
44
  end
41
45
 
42
- describe "#links" do
46
+ describe "#resource_relations" do
43
47
  it "returns links to associated resources" do
44
- expect(response.links).to eql(links)
48
+ href = response.resource_relations[:'rentals.photos'].href
49
+ expect(href).not_to be_nil
50
+ expect(href).to eq(resource_relations[:'rentals.photos'].href)
45
51
  end
46
52
  end
47
53
 
@@ -57,10 +63,10 @@ describe BookingSync::API::Response do
57
63
  end
58
64
  end
59
65
 
60
- describe "#rels" do
66
+ describe "#relations" do
61
67
  it "returns relations from Link header" do
62
- expect(response.rels[:next].href).to eql('/rentals?page=2')
63
- expect(response.rels[:last].href).to eql('/rentals?page=19')
68
+ expect(response.relations[:next].href).to eql('/rentals?page=2')
69
+ expect(response.relations[:last].href).to eql('/rentals?page=19')
64
70
  end
65
71
  end
66
72
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bookingsync-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Grosjean
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-31 00:00:00.000000000 Z
11
+ date: 2014-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday