xdelivery 1.0.1 → 2.0.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
  SHA256:
3
- metadata.gz: 6ed303bad8a770ac7c39f31b93e5e5901d92faef2c8362304031f453e6630210
4
- data.tar.gz: 204deb46037e44ba3e0a471e5b2f1b3faf8722907a8a4b694226abfbb205e614
3
+ metadata.gz: '09446a0eba629a611cbe3fc4b56f751fcb5cdd34f02c30e88cd7c941cbbf6645'
4
+ data.tar.gz: cf34ac202d5d055b741e8db30fe3cf0b551a1ef1dd8e642e013f5c6075030d1c
5
5
  SHA512:
6
- metadata.gz: 95de1b1114ccae53265de8ebfcc2f3ad2d5c0cd539b6dcff73faf6584c02e6b0681c982ffda7f895af48ed2f8a9ff04dc4561bf8586de7979e5452f38ab24f25
7
- data.tar.gz: 407f8bf9b6072a879730c66eb08d19e8c3a35a7cef93155ac8baf5abb0d8cc30d6e863faa0c129b36409dfc252958d6f27c53ae76774e3a116e7aad7043000b5
6
+ metadata.gz: cad357967a7b44da238f6084ba67cbd1cbc3b1ae21088b7ac7a3d2c2128cb0678b4fc01385a42fcd4d4b0c569b5df07856545c5a25cf610b99fd0c530af3a2b8
7
+ data.tar.gz: 1ca96bc7f43b2a034666430dee8b47c78dacb4bb50eede1d225635645cc1dd42ce2c1e9f35ad4eac567d6fcf5d8724583d50b6b21eee4cc1a2199d7068f7bd11
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- xdelivery (1.0.1)
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.17.3
53
+ 2.3.14
@@ -6,13 +6,22 @@ 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)
@@ -59,7 +68,7 @@ module Xdelivery
59
68
  private
60
69
 
61
70
  def uri(path)
62
- 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 }
63
72
  end
64
73
 
65
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.1"
2
+ VERSION = "2.0.1"
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"
@@ -21,6 +22,16 @@ module Xdelivery
21
22
  @@open_timeout = 5
22
23
  @@read_timeout = 5
23
24
 
25
+ @@env = :production
26
+
27
+ def self.env(env)
28
+ @@env
29
+ end
30
+
31
+ def self.env=(env)
32
+ @@env = env
33
+ end
34
+
24
35
  def self.open_timeout
25
36
  @@open_timeout
26
37
  end
@@ -45,7 +56,11 @@ module Xdelivery
45
56
  yield(self)
46
57
  end
47
58
 
48
- def url
49
- API::Base::BASE_URL
59
+ def self.production?
60
+ @@env.to_s == "production"
61
+ end
62
+
63
+ def self.test?
64
+ @@env.to_s == "test"
50
65
  end
51
66
  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.1
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - eddie
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-22 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,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
132
  - !ruby/object:Gem::Version
132
133
  version: '0'
133
134
  requirements: []
134
- rubygems_version: 3.0.8
135
+ rubygems_version: 3.1.6
135
136
  signing_key:
136
137
  specification_version: 4
137
138
  summary: 火箭快遞 API 串接