friendly_shipping 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +1 -1
- data/.env.template +7 -0
- data/.env.test +4 -0
- data/.env.test.local.template +6 -0
- data/.gitignore +1 -0
- data/.rubocop-relaxed.yml +7 -23
- data/.rubocop.yml +11 -2
- data/.rubocop_todo.yml +21 -0
- data/.yardopts +6 -0
- data/CHANGELOG.md +82 -0
- data/Gemfile +17 -0
- data/friendly_shipping.gemspec +4 -13
- data/lib/friendly_shipping/api_error.rb +6 -4
- data/lib/friendly_shipping/api_error_handler.rb +10 -6
- data/lib/friendly_shipping/api_failure.rb +4 -0
- data/lib/friendly_shipping/api_result.rb +16 -4
- data/lib/friendly_shipping/carrier.rb +28 -8
- data/lib/friendly_shipping/http_client.rb +25 -2
- data/lib/friendly_shipping/inflections.rb +10 -0
- data/lib/friendly_shipping/item_options.rb +3 -0
- data/lib/friendly_shipping/label.rb +41 -20
- data/lib/friendly_shipping/package_options.rb +21 -2
- data/lib/friendly_shipping/rate.rb +50 -15
- data/lib/friendly_shipping/request.rb +23 -7
- data/lib/friendly_shipping/response.rb +21 -6
- data/lib/friendly_shipping/services/rl/api_error.rb +33 -0
- data/lib/friendly_shipping/services/rl/bol_options.rb +107 -0
- data/lib/friendly_shipping/services/rl/bol_packages_serializer.rb +32 -0
- data/lib/friendly_shipping/services/rl/bol_structures_serializer.rb +31 -0
- data/lib/friendly_shipping/services/rl/item_options.rb +38 -0
- data/lib/friendly_shipping/services/rl/package_options.rb +40 -0
- data/lib/friendly_shipping/services/rl/parse_create_bol_response.rb +46 -0
- data/lib/friendly_shipping/services/rl/parse_invoice_response.rb +50 -0
- data/lib/friendly_shipping/services/rl/parse_print_bol_response.rb +47 -0
- data/lib/friendly_shipping/services/rl/parse_print_shipping_labels_response.rb +47 -0
- data/lib/friendly_shipping/services/rl/parse_rate_quote_response.rb +66 -0
- data/lib/friendly_shipping/services/rl/parse_transit_times_response.rb +76 -0
- data/lib/friendly_shipping/services/rl/rate_quote_options.rb +86 -0
- data/lib/friendly_shipping/services/rl/rate_quote_packages_serializer.rb +54 -0
- data/lib/friendly_shipping/services/rl/rate_quote_structures_serializer.rb +53 -0
- data/lib/friendly_shipping/services/rl/serialize_create_bol_request.rb +86 -0
- data/lib/friendly_shipping/services/rl/serialize_location.rb +46 -0
- data/lib/friendly_shipping/services/rl/serialize_rate_quote_request.rb +69 -0
- data/lib/friendly_shipping/services/rl/serialize_transit_times_request.rb +38 -0
- data/lib/friendly_shipping/services/rl/shipment_document.rb +40 -0
- data/lib/friendly_shipping/services/rl/shipment_information.rb +41 -0
- data/lib/friendly_shipping/services/rl/shipment_options.rb +50 -0
- data/lib/friendly_shipping/services/rl/shipping_methods.rb +28 -0
- data/lib/friendly_shipping/services/rl/structure_options.rb +13 -0
- data/lib/friendly_shipping/services/rl.rb +204 -0
- data/lib/friendly_shipping/services/ship_engine/api_error.rb +33 -0
- data/lib/friendly_shipping/services/ship_engine/customs_items_serializer.rb +36 -0
- data/lib/friendly_shipping/services/ship_engine/label_customs_options.rb +10 -7
- data/lib/friendly_shipping/services/ship_engine/label_item_options.rb +10 -5
- data/lib/friendly_shipping/services/ship_engine/label_options.rb +31 -14
- data/lib/friendly_shipping/services/ship_engine/label_package_options.rb +18 -11
- data/lib/friendly_shipping/services/ship_engine/parse_address_validation_response.rb +77 -0
- data/lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb +9 -0
- data/lib/friendly_shipping/services/ship_engine/parse_label_response.rb +4 -0
- data/lib/friendly_shipping/services/ship_engine/{parse_rate_estimate_response.rb → parse_rate_estimates_response.rb} +26 -6
- data/lib/friendly_shipping/services/ship_engine/parse_rates_response.rb +101 -0
- data/lib/friendly_shipping/services/ship_engine/parse_void_response.rb +4 -0
- data/lib/friendly_shipping/services/ship_engine/rate_estimates_options.rb +17 -4
- data/lib/friendly_shipping/services/ship_engine/rates_item_options.rb +28 -0
- data/lib/friendly_shipping/services/ship_engine/rates_options.rb +61 -0
- data/lib/friendly_shipping/services/ship_engine/rates_package_options.rb +20 -0
- data/lib/friendly_shipping/services/ship_engine/serialize_address_residential_indicator.rb +27 -0
- data/lib/friendly_shipping/services/ship_engine/serialize_address_validation_request.rb +31 -0
- data/lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb +22 -27
- data/lib/friendly_shipping/services/ship_engine/serialize_rate_estimate_request.rb +41 -16
- data/lib/friendly_shipping/services/ship_engine/serialize_rates_request.rb +126 -0
- data/lib/friendly_shipping/services/ship_engine.rb +94 -21
- data/lib/friendly_shipping/services/ship_engine_ltl/api_error.rb +12 -0
- data/lib/friendly_shipping/services/ship_engine_ltl/item_options.rb +50 -0
- data/lib/friendly_shipping/services/ship_engine_ltl/package_options.rb +50 -0
- data/lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb +53 -0
- data/lib/friendly_shipping/services/ship_engine_ltl/parse_quote_response.rb +82 -0
- data/lib/friendly_shipping/services/ship_engine_ltl/quote_options.rb +52 -0
- data/lib/friendly_shipping/services/ship_engine_ltl/serialize_packages.rb +46 -0
- data/lib/friendly_shipping/services/ship_engine_ltl/serialize_quote_request.rb +143 -0
- data/lib/friendly_shipping/services/ship_engine_ltl/serialize_structures.rb +42 -0
- data/lib/friendly_shipping/services/ship_engine_ltl/shipment_options.rb +47 -0
- data/lib/friendly_shipping/services/ship_engine_ltl/structure_options.rb +17 -0
- data/lib/friendly_shipping/services/ship_engine_ltl.rb +153 -0
- data/lib/friendly_shipping/services/tforce_freight/access_token.rb +43 -0
- data/lib/friendly_shipping/services/tforce_freight/api_error.rb +42 -0
- data/lib/friendly_shipping/services/tforce_freight/bol_options.rb +180 -0
- data/lib/friendly_shipping/services/tforce_freight/document_options.rb +100 -0
- data/lib/friendly_shipping/services/tforce_freight/generate_commodity_information.rb +92 -0
- data/lib/friendly_shipping/services/tforce_freight/generate_create_bol_request_hash.rb +165 -0
- data/lib/friendly_shipping/services/tforce_freight/generate_document_options_hash.rb +36 -0
- data/lib/friendly_shipping/services/tforce_freight/generate_handling_units_hash.rb +51 -0
- data/lib/friendly_shipping/services/tforce_freight/generate_location_hash.rb +25 -0
- data/lib/friendly_shipping/services/tforce_freight/generate_pickup_request_hash.rb +113 -0
- data/lib/friendly_shipping/services/tforce_freight/generate_rates_request_hash.rb +65 -0
- data/lib/friendly_shipping/services/tforce_freight/generate_reference_hash.rb +28 -0
- data/lib/friendly_shipping/services/tforce_freight/item_options.rb +93 -0
- data/lib/friendly_shipping/services/tforce_freight/package_options.rb +121 -0
- data/lib/friendly_shipping/services/tforce_freight/parse_create_bol_response.rb +94 -0
- data/lib/friendly_shipping/services/tforce_freight/parse_pickup_response.rb +45 -0
- data/lib/friendly_shipping/services/tforce_freight/parse_rates_response.rb +58 -0
- data/lib/friendly_shipping/services/tforce_freight/parse_shipment_document.rb +31 -0
- data/lib/friendly_shipping/services/tforce_freight/pickup_options.rb +82 -0
- data/lib/friendly_shipping/services/tforce_freight/rates_item_options.rb +12 -0
- data/lib/friendly_shipping/services/tforce_freight/rates_options.rb +162 -0
- data/lib/friendly_shipping/services/tforce_freight/rates_package_options.rb +12 -0
- data/lib/friendly_shipping/services/tforce_freight/shipment_document.rb +38 -0
- data/lib/friendly_shipping/services/tforce_freight/shipment_information.rb +104 -0
- data/lib/friendly_shipping/services/tforce_freight/shipment_options.rb +49 -0
- data/lib/friendly_shipping/services/tforce_freight/shipping_methods.rb +25 -0
- data/lib/friendly_shipping/services/tforce_freight/structure_options.rb +44 -0
- data/lib/friendly_shipping/services/tforce_freight.rb +202 -0
- data/lib/friendly_shipping/services/ups/label_options.rb +14 -2
- data/lib/friendly_shipping/services/ups/label_package_options.rb +8 -4
- data/lib/friendly_shipping/services/ups/parse_modifier_element.rb +29 -0
- data/lib/friendly_shipping/services/ups/parse_rate_response.rb +12 -0
- data/lib/friendly_shipping/services/ups/parse_time_in_transit_response.rb +1 -1
- data/lib/friendly_shipping/services/ups/rate_estimate_options.rb +14 -1
- data/lib/friendly_shipping/services/ups/serialize_package_node.rb +11 -1
- data/lib/friendly_shipping/services/ups/serialize_rating_service_selection_request.rb +10 -1
- data/lib/friendly_shipping/services/ups/serialize_shipment_accept_request.rb +1 -1
- data/lib/friendly_shipping/services/ups/serialize_shipment_confirm_request.rb +8 -4
- data/lib/friendly_shipping/services/ups/shipping_methods.rb +1 -1
- data/lib/friendly_shipping/services/ups.rb +1 -1
- data/lib/friendly_shipping/services/ups_freight/api_error.rb +8 -5
- data/lib/friendly_shipping/services/ups_freight/generate_commodity_information.rb +65 -19
- data/lib/friendly_shipping/services/ups_freight/generate_freight_rate_request_hash.rb +3 -20
- data/lib/friendly_shipping/services/ups_freight/generate_freight_ship_request_hash.rb +2 -20
- data/lib/friendly_shipping/services/ups_freight/generate_handling_units_hash.rb +54 -0
- data/lib/friendly_shipping/services/ups_freight/generate_location_hash.rb +10 -6
- data/lib/friendly_shipping/services/ups_freight/label_options.rb +36 -10
- data/lib/friendly_shipping/services/ups_freight/label_structure_options.rb +13 -0
- data/lib/friendly_shipping/services/ups_freight/parse_freight_label_response.rb +2 -2
- data/lib/friendly_shipping/services/ups_freight/parse_shipment_document.rb +1 -1
- data/lib/friendly_shipping/services/ups_freight/rates_item_options.rb +18 -9
- data/lib/friendly_shipping/services/ups_freight/rates_options.rb +31 -21
- data/lib/friendly_shipping/services/ups_freight/rates_package_options.rb +79 -8
- data/lib/friendly_shipping/services/ups_freight/rates_structure_options.rb +46 -0
- data/lib/friendly_shipping/services/ups_freight/shipment_information.rb +7 -3
- data/lib/friendly_shipping/services/ups_freight/shipment_options.rb +49 -0
- data/lib/friendly_shipping/services/ups_freight.rb +3 -1
- data/lib/friendly_shipping/services/ups_json/access_token.rb +29 -0
- data/lib/friendly_shipping/services/ups_json/api_error.rb +29 -0
- data/lib/friendly_shipping/services/ups_json/generate_address_classification_payload.rb +29 -0
- data/lib/friendly_shipping/services/ups_json/generate_address_hash.rb +30 -0
- data/lib/friendly_shipping/services/ups_json/generate_labels_payload.rb +211 -0
- data/lib/friendly_shipping/services/ups_json/generate_package_hash.rb +76 -0
- data/lib/friendly_shipping/services/ups_json/generate_rates_payload.rb +86 -0
- data/lib/friendly_shipping/services/ups_json/generate_timings_payload.rb +44 -0
- data/lib/friendly_shipping/services/ups_json/label.rb +20 -0
- data/lib/friendly_shipping/services/ups_json/label_billing_options.rb +41 -0
- data/lib/friendly_shipping/services/ups_json/label_item_options.rb +77 -0
- data/lib/friendly_shipping/services/ups_json/label_options.rb +177 -0
- data/lib/friendly_shipping/services/ups_json/label_package_options.rb +51 -0
- data/lib/friendly_shipping/services/ups_json/parse_address_classification_response.rb +31 -0
- data/lib/friendly_shipping/services/ups_json/parse_json_response.rb +44 -0
- data/lib/friendly_shipping/services/ups_json/parse_labels_response.rb +71 -0
- data/lib/friendly_shipping/services/ups_json/parse_money_hash.rb +128 -0
- data/lib/friendly_shipping/services/ups_json/parse_rate_modifier_hash.rb +28 -0
- data/lib/friendly_shipping/services/ups_json/parse_rates_response.rb +105 -0
- data/lib/friendly_shipping/services/ups_json/parse_timings_response.rb +56 -0
- data/lib/friendly_shipping/services/ups_json/parse_void_response.rb +32 -0
- data/lib/friendly_shipping/services/ups_json/rates_item_options.rb +22 -0
- data/lib/friendly_shipping/services/ups_json/rates_options.rb +113 -0
- data/lib/friendly_shipping/services/ups_json/rates_package_options.rb +17 -0
- data/lib/friendly_shipping/services/ups_json/shipping_methods.rb +111 -0
- data/lib/friendly_shipping/services/ups_json/timings_options.rb +33 -0
- data/lib/friendly_shipping/services/ups_json.rb +216 -0
- data/lib/friendly_shipping/services/usps/choose_package_rate.rb +3 -3
- data/lib/friendly_shipping/services/usps/machinable_package.rb +1 -1
- data/lib/friendly_shipping/services/usps/parse_package_rate.rb +8 -7
- data/lib/friendly_shipping/services/usps/parse_time_in_transit_response.rb +12 -8
- data/lib/friendly_shipping/services/usps/rate_estimate_options.rb +1 -1
- data/lib/friendly_shipping/services/usps/rate_estimate_package_options.rb +6 -1
- data/lib/friendly_shipping/services/usps/serialize_rate_request.rb +2 -2
- data/lib/friendly_shipping/services/usps/shipping_methods.rb +8 -5
- data/lib/friendly_shipping/services/usps.rb +1 -1
- data/lib/friendly_shipping/services/usps_international/parse_package_rate.rb +5 -4
- data/lib/friendly_shipping/services/usps_international/rate_estimate_options.rb +1 -1
- data/lib/friendly_shipping/services/usps_international/serialize_rate_request.rb +3 -3
- data/lib/friendly_shipping/services/usps_international.rb +1 -1
- data/lib/friendly_shipping/services/usps_ship/access_token.rb +37 -0
- data/lib/friendly_shipping/services/usps_ship/api_error.rb +29 -0
- data/lib/friendly_shipping/services/usps_ship/machinable_package.rb +53 -0
- data/lib/friendly_shipping/services/usps_ship/parse_rate_estimates_response.rb +80 -0
- data/lib/friendly_shipping/services/usps_ship/parse_timings_response.rb +80 -0
- data/lib/friendly_shipping/services/usps_ship/rate_estimate_options.rb +45 -0
- data/lib/friendly_shipping/services/usps_ship/rate_estimate_package_options.rb +124 -0
- data/lib/friendly_shipping/services/usps_ship/serialize_rate_estimates_request.rb +55 -0
- data/lib/friendly_shipping/services/usps_ship/shipping_methods.rb +38 -0
- data/lib/friendly_shipping/services/usps_ship/timing_options.rb +9 -0
- data/lib/friendly_shipping/services/usps_ship.rb +199 -0
- data/lib/friendly_shipping/shipment_options.rb +13 -1
- data/lib/friendly_shipping/shipping_method.rb +38 -11
- data/lib/friendly_shipping/structure_options.rb +38 -0
- data/lib/friendly_shipping/timing.rb +42 -7
- data/lib/friendly_shipping/version.rb +1 -1
- data/lib/friendly_shipping.rb +7 -0
- metadata +149 -172
- data/lib/friendly_shipping/services/ship_engine/bad_request.rb +0 -29
- data/lib/friendly_shipping/services/ship_engine/bad_request_handler.rb +0 -33
@@ -1,28 +1,54 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module FriendlyShipping
|
4
|
+
# Base class for a shipping rate returned by a carrier API.
|
4
5
|
class Rate
|
6
|
+
# Indicates a shipping rate has no amounts.
|
5
7
|
class NoAmountsGiven < StandardError; end
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
# @
|
17
|
-
|
18
|
-
|
19
|
-
# @
|
20
|
-
|
8
|
+
|
9
|
+
# @return [ShippingMethod] the rate's shipping method
|
10
|
+
attr_reader :shipping_method
|
11
|
+
|
12
|
+
# @return [Hash{String,Symbol=>Money}] the amounts that make up the rate
|
13
|
+
attr_reader :amounts
|
14
|
+
|
15
|
+
# @return [Integer] the remote service ID for the rate
|
16
|
+
attr_reader :remote_service_id
|
17
|
+
|
18
|
+
# @return [Time] the pickup date for the rate
|
19
|
+
attr_reader :pickup_date
|
20
|
+
|
21
|
+
# @return [Time] the delivery date for the rate
|
22
|
+
attr_reader :delivery_date
|
23
|
+
|
24
|
+
# @return [Boolean] whether the delivery date is guaranteed
|
25
|
+
attr_reader :guaranteed
|
26
|
+
|
27
|
+
# @return [Array] any warnings that were generated while getting this rate
|
28
|
+
attr_reader :warnings
|
29
|
+
|
30
|
+
# @return [Array] any errors that were generated while getting this rate
|
31
|
+
attr_reader :errors
|
32
|
+
|
33
|
+
# @return [Hash] additional data related to the rate
|
34
|
+
attr_reader :data
|
35
|
+
|
36
|
+
# @param shipping_method [ShippingMethod] the rate's shipping method
|
37
|
+
# @param amounts [Hash{String,Symbol=>Money}] the amounts that make up the rate
|
38
|
+
# @param remote_service_id [Integer] the remote service ID for the rate
|
39
|
+
# @param pickup_date [Time] the pickup date for the rate
|
40
|
+
# @param delivery_date [Time] the delivery date for the rate
|
41
|
+
# @param guaranteed [Boolean] whether the delivery date is guaranteed
|
42
|
+
# @param warnings [Array] any warnings that were generated while getting this rate
|
43
|
+
# @param errors [Array] any errors that were generated while getting this rate
|
44
|
+
# @param data [Hash] additional data related to the rate
|
21
45
|
def initialize(
|
22
46
|
shipping_method:,
|
23
47
|
amounts:,
|
24
48
|
remote_service_id: nil,
|
49
|
+
pickup_date: nil,
|
25
50
|
delivery_date: nil,
|
51
|
+
guaranteed: false,
|
26
52
|
warnings: [],
|
27
53
|
errors: [],
|
28
54
|
data: {}
|
@@ -30,12 +56,21 @@ module FriendlyShipping
|
|
30
56
|
@remote_service_id = remote_service_id
|
31
57
|
@shipping_method = shipping_method
|
32
58
|
@amounts = amounts
|
59
|
+
@pickup_date = pickup_date
|
33
60
|
@delivery_date = delivery_date
|
61
|
+
@guaranteed = guaranteed
|
34
62
|
@warnings = warnings
|
35
63
|
@errors = errors
|
36
64
|
@data = data
|
37
65
|
end
|
38
66
|
|
67
|
+
# Make this class interchangeable with FriendlyShipping::Timing
|
68
|
+
alias_method :pickup, :pickup_date
|
69
|
+
alias_method :delivery, :delivery_date
|
70
|
+
|
71
|
+
# Sums all the amounts in the rate, returning the total amount.
|
72
|
+
# @return [Money]
|
73
|
+
# @raise [NoAmountsGiven] if the rate has no amounts
|
39
74
|
def total_amount
|
40
75
|
raise NoAmountsGiven if amounts.empty?
|
41
76
|
|
@@ -1,15 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module FriendlyShipping
|
4
|
+
# Represents an HTTP request sent to a carrier API.
|
4
5
|
class Request
|
5
|
-
|
6
|
+
# @return [String] the HTTP request URL
|
7
|
+
attr_reader :url
|
6
8
|
|
7
|
-
# @
|
8
|
-
|
9
|
-
|
10
|
-
# @
|
11
|
-
|
12
|
-
|
9
|
+
# @return [String] the HTTP request method
|
10
|
+
attr_reader :http_method
|
11
|
+
|
12
|
+
# @return [String] the HTTP request body
|
13
|
+
attr_reader :body
|
14
|
+
|
15
|
+
# @return [String] human-readable HTTP request body
|
16
|
+
attr_reader :headers
|
17
|
+
|
18
|
+
# @return [Boolean] whether to attach debugging information to the response
|
19
|
+
attr_reader :debug
|
20
|
+
|
21
|
+
# @param url [String] the HTTP request URL
|
22
|
+
# @param http_method [String] the HTTP request method
|
23
|
+
# @param body [String] the HTTP request body
|
24
|
+
# @param readable_body [String] human-readable HTTP request body
|
25
|
+
# @param headers [Hash] the HTTP request headers
|
26
|
+
# @param debug [Boolean] whether to attach debugging information to the response
|
13
27
|
def initialize(url:, http_method: nil, body: nil, readable_body: nil, headers: {}, debug: false)
|
14
28
|
@url = url
|
15
29
|
@http_method = http_method
|
@@ -19,6 +33,8 @@ module FriendlyShipping
|
|
19
33
|
@debug = debug
|
20
34
|
end
|
21
35
|
|
36
|
+
# Returns the HTTP request body.
|
37
|
+
# @return [String] human-readable HTTP request body
|
22
38
|
def readable_body
|
23
39
|
@readable_body.presence || @body
|
24
40
|
end
|
@@ -1,12 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module FriendlyShipping
|
4
|
+
# Represents an HTTP response received from a carrier API.
|
4
5
|
class Response
|
5
|
-
|
6
|
+
# @return [Integer] the HTTP response status code
|
7
|
+
attr_reader :status
|
6
8
|
|
7
|
-
# @
|
8
|
-
|
9
|
-
|
9
|
+
# @return [String] the HTTP response body
|
10
|
+
attr_reader :body
|
11
|
+
|
12
|
+
# @return [Hash] the HTTP response headers
|
13
|
+
attr_reader :headers
|
14
|
+
|
15
|
+
# @param status [Integer] the HTTP response status code
|
16
|
+
# @param body [String] the HTTP response body
|
17
|
+
# @param headers [Hash] the HTTP response headers
|
10
18
|
def initialize(status:, body:, headers:)
|
11
19
|
@status = status
|
12
20
|
@body = body
|
@@ -15,13 +23,16 @@ module FriendlyShipping
|
|
15
23
|
|
16
24
|
alias_method :code, :status
|
17
25
|
|
18
|
-
#
|
19
|
-
# @
|
26
|
+
# Constructs a new {Response} from a `RestClient::Response` object.
|
27
|
+
# @param response [RestClient::Response] the response to use
|
28
|
+
# @return [Response]
|
20
29
|
def self.new_from_rest_client_response(response)
|
21
30
|
new(status: response&.code, body: response&.body, headers: response&.headers)
|
22
31
|
end
|
23
32
|
|
33
|
+
# Returns true if the given object shares the same class and attributes with this response.
|
24
34
|
# @param [Object] other
|
35
|
+
# @return [Boolean]
|
25
36
|
def ==(other)
|
26
37
|
other.class == self.class &&
|
27
38
|
other.attributes == attributes
|
@@ -29,12 +40,16 @@ module FriendlyShipping
|
|
29
40
|
|
30
41
|
alias_method :eql?, :==
|
31
42
|
|
43
|
+
# Returns this response's attributes as a hash.
|
44
|
+
# @return [Hash]
|
32
45
|
def hash
|
33
46
|
attributes.hash
|
34
47
|
end
|
35
48
|
|
36
49
|
protected
|
37
50
|
|
51
|
+
# Returns the status, body, and headers from this response.
|
52
|
+
# @return [Array]
|
38
53
|
def attributes
|
39
54
|
[status, body, headers]
|
40
55
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'friendly_shipping/api_error'
|
4
|
+
|
5
|
+
module FriendlyShipping
|
6
|
+
module Services
|
7
|
+
class RL
|
8
|
+
# Raised when an R+L API error is returned.
|
9
|
+
class ApiError < FriendlyShipping::ApiError
|
10
|
+
# @param cause [RestClient::Exception] the cause of the error
|
11
|
+
def initialize(cause)
|
12
|
+
super(cause, parse_message(cause))
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
# Parses the message from the error response.
|
18
|
+
#
|
19
|
+
# @param error [RestClient::Exception] the cause of the error
|
20
|
+
# @return [String, nil] the message or nil if it couldn't be parsed
|
21
|
+
def parse_message(error)
|
22
|
+
return error.message unless error.response && error.http_code == 400
|
23
|
+
|
24
|
+
parsed_body = JSON.parse(error.response.body)
|
25
|
+
messages = parsed_body.fetch('Errors')&.map { |e| e.fetch('ErrorMessage') }
|
26
|
+
messages&.join(', ')
|
27
|
+
rescue JSON::ParserError, KeyError => _e
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'friendly_shipping/services/rl/shipment_options'
|
4
|
+
require 'friendly_shipping/services/rl/bol_structures_serializer'
|
5
|
+
require 'friendly_shipping/services/rl/bol_packages_serializer'
|
6
|
+
|
7
|
+
module FriendlyShipping
|
8
|
+
module Services
|
9
|
+
class RL
|
10
|
+
# Bill of Lading (BOL) options class. Used when serializing R+L API requests.
|
11
|
+
class BOLOptions < ShipmentOptions
|
12
|
+
# @return [Range] the pickup time window
|
13
|
+
attr_reader :pickup_time_window
|
14
|
+
|
15
|
+
# @return [String] the pickup instructions
|
16
|
+
attr_reader :pickup_instructions
|
17
|
+
|
18
|
+
# @return [Numeric] the declared value of this shipment
|
19
|
+
attr_reader :declared_value
|
20
|
+
|
21
|
+
# @return [String] any special instructions
|
22
|
+
attr_reader :special_instructions
|
23
|
+
|
24
|
+
# @return [Hash] reference numbers for the shipment
|
25
|
+
attr_reader :reference_numbers
|
26
|
+
|
27
|
+
# @return [Array<String>] additional service codes
|
28
|
+
attr_reader :additional_service_codes
|
29
|
+
|
30
|
+
# @return [Boolean] whether to generate universal PRO number
|
31
|
+
attr_reader :generate_universal_pro
|
32
|
+
|
33
|
+
# @return [Callable] the structures serializer
|
34
|
+
attr_reader :structures_serializer
|
35
|
+
|
36
|
+
# @return [Callable]
|
37
|
+
# @deprecated Use {#structures_serializer} instead.
|
38
|
+
attr_reader :packages_serializer
|
39
|
+
|
40
|
+
# @param pickup_time_window [Range]
|
41
|
+
# @param pickup_instructions [String]
|
42
|
+
# @param declared_value [Numeric]
|
43
|
+
# @param special_instructions [String]
|
44
|
+
# @param reference_numbers [Hash]
|
45
|
+
# @param additional_service_codes [Array<String>]
|
46
|
+
# @param generate_universal_pro [Boolean]
|
47
|
+
# @param structures_serializer [Callable] a callable that takes structures
|
48
|
+
# and an options object to create an Array of item hashes per the R+L Carriers docs
|
49
|
+
# @param packages_serializer [Callable] a callable that takes packages
|
50
|
+
# and an options object to create an Array of item hashes per the R+L Carriers docs (DEPRECATED: use `structures_serializer` instead)
|
51
|
+
# @param kwargs [Hash]
|
52
|
+
# @option kwargs [Array<PackageOptions>] :package_options the options for packages in this shipment
|
53
|
+
# @option kwargs [Class] :package_options_class the class to use for package options when none are provided
|
54
|
+
def initialize(
|
55
|
+
pickup_time_window:,
|
56
|
+
pickup_instructions: nil,
|
57
|
+
declared_value: nil,
|
58
|
+
special_instructions: nil,
|
59
|
+
reference_numbers: {},
|
60
|
+
additional_service_codes: [],
|
61
|
+
structures_serializer: BOLStructuresSerializer,
|
62
|
+
packages_serializer: BOLPackagesSerializer,
|
63
|
+
generate_universal_pro: false,
|
64
|
+
**kwargs
|
65
|
+
)
|
66
|
+
@pickup_time_window = pickup_time_window
|
67
|
+
@pickup_instructions = pickup_instructions
|
68
|
+
@declared_value = declared_value
|
69
|
+
@special_instructions = special_instructions
|
70
|
+
@reference_numbers = reference_numbers
|
71
|
+
@additional_service_codes = additional_service_codes
|
72
|
+
@structures_serializer = structures_serializer
|
73
|
+
@packages_serializer = packages_serializer
|
74
|
+
@generate_universal_pro = generate_universal_pro
|
75
|
+
validate_additional_service_codes!
|
76
|
+
super(**kwargs)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Optional service codes that can be used for R+L shipments.
|
80
|
+
ADDITIONAL_SERVICE_CODES = %w[
|
81
|
+
OriginLiftgate
|
82
|
+
DestinationLiftgate
|
83
|
+
InsidePickup
|
84
|
+
InsideDelivery
|
85
|
+
LimitedAccessPickup
|
86
|
+
LimitedAccessDelivery
|
87
|
+
Freezable
|
88
|
+
DeliveryAppointment
|
89
|
+
].freeze
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
# Raises an exception if the additional service codes passed into the initializer
|
94
|
+
# don't correspond to valid codes from {ADDITIONAL_SERVICE_CODES}.
|
95
|
+
#
|
96
|
+
# @raise [ArgumentError] invalid additional service codes
|
97
|
+
# @return [nil]
|
98
|
+
def validate_additional_service_codes!
|
99
|
+
invalid_codes = (additional_service_codes - ADDITIONAL_SERVICE_CODES)
|
100
|
+
return unless invalid_codes.any?
|
101
|
+
|
102
|
+
raise ArgumentError, "Invalid additional service code(s): #{invalid_codes.join(',')}"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FriendlyShipping
|
4
|
+
module Services
|
5
|
+
class RL
|
6
|
+
# Serializes packages for R+L BOL API requests.
|
7
|
+
class BOLPackagesSerializer
|
8
|
+
# @param packages [Array<Physical::Package>] packages to serialize
|
9
|
+
# @param options [BOLOptions] options for these packages
|
10
|
+
# @return [Array<Hash>] serialized packages
|
11
|
+
def self.call(packages:, options:)
|
12
|
+
packages.flat_map do |package|
|
13
|
+
package_options = options.options_for_package(package)
|
14
|
+
package.items.map do |item|
|
15
|
+
item_options = package_options.options_for_item(item)
|
16
|
+
{
|
17
|
+
IsHazmat: false,
|
18
|
+
Pieces: 1,
|
19
|
+
PackageType: "BOX",
|
20
|
+
NMFCItemNumber: item_options.nmfc_primary_code,
|
21
|
+
NMFCSubNumber: item_options.nmfc_sub_code,
|
22
|
+
Class: item_options.freight_class,
|
23
|
+
Weight: item.weight.convert_to(:pounds).value.ceil,
|
24
|
+
Description: item.description.presence || "Commodities"
|
25
|
+
}.compact
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FriendlyShipping
|
4
|
+
module Services
|
5
|
+
class RL
|
6
|
+
class BOLStructuresSerializer
|
7
|
+
# @param [Array<Physical::Structure>] structures
|
8
|
+
# @param [FriendlyShipping::Services::RL::BOLOptions] options
|
9
|
+
# @return [Array<Hash>]
|
10
|
+
def self.call(structures:, options:)
|
11
|
+
structures.flat_map do |structure|
|
12
|
+
structure_options = options.options_for_structure(structure)
|
13
|
+
structure.packages.map do |package|
|
14
|
+
package_options = structure_options.options_for_package(package)
|
15
|
+
{
|
16
|
+
IsHazmat: false,
|
17
|
+
Pieces: 1,
|
18
|
+
PackageType: "BOX",
|
19
|
+
NMFCItemNumber: package_options.nmfc_primary_code,
|
20
|
+
NMFCSubNumber: package_options.nmfc_sub_code,
|
21
|
+
Class: package_options.freight_class,
|
22
|
+
Weight: package.weight.convert_to(:pounds).value.ceil,
|
23
|
+
Description: package.description.presence || "Commodities"
|
24
|
+
}.compact
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'friendly_shipping/item_options'
|
4
|
+
|
5
|
+
module FriendlyShipping
|
6
|
+
module Services
|
7
|
+
class RL
|
8
|
+
# Serializes items for R+L API requests.
|
9
|
+
class ItemOptions < FriendlyShipping::ItemOptions
|
10
|
+
# @return [String] the freight class
|
11
|
+
attr_reader :freight_class
|
12
|
+
|
13
|
+
# @return [String] the NMFC primary code
|
14
|
+
attr_reader :nmfc_primary_code
|
15
|
+
|
16
|
+
# @return [String] the NMFC sub code
|
17
|
+
attr_reader :nmfc_sub_code
|
18
|
+
|
19
|
+
# @param freight_class [String] the freight class
|
20
|
+
# @param nmfc_primary_code [String] the NMFC primary code
|
21
|
+
# @param nmfc_sub_code [String] the NMFC sub code
|
22
|
+
# @param kwargs [Hash]
|
23
|
+
# @option kwargs [String] :item_id the ID for the item that belongs to these options
|
24
|
+
def initialize(
|
25
|
+
freight_class: nil,
|
26
|
+
nmfc_primary_code: nil,
|
27
|
+
nmfc_sub_code: nil,
|
28
|
+
**kwargs
|
29
|
+
)
|
30
|
+
@freight_class = freight_class
|
31
|
+
@nmfc_primary_code = nmfc_primary_code
|
32
|
+
@nmfc_sub_code = nmfc_sub_code
|
33
|
+
super(**kwargs)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'friendly_shipping/package_options'
|
4
|
+
|
5
|
+
module FriendlyShipping
|
6
|
+
module Services
|
7
|
+
class RL
|
8
|
+
# Serializes packages for R+L API requests.
|
9
|
+
class PackageOptions < FriendlyShipping::PackageOptions
|
10
|
+
# @return [String]
|
11
|
+
attr_reader :freight_class
|
12
|
+
|
13
|
+
# @return [String]
|
14
|
+
attr_reader :nmfc_primary_code
|
15
|
+
|
16
|
+
# @return [String]
|
17
|
+
attr_reader :nmfc_sub_code
|
18
|
+
|
19
|
+
# @param freight_class [String] the freight class
|
20
|
+
# @param nmfc_primary_code [String] the NMFC primary code
|
21
|
+
# @param nmfc_sub_code [String] the NMFC sub code
|
22
|
+
# @param kwargs [Hash]
|
23
|
+
# @option kwargs [String] :package_id the ID for the package that belongs to these options
|
24
|
+
# @option kwargs [Array<ItemOptions>] :item_options the options for items in this package
|
25
|
+
# @option kwargs [Class] :item_options_class the class to use for item options when none are provided
|
26
|
+
def initialize(
|
27
|
+
freight_class: nil,
|
28
|
+
nmfc_primary_code: nil,
|
29
|
+
nmfc_sub_code: nil,
|
30
|
+
**kwargs
|
31
|
+
)
|
32
|
+
@freight_class = freight_class
|
33
|
+
@nmfc_primary_code = nmfc_primary_code
|
34
|
+
@nmfc_sub_code = nmfc_sub_code
|
35
|
+
super(**kwargs.reverse_merge(item_options_class: ItemOptions))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'friendly_shipping/services/rl/shipment_information'
|
5
|
+
|
6
|
+
module FriendlyShipping
|
7
|
+
module Services
|
8
|
+
class RL
|
9
|
+
# Parses the response from the R+L API when creating a Bill of Lading (BOL).
|
10
|
+
class ParseCreateBOLResponse
|
11
|
+
extend Dry::Monads::Result::Mixin
|
12
|
+
|
13
|
+
class << self
|
14
|
+
# @param request [Request] the request to attach to the API result
|
15
|
+
# @param response [Response] the response to parse
|
16
|
+
# @return [Result<ApiResult<ShipmentInformation>>] shipment info with the BOL document attached
|
17
|
+
def call(request:, response:)
|
18
|
+
parsed_json = JSON.parse(response.body)
|
19
|
+
shipment_info = ShipmentInformation.new(
|
20
|
+
pro_number: parsed_json['ProNumber'],
|
21
|
+
pickup_request_number: parsed_json['PickupRequestNumber']
|
22
|
+
)
|
23
|
+
if shipment_info.valid?
|
24
|
+
Success(
|
25
|
+
ApiResult.new(
|
26
|
+
shipment_info,
|
27
|
+
original_request: request,
|
28
|
+
original_response: response
|
29
|
+
)
|
30
|
+
)
|
31
|
+
else
|
32
|
+
errors = parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }])
|
33
|
+
Failure(
|
34
|
+
ApiResult.new(
|
35
|
+
errors.map { |e| e['ErrorMessage'] },
|
36
|
+
original_request: request,
|
37
|
+
original_response: response
|
38
|
+
)
|
39
|
+
)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'friendly_shipping/services/rl/shipment_document'
|
5
|
+
|
6
|
+
module FriendlyShipping
|
7
|
+
module Services
|
8
|
+
class RL
|
9
|
+
class ParseInvoiceResponse
|
10
|
+
extend Dry::Monads::Result::Mixin
|
11
|
+
|
12
|
+
class << self
|
13
|
+
# @param [FriendlyShipping::Request] request
|
14
|
+
# @param [FriendlyShipping::Response] response
|
15
|
+
# @return [Result<ApiResult<ShipmentDocument>>]
|
16
|
+
def call(request:, response:)
|
17
|
+
parsed_json = JSON.parse(response.body)
|
18
|
+
data = (parsed_json['Documents'].find { _1["Type"] == "Invoice" } || {})["Data"]
|
19
|
+
|
20
|
+
document = ShipmentDocument.new(binary: data, format: :pdf, document_type: :invoice)
|
21
|
+
if document.valid?
|
22
|
+
Success(
|
23
|
+
ApiResult.new(
|
24
|
+
document,
|
25
|
+
original_request: request,
|
26
|
+
original_response: response
|
27
|
+
)
|
28
|
+
)
|
29
|
+
else
|
30
|
+
errors = if parsed_json["Messages"].present?
|
31
|
+
parsed_json["Messages"]
|
32
|
+
else
|
33
|
+
parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }]).
|
34
|
+
map { _1['ErrorMessage'] }
|
35
|
+
end
|
36
|
+
|
37
|
+
Failure(
|
38
|
+
ApiResult.new(
|
39
|
+
errors,
|
40
|
+
original_request: request,
|
41
|
+
original_response: response
|
42
|
+
)
|
43
|
+
)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'friendly_shipping/services/rl/shipment_document'
|
5
|
+
|
6
|
+
module FriendlyShipping
|
7
|
+
module Services
|
8
|
+
class RL
|
9
|
+
# Parses the response from the R+L API when printing a Bill of Lading (BOL).
|
10
|
+
class ParsePrintBOLResponse
|
11
|
+
extend Dry::Monads::Result::Mixin
|
12
|
+
|
13
|
+
class << self
|
14
|
+
# @param request [Request] the request to attach to the API result
|
15
|
+
# @param response [Response] the response to parse
|
16
|
+
# @return [Result<ApiResult<ShipmentDocument>>] shipment document containing the BOL
|
17
|
+
def call(request:, response:)
|
18
|
+
parsed_json = JSON.parse(response.body)
|
19
|
+
bol_document = ShipmentDocument.new(
|
20
|
+
format: :pdf,
|
21
|
+
document_type: :rl_bol,
|
22
|
+
binary: Base64.decode64(parsed_json['BolDocument'])
|
23
|
+
)
|
24
|
+
if bol_document.valid?
|
25
|
+
Success(
|
26
|
+
ApiResult.new(
|
27
|
+
bol_document,
|
28
|
+
original_request: request,
|
29
|
+
original_response: response
|
30
|
+
)
|
31
|
+
)
|
32
|
+
else
|
33
|
+
errors = parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }])
|
34
|
+
Failure(
|
35
|
+
ApiResult.new(
|
36
|
+
errors.map { |e| e['ErrorMessage'] },
|
37
|
+
original_request: request,
|
38
|
+
original_response: response
|
39
|
+
)
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'friendly_shipping/services/rl/shipment_document'
|
5
|
+
|
6
|
+
module FriendlyShipping
|
7
|
+
module Services
|
8
|
+
class RL
|
9
|
+
# Parses the response from the R+L API when printing shipping labels.
|
10
|
+
class ParsePrintShippingLabelsResponse
|
11
|
+
extend Dry::Monads::Result::Mixin
|
12
|
+
|
13
|
+
class << self
|
14
|
+
# @param request [Request] the request to attach to the API result
|
15
|
+
# @param response [Response] the response to parse
|
16
|
+
# @return [Result<ApiResult<ShipmentDocument>>] shipping document containing the labels
|
17
|
+
def call(request:, response:)
|
18
|
+
parsed_json = JSON.parse(response.body)
|
19
|
+
label_doc = ShipmentDocument.new(
|
20
|
+
format: :pdf,
|
21
|
+
document_type: :label,
|
22
|
+
binary: Base64.decode64(parsed_json['ShippingLabelsFile'])
|
23
|
+
)
|
24
|
+
if label_doc.valid?
|
25
|
+
Success(
|
26
|
+
ApiResult.new(
|
27
|
+
label_doc,
|
28
|
+
original_request: request,
|
29
|
+
original_response: response
|
30
|
+
)
|
31
|
+
)
|
32
|
+
else
|
33
|
+
errors = parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }])
|
34
|
+
Failure(
|
35
|
+
ApiResult.new(
|
36
|
+
errors.map { |e| e['ErrorMessage'] },
|
37
|
+
original_request: request,
|
38
|
+
original_response: response
|
39
|
+
)
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|