packlink_lite 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -0
- data/lib/packlink_lite/client.rb +41 -52
- data/lib/packlink_lite/configuration.rb +5 -1
- data/lib/packlink_lite/label.rb +2 -2
- data/lib/packlink_lite/order.rb +2 -2
- data/lib/packlink_lite/service.rb +2 -2
- data/lib/packlink_lite/shipment.rb +4 -4
- data/lib/packlink_lite/tracking_history.rb +2 -2
- data/lib/packlink_lite/version.rb +1 -1
- data/packlink_lite.gemspec +2 -1
- metadata +20 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ccaab6fdc49a94edc939dd75fb129a75d188a6e
|
4
|
+
data.tar.gz: effae85c177be32d29907f89948d1859a06b3f36
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 262a7e460c8fb55bd3e23273e5978525e780d83d0544910ff7e4e277288f09f5f39d966717f9d8f5bf3306ad062cead28bec0ab60df7a076b4326a7c506f5cbb
|
7
|
+
data.tar.gz: 108d0de88dfaf74dbeff54098c595218f8b67d6ab0ee3d7a186a9e0f7dd400030604d80d02e58c05e2392b5e84cdfebafdaca77df14e441663517434ad3a96ba
|
data/README.md
CHANGED
@@ -21,6 +21,14 @@ PacklinkLite.configure do |config|
|
|
21
21
|
end
|
22
22
|
```
|
23
23
|
|
24
|
+
API key can be passed via options too:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
PacklinkLite::Service.all(query, api_key: 'mykey')
|
28
|
+
PacklinkLite::Order.create(order_hash, api_key: 'mykey')
|
29
|
+
|
30
|
+
```
|
31
|
+
|
24
32
|
## Usage
|
25
33
|
|
26
34
|
### Fetch services list
|
data/lib/packlink_lite/client.rb
CHANGED
@@ -1,80 +1,69 @@
|
|
1
|
-
require '
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday_middleware'
|
2
3
|
|
3
4
|
module PacklinkLite
|
4
5
|
class Client
|
5
6
|
def get(path, params = nil, options = {})
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
with_error_handling do
|
8
|
+
response = connection(options[:api_key]).get(path, params)
|
9
|
+
response.body
|
10
|
+
end
|
10
11
|
end
|
11
12
|
|
12
13
|
def post(path, payload, options = {})
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
with_error_handling do
|
15
|
+
response = connection(options[:api_key]).post(path, payload)
|
16
|
+
response.body
|
17
|
+
end
|
16
18
|
end
|
17
19
|
|
18
20
|
def delete(path, options = {})
|
19
|
-
|
20
|
-
|
21
|
+
with_error_handling do
|
22
|
+
response = connection(options[:api_key]).delete(path)
|
23
|
+
response.body
|
24
|
+
end
|
21
25
|
end
|
22
26
|
|
23
27
|
private
|
24
28
|
|
25
|
-
def
|
26
|
-
|
27
|
-
|
29
|
+
def with_error_handling
|
30
|
+
yield
|
31
|
+
rescue Faraday::ClientError => e
|
32
|
+
message = extract_error_message(e.response[:body]) || e.message
|
33
|
+
raise(Error, message)
|
28
34
|
end
|
29
35
|
|
30
|
-
def
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
raise Error.new(message, e.http_body, e.http_code)
|
36
|
+
def connection(api_key)
|
37
|
+
@connection ||= build_connection
|
38
|
+
@connection.headers['Authorization'] = api_key || PacklinkLite.config.api_key
|
39
|
+
@connection
|
35
40
|
end
|
36
41
|
|
37
|
-
def
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
end
|
42
|
+
def build_connection
|
43
|
+
Faraday.new(url: PacklinkLite.url) do |builder|
|
44
|
+
builder.options[:timeout] = config.timeout
|
45
|
+
builder.options[:open_timeout] = config.timeout
|
42
46
|
|
43
|
-
|
44
|
-
|
45
|
-
end
|
47
|
+
builder.request :retry
|
48
|
+
builder.request :json
|
46
49
|
|
47
|
-
|
48
|
-
JSON.parse(response)
|
49
|
-
rescue JSON::ParserError => e
|
50
|
-
raise ResponseError.new(e.message, response)
|
51
|
-
end
|
50
|
+
builder.headers['Accept'] = 'application/json'
|
52
51
|
|
53
|
-
|
54
|
-
|
55
|
-
'Content-Type' => 'application/json' }
|
56
|
-
end
|
52
|
+
builder.response :json, content_type: /\bjson$/
|
53
|
+
builder.response :raise_error
|
57
54
|
|
58
|
-
|
59
|
-
case value
|
60
|
-
when Array
|
61
|
-
value.map { |v|
|
62
|
-
build_nested_query(v, "#{prefix}[]")
|
63
|
-
}.join("&")
|
64
|
-
when Hash
|
65
|
-
value.map { |k, v|
|
66
|
-
build_nested_query(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k))
|
67
|
-
}.reject(&:empty?).join('&')
|
68
|
-
when nil
|
69
|
-
prefix
|
70
|
-
else
|
71
|
-
raise ArgumentError, "value must be a Hash" if prefix.nil?
|
72
|
-
"#{prefix}=#{escape(value)}"
|
55
|
+
builder.adapter Faraday.default_adapter
|
73
56
|
end
|
74
57
|
end
|
75
58
|
|
76
|
-
def
|
77
|
-
|
59
|
+
def extract_error_message(response_body)
|
60
|
+
result = JSON.parse(response_body)
|
61
|
+
result['message'] || result['messages']
|
62
|
+
rescue JSON::ParserError
|
63
|
+
end
|
64
|
+
|
65
|
+
def config
|
66
|
+
PacklinkLite.config
|
78
67
|
end
|
79
68
|
end
|
80
69
|
end
|
data/lib/packlink_lite/label.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module PacklinkLite
|
2
2
|
class Label
|
3
|
-
def self.all(shipment_reference)
|
4
|
-
PacklinkLite.client.get("shipments/#{shipment_reference}/labels")
|
3
|
+
def self.all(shipment_reference, options = {})
|
4
|
+
PacklinkLite.client.get("shipments/#{shipment_reference}/labels", {}, options)
|
5
5
|
end
|
6
6
|
end
|
7
7
|
end
|
data/lib/packlink_lite/order.rb
CHANGED
@@ -2,12 +2,12 @@ module PacklinkLite
|
|
2
2
|
class Shipment
|
3
3
|
PATH = 'shipments'
|
4
4
|
|
5
|
-
def self.all(params = {})
|
6
|
-
PacklinkLite.client.get(PATH, params)
|
5
|
+
def self.all(params = {}, options = {})
|
6
|
+
PacklinkLite.client.get(PATH, params, options)
|
7
7
|
end
|
8
8
|
|
9
|
-
def self.find(reference)
|
10
|
-
PacklinkLite.client.get(PATH + '/' + reference)
|
9
|
+
def self.find(reference, options = {})
|
10
|
+
PacklinkLite.client.get(PATH + '/' + reference, {}, options)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module PacklinkLite
|
2
2
|
class TrackingHistory
|
3
|
-
def self.find(shipment_reference)
|
4
|
-
PacklinkLite.client.get("shipments/#{shipment_reference}/track")
|
3
|
+
def self.find(shipment_reference, options = {})
|
4
|
+
PacklinkLite.client.get("shipments/#{shipment_reference}/track", {}, options)
|
5
5
|
end
|
6
6
|
end
|
7
7
|
end
|
data/packlink_lite.gemspec
CHANGED
@@ -19,7 +19,8 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ['lib']
|
21
21
|
|
22
|
-
spec.add_dependency '
|
22
|
+
spec.add_dependency 'faraday', '~> 0.9'
|
23
|
+
spec.add_dependency 'faraday_middleware', '~> 0.10'
|
23
24
|
|
24
25
|
spec.add_development_dependency 'bundler', '~> 1.11'
|
25
26
|
spec.add_development_dependency 'rake', '~> 10.0'
|
metadata
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: packlink_lite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Laurynas Butkus
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: faraday
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0.9'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday_middleware
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.10'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.10'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -141,9 +155,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
155
|
version: '0'
|
142
156
|
requirements: []
|
143
157
|
rubyforge_project:
|
144
|
-
rubygems_version: 2.
|
158
|
+
rubygems_version: 2.4.5
|
145
159
|
signing_key:
|
146
160
|
specification_version: 4
|
147
161
|
summary: Packlink.com API client
|
148
162
|
test_files: []
|
149
|
-
has_rdoc:
|