namely 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -1
- data/lib/namely.rb +1 -0
- data/lib/namely/configuration.rb +56 -0
- data/lib/namely/resource_gateway.rb +10 -1
- data/lib/namely/version.rb +1 -1
- data/namely.gemspec +1 -1
- data/spec/namely/configuration_spec.rb +41 -0
- data/spec/namely/resource_gateway_spec.rb +32 -0
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e43ba1385890f1253066c41a14eae2213145d660
|
4
|
+
data.tar.gz: f334b88109df6428c2e900755e56b6f3f9dc0435
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c73def2dc1ee53081c84a174c19a21c2085cb921bcdb20a8ce0eeaf69d336448da68f8a935bb8f77e9ba0d0e4c6d5f42f4b12b8a813d7a89a9d3c2e797ed3fde
|
7
|
+
data.tar.gz: 5e98f5d8e6cdb893c84d7a0100886915d398158e48bb392729f8d4de0b456392921dc140168aae242703c962f9642146585d58de929f736de337c0ab9c0a7142
|
data/README.md
CHANGED
@@ -41,6 +41,22 @@ Namely associates a subdomain with your organization. For example, if
|
|
41
41
|
your account is at `http://your-organization.namely.com/`, your
|
42
42
|
subdomain would be `"your-organization"`.
|
43
43
|
|
44
|
+
## Configuration
|
45
|
+
|
46
|
+
You can override default configuration using `Namely.configuration` like:
|
47
|
+
|
48
|
+
```Ruby
|
49
|
+
Namely.configuration do |config|
|
50
|
+
|
51
|
+
# While returning paged results, which http codes should be rescued and
|
52
|
+
# retried? Defaults to none.
|
53
|
+
config.http_codes_to_retry = [504] # gateway timeout
|
54
|
+
|
55
|
+
# The number of times to retry the requests rescued above. Default is 0
|
56
|
+
config.retries = 2
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
44
60
|
## Usage Examples
|
45
61
|
|
46
62
|
Once you've created a connection you can use it to access your data.
|
@@ -118,7 +134,7 @@ page of the site.
|
|
118
134
|
5. Create a new pull request. We'll review it and leave feedback for
|
119
135
|
you ASAP.
|
120
136
|
|
121
|
-
[Namely's HTTP API]:
|
137
|
+
[Namely's HTTP API]: https://developers.namely.com
|
122
138
|
[thoughtbot's style guide]: https://github.com/thoughtbot/guides/tree/master/style
|
123
139
|
[JSON API standard]: http://jsonapi.org/
|
124
140
|
[dotenv]: https://github.com/bkeepers/dotenv
|
data/lib/namely.rb
CHANGED
@@ -0,0 +1,56 @@
|
|
1
|
+
module Namely
|
2
|
+
class Configuration
|
3
|
+
|
4
|
+
# The http codes that should be retried if a request fails while returning
|
5
|
+
# a page in paged results. Number of times to retry specified in {#retries}.
|
6
|
+
# Defaults to an empty Array.
|
7
|
+
#
|
8
|
+
# @see #retries
|
9
|
+
# @return [Array<Integer>] the http codes to retry for a failed request
|
10
|
+
attr_reader :http_codes_to_retry
|
11
|
+
|
12
|
+
# Specifies the http codes of failed GET requests encountered while paging
|
13
|
+
# that should be retried.
|
14
|
+
#
|
15
|
+
# @param codes [Array<Integer>, Integer] http codes to retry
|
16
|
+
def http_codes_to_retry=(codes)
|
17
|
+
@http_codes_to_retry = Array(codes).map(&:to_int)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Controls the number of times that a request for a page that failed while
|
21
|
+
# returning paged results with one of the http codes listed in
|
22
|
+
# {#http_codes_to_retry} will be retried before raising
|
23
|
+
# an exception. 0 by default.
|
24
|
+
#
|
25
|
+
# @return [Integer] number of times to retry request.
|
26
|
+
attr_accessor :retries
|
27
|
+
|
28
|
+
def initialize
|
29
|
+
@http_codes_to_retry = []
|
30
|
+
@retries = 0
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# @return [Namely::Configuration] Namely's current configuration
|
35
|
+
def self.configuration
|
36
|
+
@configuration ||= Configuration.new
|
37
|
+
end
|
38
|
+
|
39
|
+
# Set Namely's configuration
|
40
|
+
# @param config [Namely::Configuration]
|
41
|
+
def self.configuration=(config)
|
42
|
+
@configuration = config
|
43
|
+
end
|
44
|
+
|
45
|
+
# Modify Namely's current configuration
|
46
|
+
# @yieldparam [Namely::Configuration] config current Namely config
|
47
|
+
# ```
|
48
|
+
# Namely.configure do |config|
|
49
|
+
# config.retries = 3
|
50
|
+
# end
|
51
|
+
# ```
|
52
|
+
def self.configure
|
53
|
+
yield configuration
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -44,7 +44,7 @@ module Namely
|
|
44
44
|
params = {}
|
45
45
|
|
46
46
|
loop do
|
47
|
-
objects = get("/#{endpoint}", params)[resource_name]
|
47
|
+
objects = with_retry { get("/#{endpoint}", params)[resource_name] }
|
48
48
|
break if objects.empty?
|
49
49
|
|
50
50
|
objects.each { |o| y << o }
|
@@ -73,6 +73,15 @@ module Namely
|
|
73
73
|
)
|
74
74
|
end
|
75
75
|
|
76
|
+
def with_retry
|
77
|
+
retries ||= 0
|
78
|
+
yield
|
79
|
+
rescue RestClient::Exception => e
|
80
|
+
raise unless Namely.configuration.http_codes_to_retry.include?(e.http_code)
|
81
|
+
retry if (retries += 1) < Namely.configuration.retries
|
82
|
+
raise
|
83
|
+
end
|
84
|
+
|
76
85
|
def get(path, params = {})
|
77
86
|
params.merge!(access_token: access_token)
|
78
87
|
JSON.parse(RestClient.get(url(path), accept: :json, params: params))
|
data/lib/namely/version.rb
CHANGED
data/namely.gemspec
CHANGED
@@ -6,7 +6,7 @@ require "namely/version"
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "namely"
|
8
8
|
spec.version = Namely::VERSION
|
9
|
-
spec.authors = ["Namely
|
9
|
+
spec.authors = ["Namely"]
|
10
10
|
spec.email = ["integrations-dev@namely.com"]
|
11
11
|
spec.summary = "Wraps the Namely HTTP API in lovely Ruby."
|
12
12
|
spec.homepage = "https://github.com/namely/ruby-client"
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Namely::Configuration do
|
4
|
+
|
5
|
+
describe "#http_codes_to_retry" do
|
6
|
+
it "is empty array by default" do
|
7
|
+
expect(Namely.configuration.http_codes_to_retry).to eq []
|
8
|
+
end
|
9
|
+
|
10
|
+
it "accepts an array of intergers" do
|
11
|
+
expected_result = http_codes = [504, 502]
|
12
|
+
|
13
|
+
Namely.configure { |config| config.http_codes_to_retry = http_codes }
|
14
|
+
|
15
|
+
expect(Namely.configuration.http_codes_to_retry).to eq expected_result
|
16
|
+
end
|
17
|
+
|
18
|
+
it "accepts a single integer" do
|
19
|
+
http_code = 504
|
20
|
+
expected_return = [http_code]
|
21
|
+
|
22
|
+
Namely.configure { |config| config.http_codes_to_retry = http_code }
|
23
|
+
|
24
|
+
expect(Namely.configuration.http_codes_to_retry).to eq expected_return
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#retries" do
|
29
|
+
it "is 0 by default" do
|
30
|
+
expect(Namely.configuration.retries).to eq 0
|
31
|
+
end
|
32
|
+
|
33
|
+
it "accepts an interger representing the number of retries" do
|
34
|
+
expected_result = number_of_retries = 3
|
35
|
+
|
36
|
+
Namely.configure { |config| config.retries = number_of_retries }
|
37
|
+
|
38
|
+
expect(Namely.configuration.retries).to eq expected_result
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -73,6 +73,38 @@ describe Namely::ResourceGateway do
|
|
73
73
|
|
74
74
|
expect(ids).to eq(['123-456', '456-789'])
|
75
75
|
end
|
76
|
+
|
77
|
+
it "retries http failures given the configured codes and number of retries" do
|
78
|
+
Namely.configuration.retries = 3
|
79
|
+
Namely.configuration.http_codes_to_retry = [504]
|
80
|
+
|
81
|
+
stub_request(:get, "https://#{subdomain}.namely.com/api/v1/widgets").
|
82
|
+
with(query: { access_token: access_token }).to_return(status:504).times(2).
|
83
|
+
then.to_return(body: { widgets: [ id: "123-456" ] }.to_json)
|
84
|
+
|
85
|
+
stub_request(:get, "https://#{subdomain}.namely.com/api/v1/widgets").
|
86
|
+
with(query: { access_token: access_token, after: "123-456" }).
|
87
|
+
to_return(body: { widgets: [ id: "456-789" ] }.to_json)
|
88
|
+
|
89
|
+
stub_request(:get, "https://#{subdomain}.namely.com/api/v1/widgets").
|
90
|
+
with(query: { access_token: access_token, after: "456-789" }).
|
91
|
+
to_return(body: { widgets: [ ] }.to_json)
|
92
|
+
|
93
|
+
ids = gateway.json_index.map { |h| h['id'] }
|
94
|
+
|
95
|
+
expect(ids).to eq(['123-456', '456-789'])
|
96
|
+
end
|
97
|
+
|
98
|
+
it "raises an exception if exceeds configured retry number" do
|
99
|
+
Namely.configuration.retries = 3
|
100
|
+
Namely.configuration.http_codes_to_retry = [504]
|
101
|
+
|
102
|
+
stub_request(:get, "https://#{subdomain}.namely.com/api/v1/widgets").
|
103
|
+
with(query: { access_token: access_token }).to_return(status: 504)
|
104
|
+
|
105
|
+
expect { gateway.json_index.map { |h| h['id'] } }.
|
106
|
+
to raise_error(RestClient::GatewayTimeout)
|
107
|
+
end
|
76
108
|
end
|
77
109
|
end
|
78
110
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: namely
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Namely
|
7
|
+
- Namely
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -143,6 +143,7 @@ files:
|
|
143
143
|
- lib/namely.rb
|
144
144
|
- lib/namely/authenticator.rb
|
145
145
|
- lib/namely/collection.rb
|
146
|
+
- lib/namely/configuration.rb
|
146
147
|
- lib/namely/connection.rb
|
147
148
|
- lib/namely/exceptions.rb
|
148
149
|
- lib/namely/model.rb
|
@@ -182,6 +183,7 @@ files:
|
|
182
183
|
- spec/fixtures/vcr_cassettes/token.yml
|
183
184
|
- spec/fixtures/vcr_cassettes/token_refresh.yml
|
184
185
|
- spec/namely/authenticator_spec.rb
|
186
|
+
- spec/namely/configuration_spec.rb
|
185
187
|
- spec/namely/connection_spec.rb
|
186
188
|
- spec/namely/integration_spec.rb
|
187
189
|
- spec/namely/resource_gateway_spec.rb
|
@@ -210,7 +212,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
210
212
|
version: '0'
|
211
213
|
requirements: []
|
212
214
|
rubyforge_project:
|
213
|
-
rubygems_version: 2.6.
|
215
|
+
rubygems_version: 2.6.13
|
214
216
|
signing_key:
|
215
217
|
specification_version: 4
|
216
218
|
summary: Wraps the Namely HTTP API in lovely Ruby.
|
@@ -248,6 +250,7 @@ test_files:
|
|
248
250
|
- spec/fixtures/vcr_cassettes/token.yml
|
249
251
|
- spec/fixtures/vcr_cassettes/token_refresh.yml
|
250
252
|
- spec/namely/authenticator_spec.rb
|
253
|
+
- spec/namely/configuration_spec.rb
|
251
254
|
- spec/namely/connection_spec.rb
|
252
255
|
- spec/namely/integration_spec.rb
|
253
256
|
- spec/namely/resource_gateway_spec.rb
|