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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7e15ca6aec9f2e4b87115f96ad199b4e9689badc
4
- data.tar.gz: 3d47e5b284423f8984d299f887e55fc74322d945
3
+ metadata.gz: a67e3173fd83c076f30bce37d2f6a1d9a80e4426
4
+ data.tar.gz: 7ee2e4a0ac5ef5604f59cfa58ee96a2fdfe37177
5
5
  SHA512:
6
- metadata.gz: c4ff2fbfd692cce791d982c511a088c8c3226207564d1aa6e5608b10447cba41fb7e83e8bea20591e9ccded1603c9abfa774270e9b2abbbe2c759f5e17df8b1a
7
- data.tar.gz: 88df0bde69c06d859a642d099de7cda4926712798fe2feeb2817d0a471c07416e931b596f51968f536d0f962253ac092194019d1b7dc0719fde575281187253b
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 *args
35
- response = RestClient.put(
36
- build_url(url || subdomain, path),
37
- body.to_json,
38
- headers.merge(content_type_header))
39
- JSON.parse response.body, symbolize_names: true unless response.code == 204
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 *args
45
- RestClient.delete(build_url(url || subdomain, path), headers)
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 *args
51
- response = RestClient.post(
52
- build_url(url || subdomain, path),
53
- body.to_json,
54
- headers.merge(content_type_header))
55
- JSON.parse response.body, symbolize_names: true unless response.code == 204
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 *args
66
+ url, subdomain, path, params = parse_args(*args)
61
67
  JSON.parse(
62
- RestClient.get(
63
- build_url(url || subdomain, path, params),
64
- headers).body,
65
- symbolize_names: true
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
@@ -1,3 +1,3 @@
1
1
  module CobotClient
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/cobot_client.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "cobot_client/version"
2
2
  require 'cobot_client/engine' if defined?(Rails)
3
+ require 'cobot_client/exceptions'
3
4
 
4
5
  module CobotClient
5
6
  autoload :ApiClient, 'cobot_client/api_client'
@@ -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.1.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: 2014-12-09 00:00:00.000000000 Z
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