xdelivery 1.0.0 → 2.0.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
- SHA1:
3
- metadata.gz: 32e729a05b87441921a17b67e77554b16d1115b0
4
- data.tar.gz: 9a1734fc97bb1424e72cbf0b637d2ca2996cb592
2
+ SHA256:
3
+ metadata.gz: f5b44f938f1f48e3d3bf8003ea9bd3d316d2ccf5008ef1c07b1cd111fdabae5a
4
+ data.tar.gz: ea978fb994e963bfbb5b2d03f4fe17a9ddbae651aea43619dc9412f3bb746b65
5
5
  SHA512:
6
- metadata.gz: f66bfcdad33a55d88e597c722995ad7611a5efea9af5dadcc17c1fcbebecb374b2c203f4c7446aba88bf390efb6364296e6347d0e4f1f27a053fb7a6244bb0a3
7
- data.tar.gz: 8819f364dfa29f97ca66f4e44cc7cc0e76f71848b97a77d25e54ff3e0cde36a17701c9fda6182fd59ac28ecb5f948da25dda4267fdd7a607b91ab70e0778e435
6
+ metadata.gz: 91bb5cc8117bdae52cd7165df6a273a87dd5d0ee929aa39fab77f73195c01dec30a91a4cdac5106e6a7e6351aea9c825ceca37f0aabb5dacbed1678c8c1930cc
7
+ data.tar.gz: ed1f71dc8d7ff3525404ea7ae4ba7e6b9c3fcb58cf385c9ff1fce229ee134054cf5277affefd499eb411c45210842dee7467058d8738d9fb7feb193a7c0631d2
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- xdelivery (1.0.0)
4
+ xdelivery (2.0.0)
5
5
  rest-client
6
6
 
7
7
  GEM
@@ -41,7 +41,7 @@ PLATFORMS
41
41
  ruby
42
42
 
43
43
  DEPENDENCIES
44
- bundler (~> 1.16)
44
+ bundler (~> 2.3.14)
45
45
  minitest (~> 5.0)
46
46
  minitest-stub-const
47
47
  rake (~> 10.0)
@@ -50,4 +50,4 @@ DEPENDENCIES
50
50
  xdelivery!
51
51
 
52
52
  BUNDLED WITH
53
- 1.16.6
53
+ 2.3.14
@@ -6,33 +6,50 @@ module Xdelivery
6
6
  class Base
7
7
  attr_accessor :merchant_no, :access_key
8
8
 
9
- BASE_URL = 'https://api.xdelivery.io'
9
+ PRODUCTION_BASE_URL = 'https://api.xdelivery.io'
10
+ TEST_BASE_URL = 'https://api.staging.xdelivery.io'
10
11
 
11
12
  def initialize(merchant_no='', access_key='')
12
13
  self.merchant_no = merchant_no
13
14
  self.access_key = access_key
14
15
  end
15
16
 
17
+ def base_url
18
+ @base_url = if ::Xdelivery.production?
19
+ PRODUCTION_BASE_URL
20
+ else
21
+ TEST_BASE_URL
22
+ end
23
+ end
24
+
16
25
  protected
17
26
 
18
27
  def patch(path)
19
- RestClient.patch(uri(path).to_s, patch_data)
28
+ RestClient::Request.execute(method: :patch, url: uri(path).to_s, payload: patch_data, open_timeout: open_timeout, read_timeout: read_timeout)
20
29
  rescue RestClient::ExceptionWithResponse => e
21
30
  e.response
22
31
  end
23
32
 
24
33
  def post(path)
25
- RestClient.post(uri(path).to_s, post_data)
34
+ RestClient::Request.execute(method: :post, url: uri(path).to_s, payload: post_data, open_timeout: open_timeout, read_timeout: read_timeout)
26
35
  rescue RestClient::ExceptionWithResponse => e
27
36
  e.response
28
37
  end
29
38
 
30
39
  def get(path)
31
- RestClient.get(uri(path).to_s)
40
+ RestClient::Request.execute(method: :get, url: uri(path).to_s, open_timeout: open_timeout, read_timeout: read_timeout)
32
41
  rescue RestClient::ExceptionWithResponse => e
33
42
  e.response
34
43
  end
35
44
 
45
+ def open_timeout
46
+ Xdelivery.open_timeout
47
+ end
48
+
49
+ def read_timeout
50
+ Xdelivery.read_timeout
51
+ end
52
+
36
53
  # [GET] query string params
37
54
  def params
38
55
  {}
@@ -51,7 +68,7 @@ module Xdelivery
51
68
  private
52
69
 
53
70
  def uri(path)
54
- uri = URI.parse("#{BASE_URL}#{path}").tap { |u| u.query = query_auth_params }
71
+ uri = URI.parse("#{base_url}#{path}").tap { |u| u.query = query_auth_params }
55
72
  end
56
73
 
57
74
  def query_auth_params
@@ -6,16 +6,38 @@ module Xdelivery
6
6
 
7
7
  def initialize(response)
8
8
  self.response = response
