xdelivery 0.2.4 → 1.0.2

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: d92f2aaad01c0ebc7a226cbb22fc08b52d74c697
4
- data.tar.gz: 070694f59a56765b20971805470f712704b74ded
2
+ SHA256:
3
+ metadata.gz: 355fb7f6c6cbc86ac8471c2b70f05dfefc87353cded96521b0b44bc5c538ee28
4
+ data.tar.gz: c001a8e1e7c561f4782420dca6c5de9c0b8d4db15012d5472751fb735ba83bde
5
5
  SHA512:
6
- metadata.gz: dfa8a207525114da3d26f2268be95bbf4023ab3330cdeb796ec5518e2f4612844f5b76d82b1792531c170d9145f61445f8096165f19c3f0965e29f92c0ee6ee1
7
- data.tar.gz: 0735a33a6509d8f53993287b667b6dd6da1dbb702813c54a3657cb4a760406cc412e3e53db43b89da3c31afba76c5521d9f36c0e096479fe8d92e04fe8157c7f
6
+ metadata.gz: 8070cce10383f1579f90b859fc9471ec1ae371def5a42b225a558783bd83b6ef568a4f9642f0c0460aefe7cd77d4de03b1653cd759b84b00a99e3cc1d2bfb0d4
7
+ data.tar.gz: 7bfe8c7d89ff4e0c20be5af13249e88837c5e405df8475ef7a40fefab4ef11b54a25fc29cc70ec308b695269a20278306bd42feb5da42f5831b16a25b9279aac
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- xdelivery (0.2.3)
4
+ xdelivery (1.0.1)
5
5
  rest-client
6
6
 
7
7
  GEM
@@ -50,4 +50,4 @@ DEPENDENCIES
50
50
  xdelivery!
51
51
 
52
52
  BUNDLED WITH
53
- 1.16.6
53
+ 1.17.3
@@ -16,23 +16,31 @@ module Xdelivery
16
16
  protected
17
17
 
18
18
  def patch(path)
19
- RestClient.patch(uri(path).to_s, patch_data)
19
+ RestClient::Request.execute(method: :patch, url: uri(path).to_s, payload: patch_data, open_timeout: open_timeout, read_timeout: read_timeout)
20
20
  rescue RestClient::ExceptionWithResponse => e
21
21
  e.response
22
22
  end
23
23
 
24
24
  def post(path)
25
- RestClient.post(uri(path).to_s, post_data)
25
+ RestClient::Request.execute(method: :post, url: uri(path).to_s, payload: post_data, open_timeout: open_timeout, read_timeout: read_timeout)
26
26
  rescue RestClient::ExceptionWithResponse => e
27
27
  e.response
28
28
  end
29
29
 
30
30
  def get(path)
31
- RestClient.get(uri(path).to_s)
31
+ RestClient::Request.execute(method: :get, url: uri(path).to_s, open_timeout: open_timeout, read_timeout: read_timeout)
32
32
  rescue RestClient::ExceptionWithResponse => e
33
33
  e.response
34
34
  end
35
35
 
36
+ def open_timeout
37
+ Xdelivery.open_timeout
38
+ end
39
+
40
+ def read_timeout
41
+ Xdelivery.read_timeout
42
+ end
43
+
36
44
  # [GET] query string params
37
45
  def params
38
46
  {}
@@ -8,11 +8,11 @@ module Xdelivery
8
8
  COLUMNS = [
9
9
  :order_id, :provider, :recipient, :mobile, :email, :store_id, :store_name, :address,
10
10
  :items, :warehouse_items, :total_order_amount, :cash_on_delivery, :order_created_at,
11
- :note, :callback_url, :ref_id, :tag, :merchant_note
11
+ :note, :callback_url, :ref_id, :tag, :merchant_note, :shop_id
12
12
  ]
13
13
 
14
14
  INVOICE_COLUMNS = [
15
- :create_type, :email, :company_code, :company_title, :donate_code, :device_id, :device, :items, :invoice_setting_id
15
+ :create_type, :email, :company_code, :company_title, :donate_code, :device_id, :device, :items
16
16
  ]
17
17
 
