cobot_client 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a67e3173fd83c076f30bce37d2f6a1d9a80e4426
4
- data.tar.gz: 7ee2e4a0ac5ef5604f59cfa58ee96a2fdfe37177
3
+ metadata.gz: 6065b773f6bf01f885cbf320e358894fdb5022ec
4
+ data.tar.gz: 2a522841f501b3604930674f6e3d69aa95f2ceca
5
5
  SHA512:
6
- metadata.gz: 94847dfac172d129492c411cac5b64c1c3da5a13f8a2219b7216f3f892d9d64ecb409e48796ae15eaec3a6819397e574748a3ab27e5e1c2b093e72388a5cc45b
7
- data.tar.gz: 40c671e342f8d083df6277912583c03d811e1b3bbb9cead2fc18d3b700a0ac79fd91238a92d7f0a1144dbc349e53ec64d3edd7ca59c2a6f56b0a4f86273251e6
6
+ metadata.gz: c550f7fdb4895e31ad25d37e7201aaca0a1bebe9724dd0ac0783ea47ed50d436f40bd2e48c538101b94e6271f0deb5d0b902f3b0aeb39b2124c3f4757400dd37
7
+ data.tar.gz: ba50673265ee4f7e7f9555b06e7364b705b6aa272c406b1dd544d9899c55b3cd6f5e2f8c56054914d7bfbdb765156c84a72cbdcb943c1485a4edf3cc3e290047
@@ -6,8 +6,9 @@ module CobotClient
6
6
  include UrlHelper
7
7
 
8
8
  class << self
9
- attr_accessor :user_agent
9
+ attr_accessor :user_agent, :retry_time
10
10
  end
11
+ self.retry_time = 1
11
12
 
12
13
  def initialize(access_token)
13
14
  @access_token = access_token
@@ -30,35 +31,13 @@ module CobotClient
30
31
  end
31
32
 
32
33
  # args: either a full URL or subdomain, path, plus a body as hash
33
- def put(*args)
34
- url, subdomain, path, body = parse_args(*args)
35
- rewrap_errors do
36
- response = RestClient.put(
37
- build_url(url || subdomain, path),
38
- body.to_json,
39
- headers.merge(content_type_header))
40
- JSON.parse response.body, symbolize_names: true unless response.code == 204
41
- end
42
- end
43
-
44
- # args: either a full URL or subdomain, path
45
- def delete(*args)
46
- url, subdomain, path, _ = parse_args(*args)
47
- rewrap_errors do
48
- RestClient.delete(build_url(url || subdomain, path), headers)
49
- end
34
+ def post(*args)
35
+ request :post, *args
50
36
  end
51
37
 
52
38
  # args: either a full URL or subdomain, path, plus a body as hash
53
- def post(*args)
54
- url, subdomain, path, body = parse_args(*args)
55
- rewrap_errors do
56
- response = RestClient.post(
57
- build_url(url || subdomain, path),
58
- body.to_json,
59
- headers.merge(content_type_header))
60
- JSON.parse response.body, symbolize_names: true unless response.code == 204
61
- end
39
+ def put(*args)
40
+ request :put, *args
62
41
  end
63
42
 
64
43
  # args: either a full URL or subdomain, path, plus an optional params hash
@@ -72,12 +51,40 @@ module CobotClient
72
51
  end, symbolize_names: true)
73
52
  end
74
53
 
54
+ # args: either a full URL or subdomain, path
55
+ def delete(*args)
56
+ url, subdomain, path, _ = parse_args(*args)
57
+ rewrap_errors do
58
+ RestClient.delete(build_url(url || subdomain, path), headers)
59
+ end
60
+ end
61
+
75
62
  private
76
63
 
64
+ def request(method, *args)
65
+ url, subdomain, path, body = parse_args(*args)
66
+ rewrap_errors do
67
+ response = RestClient.public_send(method,
68
+ build_url(url || subdomain, path),
69
+ body.to_json,
70
+ headers.merge(content_type_header))
71
+ JSON.parse response.body, symbolize_names: true unless response.code == 204
72
+ end
73
+ end
74
+
77
75
  def rewrap_errors
76
+ retries = 0
78
77
  yield
79
- rescue RestClient::Exception => e
80
- raise CobotClient::Exceptions::EXCEPTIONS_MAP[e.class].new(e.response)
78
+ rescue RestClient::BadGateway => e
79
+ if retries < 3
80
+ sleep self.class.retry_time
81
+ retries += 1
82
+ retry
83
+ else
84
+ raise e
85
+ end
86
+ rescue RestClient::Exception => e
87
+ fail CobotClient::Exceptions::EXCEPTIONS_MAP[e.class].new(e.response)
81
88
  end
82
89
 
83
90
  def parse_args(*args)
@@ -1,3 +1,3 @@
1
1
  module CobotClient
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
@@ -6,6 +6,7 @@ describe CobotClient::ApiClient do
6
6
 
7
7
  before(:each) do
8
8
  CobotClient::ApiClient.user_agent = 'test agent'
9
+ CobotClient::ApiClient.retry_time = 0
9
10
  end
10
11
 
11
12
  context 'listing resources' do
@@ -107,6 +108,20 @@ describe CobotClient::ApiClient do
107
108
 
108
109
  expect(api_client.put('co-up', '/invoices', {})).to be_nil
109
110
  end
111
+
112
+ it 'retries a 502 error' do
113
+ @times = 0
114
+ allow(RestClient).to receive(:put) do
115
+ if @times < 3
116
+ @times += 1
117
+ fail RestClient::BadGateway
118
+ else
119
+ double(code: 200, body: {success: true}.to_json)
120
+ end
121
+ end
122
+
123
+ expect(api_client.put('co-up', '/invoices', {})).to eql(success: true)
124
+ end
110
125
  end
111
126
 
112
127
  context '#post' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cobot_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Lang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-09 00:00:00.000000000 Z
11
+ date: 2015-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus