xdelivery 1.0.1 → 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
2
  SHA256:
3
- metadata.gz: 6ed303bad8a770ac7c39f31b93e5e5901d92faef2c8362304031f453e6630210
4
- data.tar.gz: 204deb46037e44ba3e0a471e5b2f1b3faf8722907a8a4b694226abfbb205e614
3
+ metadata.gz: 355fb7f6c6cbc86ac8471c2b70f05dfefc87353cded96521b0b44bc5c538ee28
4
+ data.tar.gz: c001a8e1e7c561f4782420dca6c5de9c0b8d4db15012d5472751fb735ba83bde
5
5
  SHA512:
6
- metadata.gz: 95de1b1114ccae53265de8ebfcc2f3ad2d5c0cd539b6dcff73faf6584c02e6b0681c982ffda7f895af48ed2f8a9ff04dc4561bf8586de7979e5452f38ab24f25
7
- data.tar.gz: 407f8bf9b6072a879730c66eb08d19e8c3a35a7cef93155ac8baf5abb0d8cc30d6e863faa0c129b36409dfc252958d6f27c53ae76774e3a116e7aad7043000b5
6
+ metadata.gz: 8070cce10383f1579f90b859fc9471ec1ae371def5a42b225a558783bd83b6ef568a4f9642f0c0460aefe7cd77d4de03b1653cd759b84b00a99e3cc1d2bfb0d4
7
+ data.tar.gz: 7bfe8c7d89ff4e0c20be5af13249e88837c5e405df8475ef7a40fefab4ef11b54a25fc29cc70ec308b695269a20278306bd42feb5da42f5831b16a25b9279aac
@@ -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 = "1.0.2"
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"
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: 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: 2020-04-22 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
@@ -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.0.3.1
135
136
  signing_key:
136
137
  specification_version: 4
137
138
  summary: 火箭快遞 API 串接