friendly_shipping 0.2.1 → 0.2.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
  SHA1:
3
- metadata.gz: ba3ffaa5a9a2dc26efe1c741f3579bb00af900b1
4
- data.tar.gz: d59fb16b95c9851917d508bf08c4c2e5eafac026
3
+ metadata.gz: c51440567e32656dd9c373764b43ed8915c7bed5
4
+ data.tar.gz: ac745c9dd9e1c087ec387c429f761633c15783bc
5
5
  SHA512:
6
- metadata.gz: 8ee545c4c8af459aa943bb97c17b5d58f71f063d7c048e2d72f8f3ece48047c65959d5f7eb94638ac8ed24b5d28012d8ae85a979446d8c8920d73349c5ebaeee
7
- data.tar.gz: 9009c54b6022b8d8b644e0032bb712d7ababcf7b13a5f726a5a5398b8c6e0b5454fd5d633eb5091ed4014277bdf0f1b3c2055547ca5c9ead9b0e1783b65cfcf9
6
+ metadata.gz: d8b986a76ea28d2ba06f7d6ad3bfea61748e0d291d95f59b9d1ea826cd6bc0d94b16b5cefe7fd2d9d03f0cbf32762b6a7fc9145d4d06054b3d0bceff2125b723
7
+ data.tar.gz: faee889795cd2d7588771e9938e1660eec64e3c72e65994dac6e348b4a5aff1eef514d0f1d5ae44a39f0bf2ff47057d1c6a7f68efde8a20ce431b9d50739540f
@@ -0,0 +1,23 @@
1
+ require 'json'
2
+
3
+ module FriendlyShipping
4
+ class BadRequest < StandardError
5
+ attr_reader :rest_error, :response
6
+
7
+ def initialize(rest_error)
8
+ @rest_error = rest_error
9
+ @response = rest_error.response
10
+ super parse_json_errors || rest_error
11
+ end
12
+
13
+ private
14
+
15
+ def parse_json_errors
16
+ parsed_body = JSON.parse(response.body)
17
+ messages = parsed_body.fetch('errors')&.map { |e| e.fetch('message') }
18
+ messages&.join(', ')
19
+ rescue JSON::ParserError, KeyError => _error
20
+ nil
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,53 @@
1
+ require 'dry/monads/result'
2
+ require 'friendly_shipping/bad_request'
3
+ require 'rest-client'
4
+
5
+ module FriendlyShipping
6
+ class RestClient
7
+ extend Dry::Monads::Result::Mixin
8
+ class <<self
9
+ def get(path, headers)
10
+ Success(
11
+ ::RestClient.get(
12
+ path,
13
+ headers
14
+ )
15
+ )
16
+ rescue ::RestClient::Exception => error
17
+ Failure(error)
18
+ end
19
+
20
+ def post(path, payload, headers)
21
+ Success(
22
+ ::RestClient.post(
23
+ path,
24
+ payload,
25
+ headers
26
+ )
27
+ )
28
+ rescue ::RestClient::Exception => error
29
+ if error.http_code == 400
30
+ Failure(BadRequest.new(error))
31
+ else
32
+ Failure(error)
33
+ end
34
+ end
35
+
36
+ def put(path, payload, headers)
37
+ Success(
38
+ ::RestClient.put(
39
+ path,
40
+ payload,
41
+ headers
42
+ )
43
+ )
44
+ rescue ::RestClient::Exception => error
45
+ if error.http_code == 400
46
+ Failure(BadRequest.new(error))
47
+ else
48
+ Failure(error)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -1,15 +1,14 @@
1
+
1
2
  require 'dry/monads/result'
2
- require 'rest-client'
3
+ require 'friendly_shipping/rest_client'
3
4
  require 'friendly_shipping/services/ship_engine/parse_carrier_response'
4
5
  require 'friendly_shipping/services/ship_engine/serialize_label_shipment'
5
6
  require 'friendly_shipping/services/ship_engine/parse_label_response'
6
- require 'friendly_shipping/services/ship_engine/bad_request'
7
+ require 'friendly_shipping/services/ship_engine/parse_void_response'
7
8
 
8
9
  module FriendlyShipping
9
10
  module Services
10
11
  class ShipEngine
11
- include Dry::Monads::Result::Mixin
12
-
13
12
  API_BASE = "https://api.shipengine.com/v1/"
