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 +4 -4
- data/lib/bookingsync/api/client.rb +3 -3
- data/lib/bookingsync/api/resource.rb +6 -7
- data/lib/bookingsync/api/response.rb +16 -14
- data/lib/bookingsync/api/version.rb +1 -1
- data/spec/bookingsync/api/resource_spec.rb +2 -2
- data/spec/bookingsync/api/response_spec.rb +18 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 880e050165fd6662866b2727aa066a6df376a609
|
4
|
+
data.tar.gz: bb7c1426aab6968106fae175fca59d8a4faa95bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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.
|
183
|
-
response = response.
|
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, :
|
6
|
+
attr_reader :_client, :_rels, :_resources_key
|
7
7
|
|
8
|
-
# Initialize a Resource with the given
|
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
|
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 = {},
|
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 =
|
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
|
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, :
|
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
|
26
|
+
def process_data(hash)
|
27
27
|
Array(hash).map do |hash|
|
28
|
-
Resource.new(client, hash,
|
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]
|
46
|
+
@resources ||= process_data(decoded_body[resources_key])
|
47
47
|
end
|
48
48
|
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
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
|
54
|
-
def
|
55
|
-
@
|
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
|
59
|
+
# Return a Hash of relations to other pages built from 'Link'
|
60
|
+
# response header
|
59
61
|
#
|
60
|
-
# @return [
|
61
|
-
def
|
62
|
-
@
|
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
|
@@ -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(:
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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 "#
|
46
|
+
describe "#resource_relations" do
|
43
47
|
it "returns links to associated resources" do
|
44
|
-
|
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 "#
|
66
|
+
describe "#relations" do
|
61
67
|
it "returns relations from Link header" do
|
62
|
-
expect(response.
|
63
|
-
expect(response.
|
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.
|
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-
|
11
|
+
date: 2014-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|