18
18
  def add(params, invoice_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,20 @@
1
+ module Xdelivery
2
+ module API
3
+ module Response
4
+ class Shops < Base
5
+
6
+ Shop = Struct.new(:id, :title, :pay2go_merchant_id, :default_shop)
7
+
8
+ def status?
9
+ auth?
10
+ end
11
+
12
+ def shops
13
+ data.map do |setting|
14
+ Shop.new(setting['id'], setting['title'], setting['pay2go_merchant_id'], setting['default_shop'])
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -8,11 +8,11 @@ module Xdelivery
8
8
  COLUMNS = [
9
9
  :order_id, :recipient, :mobile, :email, :address,
10
10
  :items, :warehouse_items, :total_order_amount, :cash_on_delivery, :order_created_at,
11
- :note, :callback_url, :ref_id, :tag, :tracking_code, :merchant_note
11
+ :note, :callback_url, :ref_id, :tag, :tracking_code, :merchant_note, :shop_id
12
12
  ]
13
13
 
14
14
  INVOICE_COLUMNS = [
15
- :create_type, :email, :company_code, :company_title, :donate_code, :device_id, :device, :items, :invoice_setting_id
15
+ :create_type, :email, :company_code, :company_title, :donate_code, :device_id, :device, :items
16
16
  ]
17
17
 
18
18
  def add(params, invoice_params={})
@@ -0,0 +1,9 @@
1
+ module Xdelivery
2
+ module API
3
+ class Shops < Base
4
+ def get!
5
+ Response::Shops.new(get('/shops.json'))
6
+ end
7
+ end
8
+ end
9
+ end
@@ -26,8 +26,8 @@ module Xdelivery
26
26
  api.update!
27
27
  end
28
28
 
29
- def get_invoice_settings!
30
- API::InvoiceSettings.new(merchant_no, access_key).get!
29
+ def get_shops!
30
+ API::Shops.new(merchant_no, access_key).get!
31
31
  end
32
32
 
33
33
  def ping!
@@ -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 = "0.2.4"
2
+ VERSION = "1.0.2"
3
3
  end
data/lib/xdelivery.rb CHANGED
@@ -3,20 +3,49 @@ 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"
9
10
  require "xdelivery/api/products"
10
11
  require "xdelivery/api/ping"
11
- require "xdelivery/api/invoice_settings"
12
+ require "xdelivery/api/shops"
12
13
  require "xdelivery/api/response/base"
13
14
  require "xdelivery/api/response/orders"
14
15
  require "xdelivery/api/response/products"
15
16
  require "xdelivery/api/response/ping"
16
- require "xdelivery/api/response/invoice_settings"
17
+ require "xdelivery/api/response/shops"
17
18
 
18
19
 
19
20
  module Xdelivery
21
+
22
+ @@open_timeout = 5
23
+ @@read_timeout = 5
24
+
25
+ def self.open_timeout
26
+ @@open_timeout
27
+ end
28
+
29
+ def self.open_timeout=(timeout)
30
+ @@open_timeout = timeout
31
+ end
32
+
33
+ def self.read_timeout
34
+ @@read_timeout
35
+ end
36
+
37
+ def self.read_timeout=(timeout)
38
+ @@read_timeout = timeout
39
+ end
40
+
41
+ # Xdelivery.configure do |config|
42
+ # config.open_timeout = 5
43
+ # config.read_timeout = 5
44
+ # end
45
+ def self.configure
46
+ yield(self)
47
+ end
48
+
20
49
  def url
21
50
  API::Base::BASE_URL
22
51
  end
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: 0.2.4
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - eddie
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-31 00:00:00.000000000 Z
11
+ date: 2022-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -98,18 +98,19 @@ files:
98
98
  - bin/setup
99
99
  - lib/xdelivery.rb
100
100
  - lib/xdelivery/api/base.rb
101
- - lib/xdelivery/api/invoice_settings.rb
102
101
  - lib/xdelivery/api/orders.rb
103
102
  - lib/xdelivery/api/ping.rb
104
103
  - lib/xdelivery/api/products.rb
105
104
  - lib/xdelivery/api/response/base.rb
106
- - lib/xdelivery/api/response/invoice_settings.rb
107
105
  - lib/xdelivery/api/response/orders.rb
108
106
  - lib/xdelivery/api/response/ping.rb
109
107
  - lib/xdelivery/api/response/products.rb
108
+ - lib/xdelivery/api/response/shops.rb
110
109
  - lib/xdelivery/api/sales.rb
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.0.3.1
136
136
  signing_key:
137
137
  specification_version: 4
138
- summary: "火箭快遞 API 串接"
138
+ summary: 火箭快遞 API 串接
139
139
  test_files: []
@@ -1,9 +0,0 @@
1
- module Xdelivery
2
- module API
3
- class InvoiceSettings < Base
4
- def get!
5
- Response::InvoiceSettings.new(get('/invoice_settings.json'))
6
- end
7
- end
8
- end
9
- end
@@ -1,20 +0,0 @@
1
- module Xdelivery
2
- module API
3
- module Response
4
- class InvoiceSettings < Base
5
-
6
- Setting = Struct.new(:id, :title, :pay2go_merchant_id, :default_setting)
7
-
8
- def status?
9
- auth?
10
- end
11
-
12
- def settings
13
- data.map do |setting|
14
- Setting.new(setting['id'], setting['title'], setting['pay2go_merchant_id'], setting['default_setting'])
15
- end
16
- end
17
- end
18
- end
19
- end
20
- end