9
- self.data = JSON.parse(response.body)
9
+ handle_error!
10
+
11
+ self.data = begin
12
+ JSON.parse(response.body)
13
+ rescue JSON::ParserError
14
+ raise Client::UnknownResponse, "#{code}, Response is not json."
15
+ end
16
+ end
17
+
18
+ def code
19
+ response.code
10
20
  end
11
21
 
12
22
  def auth?
13
- response.code == 200
23
+ code == 200
14
24
  end
15
25
 
16
26
  def status?
17
27
  data['status'] == true
18
28
  end
29
+
30
+ protected
31
+
32
+ def handle_error!
33
+ if Client::EXCEPTION_STATUSES[code]
34
+ raise Exceptions::EXCEPTIONS_MAP[code]
35
+ end
36
+
37
+ unless Client::EXPECTED_STATUSES[code]
38
+ raise Client::UnknownResponse, "Unexpected response status code: #{code}."
39
+ end
40
+ end
19
41
  end
20
42
  end
21
43
  end
@@ -0,0 +1,33 @@
1
+ module Xdelivery
2
+
3
+ module Exceptions
4
+
5
+ EXCEPTIONS_MAP = {}
6
+ end
7
+
8
+ class Client
9
+ class UnknownResponse < StandardError; end
10
+
11
+ EXPECTED_STATUSES = {
12
+ 200 => 'Ok' ,
13
+ 401 => 'Unauthorized'
14
+ }
15
+
16
+ EXCEPTION_STATUSES = {
17
+ 404 => 'Not Found',
18
+ 500 => 'Internal Server Error',
19
+ 502 => 'Bad Gateway',
20
+ 503 => 'Service Unavailable',
21
+ 504 => 'Gateway Timeout',
22
+ }
23
+
24
+ EXCEPTION_STATUSES.each_pair do |code, message|
25
+ klass = Class.new(StandardError) do
26
+ send(:define_method, :message) { "#{code}, #{message}" }
27
+ end
28
+ const_name = message.delete(',\.\-\'\"').split(' ').map(&:capitalize).join
29
+ klass_constant = const_set(const_name, klass)
30
+ Exceptions::EXCEPTIONS_MAP[code] = klass_constant
31
+ end
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module Xdelivery
2
- VERSION = "1.0.0"
2
+ VERSION = "2.0.0"
3
3
  end
data/lib/xdelivery.rb CHANGED
@@ -3,6 +3,7 @@ require 'rest-client'
3
3
  require "xdelivery/version"
4
4
  require "xdelivery/callback"
5
5
  require "xdelivery/client"
6
+ require "xdelivery/exceptions"
6
7
  require "xdelivery/api/base"
7
8
  require "xdelivery/api/orders"
8
9
  require "xdelivery/api/sales"
@@ -17,7 +18,41 @@ require "xdelivery/api/response/shops"
17
18
 
18
19
 
19
20
  module Xdelivery
20
- def url
21
- API::Base::BASE_URL
21
+
22
+ @@open_timeout = 5
23
+ @@read_timeout = 5
24
+
25
+ @@env = :production
26
+
27
+ def self.open_timeout
28
+ @@open_timeout
29
+ end
30
+
31
+ def self.open_timeout=(timeout)
32
+ @@open_timeout = timeout
33
+ end
34
+
35
+ def self.read_timeout
36
+ @@read_timeout
37
+ end
38
+
39
+ def self.read_timeout=(timeout)
40
+ @@read_timeout = timeout
41
+ end
42
+
43
+ # Xdelivery.configure do |config|
44
+ # config.open_timeout = 5
45
+ # config.read_timeout = 5
46
+ # end
47
+ def self.configure
48
+ yield(self)
49
+ end
50
+
51
+ def self.production?
52
+ @@env.to_s == "production"
53
+ end
54
+
55
+ def self.test?
56
+ @@env.to_s == "test"
22
57
  end
23
58
  end
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
24
  spec.require_paths = ["lib"]
25
25
 
26
- spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "bundler", "~> 2.3.14"
27
27
  spec.add_development_dependency "rake", "~> 10.0"
28
28
  spec.add_development_dependency "minitest", "~> 5.0"
29
29
  spec.add_development_dependency "minitest-stub-const"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xdelivery
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - eddie
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-21 00:00:00.000000000 Z
11
+ date: 2022-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.16'
19
+ version: 2.3.14
20
20
  type: :development
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.16'
26
+ version: 2.3.14
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -110,6 +110,7 @@ files:
110
110
  - lib/xdelivery/api/shops.rb
111
111
  - lib/xdelivery/callback.rb
112
112
  - lib/xdelivery/client.rb
113
+ - lib/xdelivery/exceptions.rb
113
114
  - lib/xdelivery/version.rb
114
115
  - xdelivery-ruby.gemspec
115
116
  homepage: https://github.com/superlanding/xdelivery-ruby
@@ -131,9 +132,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
132
  - !ruby/object:Gem::Version
132
133
  version: '0'
133
134
  requirements: []
134
- rubyforge_project:
135
- rubygems_version: 2.4.8
135
+ rubygems_version: 3.1.6
136
136
  signing_key:
137
137
  specification_version: 4
138
- summary: "火箭快遞 API 串接"
138
+ summary: 火箭快遞 API 串接
139
139
  test_files: []