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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dcd5d4af3dbd874f0f5de6ba024e4e6ca3ba3770
4
- data.tar.gz: 7cba79eae714f11570a8c60716635e4a0d8da2c5
3
+ metadata.gz: 7ccaab6fdc49a94edc939dd75fb129a75d188a6e
4
+ data.tar.gz: effae85c177be32d29907f89948d1859a06b3f36
5
5
  SHA512:
6
- metadata.gz: 116974f970164cc7e9bd429822b28f89d8263375d3d4f5f09d0e600ca9de3a1d3276148700dd58e4020e37206ad57fb663550b31655fe750090dae0a77bc15c7
7
- data.tar.gz: 7c99040a265acf725f20901de705c9e979995ed2b4010e4c0db971e874dad15b49a80802dc98f4bfbe4ec50ecd105f85a2a7a49a25ca6419c34a07230f0eaf6d
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
@@ -1,80 +1,69 @@
1
- require 'rest-client'
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
- url = url(path)
7
- url+= '?' + build_nested_query(params) if params
8
- response = request(:get, url, headers)
9
- process_response(response, options)
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
- payload = payload.to_json unless payload.is_a?(String)
14
- response = request(:post, url(path), payload, headers)
15
- process_response(response, options)
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
- response = request(:delete, url(path))
20
- process_response(response, options)
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 process_response(response, parse_response: true)
26
- return response unless parse_response
27
- parse_response(response)
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 request(method, *args)
31
- RestClient.send(method, *args)
32
- rescue RestClient::Exception => e
33
- message = extract_error_message(e.http_body) || e.message
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 extract_error_message(body)
38
- result = parse_response(body)
39
- result['message'] || result['messages']
40
- rescue JSON::ParserError
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
- def url(path)
44
- PacklinkLite.url + path
45
- end
47
+ builder.request :retry
48
+ builder.request :json
46
49
 
47
- def parse_response(response)
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
- def headers
54
- { 'Authorization' => PacklinkLite.config.api_key,
55
- 'Content-Type' => 'application/json' }
56
- end
52
+ builder.response :json, content_type: /\bjson$/
53
+ builder.response :raise_error
57
54
 
58
- def build_nested_query(value, prefix = nil)
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 escape(s)
77
- URI.encode_www_form_component(s)
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
@@ -1,6 +1,10 @@
1
1
  module PacklinkLite
2
2
  class Configuration
3
- attr_accessor :api_key, :testing
3
+ attr_accessor :api_key, :testing, :timeout
4
4
  alias_method :testing?, :testing
5
+
6
+ def timeout
7
+ @timeout || 60
8
+ end
5
9
  end
6
10
  end
@@ -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
@@ -2,8 +2,8 @@ module PacklinkLite
2
2
  class Order
3
3
  PATH = 'orders'
4
4
 
5
- def self.create(payload)
6
- PacklinkLite.client.post(PATH, payload)
5
+ def self.create(payload, options = {})
6
+ PacklinkLite.client.post(PATH, payload, options)
7
7
  end
8
8
  end
9
9
  end
@@ -2,8 +2,8 @@ module PacklinkLite
2
2
  class Service
3
3
  PATH = 'services'
4
4
 
5
- def self.all(query)
6
- PacklinkLite.client.get(PATH, query)
5
+ def self.all(query, options = {})
6
+ PacklinkLite.client.get(PATH, query, options)
7
7
  end
8
8
  end
9
9
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module PacklinkLite
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -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 'rest-client', '~> 1.8'
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.1.2
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-03-30 00:00:00.000000000 Z
11
+ date: 2016-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rest-client
14
+ name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.8'
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: '1.8'
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.2.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: