packlink_lite 0.1.0 → 0.1.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: 0ff464661f493b1f01020f2db2bf298cdeae58e8
4
- data.tar.gz: 0091ddb428e56da4c4c1c603c56bd5c399e6981e
3
+ metadata.gz: 3e2dd2e94cf5d52c7eebdf4a18042255649e249d
4
+ data.tar.gz: 057cba8b7c63214dc2e7eee4bf97508d33820bdd
5
5
  SHA512:
6
- metadata.gz: c667effad5cb1441e491b7fb4f857f8f6e4af04d4937e5d19ea612cc0090731a2419591d66e11fdbba2d9802b185bce4bb2cc12bb06e4d107fb748475f0fb2f3
7
- data.tar.gz: 090f5b0896429dbf3c2fd60e09ec3c3fa468e995e4a891235c86f484d8a05f74bb27747c9b173047a2f3f335905acc9e01649148bf31ac7244601fcd0d9ed34f
6
+ metadata.gz: 21f2b551b674a7a7dfb18e4e879963524642f44418a637e9346b5e7f58d6e1b595adba1402f5ee42a291c3faa058b07f04c9bb733d30f8ab6cd9c1783bba7ac0
7
+ data.tar.gz: e3935ecc003e5168d792572c3cb76351f315d0ead833dbbfaedc0bc95b86f470f993996879c3d5d8bba068f56af9940b20b0f85c7e504e9aa44167bf2e9fc250
data/lib/packlink_lite.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'packlink_lite/version'
2
2
  require 'packlink_lite/configuration'
3
+ require 'packlink_lite/errors'
3
4
  require 'packlink_lite/client'
4
5
  require 'packlink_lite/service'
5
6
  require 'packlink_lite/order'
@@ -5,28 +5,39 @@ module PacklinkLite
5
5
  def get(path, params = nil)
6
6
  url = url(path)
7
7
  url+= '?' + build_nested_query(params) if params
8
- response = RestClient.get(url, headers)
9
- handle_response(response)
8
+ request(:get, url, headers)
10
9
  end
11
10
 
12
11
  def post(path, payload)
13
12
  payload = payload.to_json unless payload.is_a?(String)
14
- response = RestClient.post(url(path), payload, headers)
15
- handle_response(response)
13
+ request(:post, url(path), payload, headers)
16
14
  end
17
15
 
18
16
  def delete(path)
19
- response = RestClient.delete(url(path))
20
- handle_response(response)
17
+ request(:delete, url(path))
21
18
  end
22
19
 
23
20
  private
24
21
 
22
+ def request(method, *args)
23
+ response = RestClient.send(method, *args)
24
+ parse_response(response)
25
+ rescue RestClient::Exception => e
26
+ message = extract_error_message(e.http_body) || e.message
27
+ raise Error.new(message, e.http_body, e.http_code)
28
+ end
29
+
30
+ def extract_error_message(body)
31
+ result = parse_response(body)
32
+ result['message'] || result['messages']
33
+ rescue JSON::ParserError
34
+ end
35
+
25
36
  def url(path)
26
37
  PacklinkLite.url + path
27
38
  end
28
39
 
29
- def handle_response(response)
40
+ def parse_response(response)
30
41
  JSON.parse(response)
31
42
  end
32
43
 
@@ -0,0 +1,23 @@
1
+ module PacklinkLite
2
+ class Error < StandardError
3
+ attr_reader :http_body, :http_code
4
+
5
+ def initialize(message, http_body = nil, http_code = nil)
6
+ @message = message
7
+ @http_body = http_body
8
+ @http_code = http_code
9
+ end
10
+
11
+ def inspect
12
+ "#{message}: #{http_body}"
13
+ end
14
+
15
+ def message
16
+ @message || self.class.to_s
17
+ end
18
+
19
+ def to_s
20
+ inspect
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module PacklinkLite
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: packlink_lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Laurynas Butkus
@@ -113,6 +113,7 @@ files:
113
113
  - lib/packlink_lite.rb
114
114
  - lib/packlink_lite/client.rb
115
115
  - lib/packlink_lite/configuration.rb
116
+ - lib/packlink_lite/errors.rb
116
117
  - lib/packlink_lite/label.rb
117
118
  - lib/packlink_lite/order.rb
118
119
  - lib/packlink_lite/service.rb