bookingsync-api 0.0.12 → 0.0.13
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/README.md +11 -0
- data/lib/bookingsync/api/client.rb +61 -42
- data/lib/bookingsync/api/version.rb +1 -1
- 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: 657a1fe08c85da356e964602580b31d892e9a0c9
|
4
|
+
data.tar.gz: 09bd00b34418d232944dcd0f790b19233cffb735
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eec8bc8666f9b3c79f1a2771f671df6a0c3fbfbcf1795ca85400128751c3e2c33dd3272c03d017c9300a156f19b89eff9a68e37200eed507b39e531c45ee2d6e
|
7
|
+
data.tar.gz: 71251e0ff150dfc367788e047a0038794634d14a0fe1065c61938fb4d3544df20a1cc1977a98c26534307c42a5b469fb119f68b88ed9f725c5d609aa5664d485
|
data/README.md
CHANGED
@@ -58,6 +58,17 @@ There are two ways to enable logging:
|
|
58
58
|
|
59
59
|
api = BookingSync::API.new("OAUTH_TOKEN", logger: Rails.logger)
|
60
60
|
|
61
|
+
### Instrumentation
|
62
|
+
|
63
|
+
BookingSync::API exposes instrumentation information that can be consumed
|
64
|
+
by various instrumentation libraries. By default it doesn't send the
|
65
|
+
information anywhere.
|
66
|
+
|
67
|
+
To hook instrumentations into `ActiveSupport::Notifications`, pass the
|
68
|
+
module into the API client initializer:
|
69
|
+
|
70
|
+
api = BookingSync::API.new("OAUTH_TOKEN", instrumenter: ActiveSupport::Notifications)
|
71
|
+
|
61
72
|
#### Log levels
|
62
73
|
|
63
74
|
`INFO` - Logged are only request method and the URL.
|
@@ -29,6 +29,7 @@ require "logger"
|
|
29
29
|
|
30
30
|
module BookingSync::API
|
31
31
|
class Client
|
32
|
+
extend Forwardable
|
32
33
|
include BookingSync::API::Client::Amenities
|
33
34
|
include BookingSync::API::Client::Availabilities
|
34
35
|
include BookingSync::API::Client::BillingAddresses
|
@@ -54,6 +55,8 @@ module BookingSync::API
|
|
54
55
|
|
55
56
|
attr_reader :token, :logger
|
56
57
|
|
58
|
+
def_delegator :@instrumenter, :instrument
|
59
|
+
|
57
60
|
# Initialize new Client
|
58
61
|
#
|
59
62
|
# @param token [String] OAuth token
|
@@ -61,10 +64,13 @@ module BookingSync::API
|
|
61
64
|
# @option options [String] base_url: Base URL to BookingSync site
|
62
65
|
# @option options [Logger] logger: Logger where headers and body of every
|
63
66
|
# request and response will be logged.
|
67
|
+
# @option options [Module] instrumenter: A module that responds to
|
68
|
+
# instrument, usually ActiveSupport::Notifications.
|
64
69
|
# @return [BookingSync::API::Client] New BookingSync API client
|
65
70
|
def initialize(token, options = {})
|
66
71
|
@token = token
|
67
72
|
@logger = options[:logger] || default_logger
|
73
|
+
@instrumenter = options[:instrumenter] || NoopInstrumenter
|
68
74
|
@base_url = options[:base_url]
|
69
75
|
@serializer = Serializer.new
|
70
76
|
@conn = Faraday.new(faraday_options)
|
@@ -142,8 +148,10 @@ module BookingSync::API
|
|
142
148
|
# @param options [Hash] A customizable set of request options.
|
143
149
|
# @return [Array<BookingSync::API::Resource>] Array of resources.
|
144
150
|
def request(method, path, data = nil, options = nil)
|
145
|
-
|
146
|
-
|
151
|
+
instrument("request.bookingsync_api", method: method, path: path) do
|
152
|
+
response = call(method, path, data, options)
|
153
|
+
response.respond_to?(:resources) ? response.resources : response
|
154
|
+
end
|
147
155
|
end
|
148
156
|
|
149
157
|
# Make a HTTP GET request to a path with pagination support.
|
@@ -157,25 +165,27 @@ module BookingSync::API
|
|
157
165
|
# @yieldreturn [Array<BookingSync::API::Resource>] Batch of resources
|
158
166
|
# @return [Array<BookingSync::API::Resource>] Batch of resources
|
159
167
|
def paginate(path, options = {}, &block)
|
160
|
-
|
161
|
-
|
162
|
-
|
168
|
+
instrument("paginate.bookingsync_api", path: path) do
|
169
|
+
auto_paginate = options.delete(:auto_paginate)
|
170
|
+
response = call(:get, path, query: options)
|
171
|
+
data = response.resources.dup
|
163
172
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
173
|
+
if (block_given? or auto_paginate) && response.rels[:next]
|
174
|
+
first_request = true
|
175
|
+
loop do
|
176
|
+
if block_given?
|
177
|
+
yield(response.resources)
|
178
|
+
elsif auto_paginate
|
179
|
+
data.concat(response.resources) unless first_request
|
180
|
+
first_request = false
|
181
|
+
end
|
182
|
+
break unless response.rels[:next]
|
183
|
+
response = response.rels[:next].get
|
172
184
|
end
|
173
|
-
break unless response.rels[:next]
|
174
|
-
response = response.rels[:next].get
|
175
185
|
end
|
176
|
-
end
|
177
186
|
|
178
|
-
|
187
|
+
data
|
188
|
+
end
|
179
189
|
end
|
180
190
|
|
181
191
|
# Make a HTTP request to given path and returns Response object.
|
@@ -187,37 +197,39 @@ module BookingSync::API
|
|
187
197
|
# @param options [Hash] A customizable set of request options.
|
188
198
|
# @return [BookingSync::API::Response] A Response object.
|
189
199
|
def call(method, path, data = nil, options = nil)
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
options ||= {}
|
195
|
-
|
196
|
-
if options.has_key?(:query)
|
197
|
-
if options[:query].has_key?(:ids)
|
198
|
-
ids = Array(options[:query].delete(:ids)).join(',')
|
199
|
-
path = "#{path}/#{ids}"
|
200
|
+
instrument("call.bookingsync_api", method: method, path: path) do
|
201
|
+
if [:get, :head].include?(method)
|
202
|
+
options = data
|
203
|
+
data = nil
|
200
204
|
end
|
201
|
-
options
|
202
|
-
|
203
|
-
|
205
|
+
options ||= {}
|
206
|
+
|
207
|
+
if options.has_key?(:query)
|
208
|
+
if options[:query].has_key?(:ids)
|
209
|
+
ids = Array(options[:query].delete(:ids)).join(',')
|
210
|
+
path = "#{path}/#{ids}"
|
211
|
+
end
|
212
|
+
options[:query].keys.each do |key|
|
213
|
+
if options[:query][key].is_a?(Array)
|
214
|
+
options[:query][key] = options[:query][key].join(",")
|
215
|
+
end
|
204
216
|
end
|
205
217
|
end
|
206
|
-
end
|
207
218
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
219
|
+
url = expand_url(path, options[:uri])
|
220
|
+
res = @conn.send(method, url) do |req|
|
221
|
+
if data
|
222
|
+
req.body = data.is_a?(String) ? data : encode_body(data)
|
223
|
+
end
|
224
|
+
if params = options[:query]
|
225
|
+
req.params.update params
|
226
|
+
end
|
227
|
+
if headers = options[:headers]
|
228
|
+
req.headers.update headers
|
229
|
+
end
|
218
230
|
end
|
231
|
+
handle_response(res)
|
219
232
|
end
|
220
|
-
handle_response(res)
|
221
233
|
end
|
222
234
|
|
223
235
|
private
|
@@ -294,5 +306,12 @@ module BookingSync::API
|
|
294
306
|
def default_logger
|
295
307
|
Logger.new(debug? ? STDOUT : nil)
|
296
308
|
end
|
309
|
+
|
310
|
+
# Default instrumenter which does nothing.
|
311
|
+
module NoopInstrumenter
|
312
|
+
def self.instrument(name, payload = {})
|
313
|
+
yield payload if block_given?
|
314
|
+
end
|
315
|
+
end
|
297
316
|
end
|
298
317
|
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.13
|
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-
|
11
|
+
date: 2014-05-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|