14
13
  API_PATHS = {
15
14
  carriers: "carriers",
@@ -22,57 +21,37 @@ module FriendlyShipping
22
21
  end
23
22
 
24
23
  def carriers
25
- path = API_PATHS[:carriers]
26
- get(path).fmap do |response|
24
+ path = API_BASE + API_PATHS[:carriers]
25
+ FriendlyShipping::RestClient.get(path, request_headers).fmap do |response|
27
26
  ParseCarrierResponse.new(response: response).call
28
27
  end
29
28
  end
30
29
 
31
30
  def labels(shipment)
32
31
  payload = SerializeLabelShipment.new(shipment: shipment).call.merge(test_label: test).to_json
33
- path = API_PATHS[:labels]
34
- post(path, payload).fmap do |response|
32
+ path = API_BASE + API_PATHS[:labels]
33
+ FriendlyShipping::RestClient.post(path, payload, request_headers).fmap do |response|
35
34
  ParseLabelResponse.new(response: response).call
36
35
  end
37
36
  end
38
37
 
39
- private
40
-
41
- def get(path)
42
- Success(
43
- RestClient.get(
44
- API_BASE + path,
45
- request_headers
46
- )
47
- )
48
- rescue RestClient::ExceptionWithResponse => error
49
- Failure(error)
50
- end
51
-
52
- def post(path, payload)
53
- Success(
54
- RestClient.post(
55
- API_BASE + path,
56
- payload,
57
- request_headers
58
- )
59
- )
60
- rescue RestClient::ExceptionWithResponse => error
61
- if error.to_s == '400 Bad Request'
62
- Failure(BadRequest.new(error))
63
- else
64
- Failure(error)
38
+ def void(label)
39
+ path = "#{API_BASE}labels/#{label.id}/void"
40
+ FriendlyShipping::RestClient.put(path, '', request_headers).bind do |response|
41
+ ParseVoidResponse.new(response: response).call
65
42
  end
66
43
  end
67
44
 
45
+ private
46
+
47
+ attr_reader :token, :test
48
+
68
49
  def request_headers
69
50
  {
70
51
  content_type: :json,
71
52
  "api-key": token
72
53
  }
73
54
  end
74
-
75
- attr_reader :token, :test
76
55
  end
77
56
  end
78
57
  end
@@ -0,0 +1,23 @@
1
+ require 'friendly_shipping/bad_request'
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngine
6
+ class ParseVoidResponse
7
+ include Dry::Monads::Result::Mixin
8
+
9
+ attr_reader :response
10
+
11
+ def initialize(response:)
12
+ @response = response
13
+ end
14
+
15
+ def call
16
+ parsed_json = JSON.parse(response.body)
17
+ approved, message = parsed_json["approved"], parsed_json["message"]
18
+ approved ? Success(message) : Failure(message)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module FriendlyShipping
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: friendly_shipping
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Meyerhoff
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-21 00:00:00.000000000 Z
11
+ date: 2019-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: physical
@@ -128,12 +128,14 @@ files:
128
128
  - bin/setup
129
129
  - friendly_shipping.gemspec
130
130
  - lib/friendly_shipping.rb
131
+ - lib/friendly_shipping/bad_request.rb
131
132
  - lib/friendly_shipping/carrier.rb
132
133
  - lib/friendly_shipping/label.rb
134
+ - lib/friendly_shipping/rest_client.rb
133
135
  - lib/friendly_shipping/services/ship_engine.rb
134
- - lib/friendly_shipping/services/ship_engine/bad_request.rb
135
136
  - lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
136
137
  - lib/friendly_shipping/services/ship_engine/parse_label_response.rb
138
+ - lib/friendly_shipping/services/ship_engine/parse_void_response.rb
137
139
  - lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb
138
140
  - lib/friendly_shipping/shipping_method.rb
139
141
  - lib/friendly_shipping/version.rb
@@ -1,27 +0,0 @@
1
- require 'json'
2
-
3
- module FriendlyShipping
4
- module Services
5
- class ShipEngine
6
- class BadRequest < StandardError
7
- attr_reader :rest_error, :response
8
-
9
- def initialize(rest_error)
10
- @rest_error = rest_error
11
- @response = rest_error.response
12
- super parse_json_errors || rest_error
13
- end
14
-
15
- private
16
-
17
- def parse_json_errors
18
- parsed_body = JSON.parse(response.body)
19
- messages = parsed_body.fetch('errors')&.map { |e| e.fetch('message') }
20
- messages&.join(', ')
21
- rescue JSON::ParserError, KeyError => _error
22
- nil
23
- end
24
- end
25
- end
26
- end
27
- end