cobot_client 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cobot_client/api_client.rb +32 -20
- data/lib/cobot_client/exceptions.rb +17 -0
- data/lib/cobot_client/version.rb +1 -1
- data/lib/cobot_client.rb +1 -0
- data/spec/cobot_client/api_client_spec.rb +22 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a67e3173fd83c076f30bce37d2f6a1d9a80e4426
|
4
|
+
data.tar.gz: 7ee2e4a0ac5ef5604f59cfa58ee96a2fdfe37177
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94847dfac172d129492c411cac5b64c1c3da5a13f8a2219b7216f3f892d9d64ecb409e48796ae15eaec3a6819397e574748a3ab27e5e1c2b093e72388a5cc45b
|
7
|
+
data.tar.gz: 40c671e342f8d083df6277912583c03d811e1b3bbb9cead2fc18d3b700a0ac79fd91238a92d7f0a1144dbc349e53ec64d3edd7ca59c2a6f56b0a4f86273251e6
|
@@ -31,43 +31,55 @@ module CobotClient
|
|
31
31
|
|
32
32
|
# args: either a full URL or subdomain, path, plus a body as hash
|
33
33
|
def put(*args)
|
34
|
-
url, subdomain, path, body = parse_args
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
40
42
|
end
|
41
43
|
|
42
44
|
# args: either a full URL or subdomain, path
|
43
45
|
def delete(*args)
|
44
|
-
url, subdomain, path, _ = parse_args
|
45
|
-
|
46
|
+
url, subdomain, path, _ = parse_args(*args)
|
47
|
+
rewrap_errors do
|
48
|
+
RestClient.delete(build_url(url || subdomain, path), headers)
|
49
|
+
end
|
46
50
|
end
|
47
51
|
|
48
52
|
# args: either a full URL or subdomain, path, plus a body as hash
|
49
53
|
def post(*args)
|
50
|
-
url, subdomain, path, body = parse_args
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
56
62
|
end
|
57
63
|
|
58
64
|
# args: either a full URL or subdomain, path, plus an optional params hash
|
59
65
|
def get(*args)
|
60
|
-
url, subdomain, path, params = parse_args
|
66
|
+
url, subdomain, path, params = parse_args(*args)
|
61
67
|
JSON.parse(
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
68
|
+
rewrap_errors do
|
69
|
+
RestClient.get(
|
70
|
+
build_url(url || subdomain, path, params),
|
71
|
+
headers).body
|
72
|
+
end, symbolize_names: true)
|
67
73
|
end
|
68
74
|
|
69
75
|
private
|
70
76
|
|
77
|
+
def rewrap_errors
|
78
|
+
yield
|
79
|
+
rescue RestClient::Exception => e
|
80
|
+
raise CobotClient::Exceptions::EXCEPTIONS_MAP[e.class].new(e.response)
|
81
|
+
end
|
82
|
+
|
71
83
|
def parse_args(*args)
|
72
84
|
if args.last.is_a?(Hash)
|
73
85
|
params = args.pop
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rest_client'
|
2
|
+
|
3
|
+
module CobotClient
|
4
|
+
module Exceptions
|
5
|
+
EXCEPTIONS_MAP = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
class Exception < RestClient::Exception
|
9
|
+
end
|
10
|
+
|
11
|
+
RestClient::STATUSES.each_pair do |code, message|
|
12
|
+
superclass = RestClient::Exceptions::EXCEPTIONS_MAP.fetch code
|
13
|
+
klass = Class.new(superclass)
|
14
|
+
klass_constant = const_set message.delete(' \-\''), klass
|
15
|
+
Exceptions::EXCEPTIONS_MAP[superclass] = klass_constant
|
16
|
+
end
|
17
|
+
end
|
data/lib/cobot_client/version.rb
CHANGED
data/lib/cobot_client.rb
CHANGED
@@ -167,6 +167,28 @@ describe CobotClient::ApiClient do
|
|
167
167
|
|
168
168
|
expect(api_client.get('co-up', '/invoices')).to eql([{number: 1}])
|
169
169
|
end
|
170
|
+
|
171
|
+
it 'converts a rest-client error into a cobot error' do
|
172
|
+
allow(RestClient).to receive(:get).and_raise(RestClient::ResourceNotFound)
|
173
|
+
|
174
|
+
expect do
|
175
|
+
api_client.get('co-up', '/invoices')
|
176
|
+
end.to raise_error(CobotClient::ResourceNotFound)
|
177
|
+
end
|
178
|
+
|
179
|
+
it 'includes the response, http code and http body in the exception' do
|
180
|
+
response = double(:response, code: 404, body: 'boom')
|
181
|
+
error = RestClient::ResourceNotFound.new(response)
|
182
|
+
allow(RestClient).to receive(:get).and_raise(error)
|
183
|
+
|
184
|
+
begin
|
185
|
+
api_client.get('co-up', '/invoices')
|
186
|
+
rescue CobotClient::ResourceNotFound => e
|
187
|
+
expect(e.response).to eql(response)
|
188
|
+
expect(e.http_code).to eql(404)
|
189
|
+
expect(e.http_body).to eql('boom')
|
190
|
+
end
|
191
|
+
end
|
170
192
|
end
|
171
193
|
|
172
194
|
context '#delete' 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.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Lang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: virtus
|
@@ -114,6 +114,7 @@ files:
|
|
114
114
|
- lib/cobot_client.rb
|
115
115
|
- lib/cobot_client/api_client.rb
|
116
116
|
- lib/cobot_client/engine.rb
|
117
|
+
- lib/cobot_client/exceptions.rb
|
117
118
|
- lib/cobot_client/navigation_link.rb
|
118
119
|
- lib/cobot_client/navigation_link_service.rb
|
119
120
|
- lib/cobot_client/url_helper.rb
|