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
@@ -0,0 +1,126 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FriendlyShipping
|
4
|
+
module Services
|
5
|
+
class ShipEngine
|
6
|
+
# Serializes a shipment and options for the rates API request.
|
7
|
+
class SerializeRatesRequest
|
8
|
+
class << self
|
9
|
+
# @param shipment [Physical::Shipment] the shipment to serialize
|
10
|
+
# @param options [LabelOptions] the options to serialize
|
11
|
+
# @return [Hash] the serialized request
|
12
|
+
def call(shipment:, options:)
|
13
|
+
rates_hash = {
|
14
|
+
shipment: {
|
15
|
+
carrier_ids: options.carrier_ids,
|
16
|
+
service_code: options.service_code,
|
17
|
+
ship_date: options.ship_date.strftime('%Y-%m-%d'),
|
18
|
+
ship_to: serialize_address(shipment.destination),
|
19
|
+
ship_from: serialize_address(shipment.origin),
|
20
|
+
items: serialize_items(shipment.packages.first),
|
21
|
+
packages: serialize_packages(shipment, options),
|
22
|
+
comparison_rate_type: options.comparison_rate_type,
|
23
|
+
confirmation: 'none'
|
24
|
+
}.merge(SerializeAddressResidentialIndicator.call(shipment.destination)).compact_blank,
|
25
|
+
rate_options: {
|
26
|
+
carrier_ids: options.carrier_ids,
|
27
|
+
service_codes: [options.service_code],
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
if international?(shipment)
|
32
|
+
rates_hash[:shipment][:customs] = {
|
33
|
+
contents: "merchandise",
|
34
|
+
non_delivery: "return_to_sender",
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
rates_hash
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
# @param package [Physical::Package]
|
44
|
+
# @return [Array<Hash>]
|
45
|
+
def serialize_items(package)
|
46
|
+
return [] unless package&.items.present?
|
47
|
+
|
48
|
+
package.items.group_by(&:sku).map do |sku, items|
|
49
|
+
reference_item = items.first
|
50
|
+
{
|
51
|
+
name: reference_item.description,
|
52
|
+
sku: sku,
|
53
|
+
quantity: items.size
|
54
|
+
}
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# @param address [Physical::Location]
|
59
|
+
# @return [Hash]
|
60
|
+
def serialize_address(address)
|
61
|
+
{
|
62
|
+
name: address.name,
|
63
|
+
phone: address.phone,
|
64
|
+
company_name: address.company_name,
|
65
|
+
address_line1: address.address1,
|
66
|
+
address_line2: address.address2,
|
67
|
+
city_locality: address.city,
|
68
|
+
state_province: address.region.code,
|
69
|
+
postal_code: address.zip,
|
70
|
+
country_code: address.country.code
|
71
|
+
}.merge(SerializeAddressResidentialIndicator.call(address))
|
72
|
+
end
|
73
|
+
|
74
|
+
# @param shipment [Physical::Shipment]
|
75
|
+
# @param options [LabelOptions]
|
76
|
+
# @return [Array<Hash>]
|
77
|
+
def serialize_packages(shipment, options)
|
78
|
+
shipment.packages.map do |package|
|
79
|
+
{
|
80
|
+
weight: {
|
81
|
+
value: package.weight.convert_to(:pound).value.to_f.round(2),
|
82
|
+
unit: 'pound'
|
83
|
+
},
|
84
|
+
dimensions: {
|
85
|
+
unit: 'inch',
|
86
|
+
length: package.length.convert_to(:inch).value.to_f.round(2),
|
87
|
+
width: package.width.convert_to(:inch).value.to_f.round(2),
|
88
|
+
height: package.height.convert_to(:inch).value.to_f.round(2)
|
89
|
+
},
|
90
|
+
products: serialize_products(package, options),
|
91
|
+
}
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# @param package [Physical::Package]
|
96
|
+
# @param options [LabelOptions]
|
97
|
+
# @return [Array<Hash>]
|
98
|
+
def serialize_products(package, options)
|
99
|
+
package.items.group_by(&:sku).map do |sku, items|
|
100
|
+
reference_item = items.first
|
101
|
+
package_options = options.options_for_package(package)
|
102
|
+
item_options = package_options.options_for_item(reference_item)
|
103
|
+
{
|
104
|
+
sku: sku,
|
105
|
+
description: reference_item.description,
|
106
|
+
quantity: items.count,
|
107
|
+
value: {
|
108
|
+
amount: reference_item.cost.to_d,
|
109
|
+
currency: reference_item.cost.currency.to_s
|
110
|
+
},
|
111
|
+
harmonized_tariff_code: item_options.commodity_code,
|
112
|
+
country_of_origin: item_options.country_of_origin
|
113
|
+
}
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
# @param shipment [Physical::Shipment]
|
118
|
+
# @return [Boolean]
|
119
|
+
def international?(shipment)
|
120
|
+
shipment.origin.country != shipment.destination.country
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -2,35 +2,54 @@
|
|
2
2
|
|
3
3
|
require 'dry/monads'
|
4
4
|
require 'friendly_shipping/http_client'
|
5
|
-
require 'friendly_shipping/services/ship_engine/
|
5
|
+
require 'friendly_shipping/services/ship_engine/api_error'
|
6
6
|
require 'friendly_shipping/services/ship_engine/parse_carrier_response'
|
7
|
+
require 'friendly_shipping/services/ship_engine/serialize_address_validation_request'
|
7
8
|
require 'friendly_shipping/services/ship_engine/serialize_label_shipment'
|
8
9
|
require 'friendly_shipping/services/ship_engine/serialize_rate_estimate_request'
|
10
|
+
require 'friendly_shipping/services/ship_engine/serialize_rates_request'
|
11
|
+
require 'friendly_shipping/services/ship_engine/serialize_address_residential_indicator'
|
12
|
+
require 'friendly_shipping/services/ship_engine/parse_address_validation_response'
|
9
13
|
require 'friendly_shipping/services/ship_engine/parse_label_response'
|
10
14
|
require 'friendly_shipping/services/ship_engine/parse_void_response'
|
11
|
-
require 'friendly_shipping/services/ship_engine/
|
15
|
+
require 'friendly_shipping/services/ship_engine/parse_rate_estimates_response'
|
16
|
+
require 'friendly_shipping/services/ship_engine/parse_rates_response'
|
12
17
|
require 'friendly_shipping/services/ship_engine/rate_estimates_options'
|
18
|
+
require 'friendly_shipping/services/ship_engine/rates_item_options'
|
19
|
+
require 'friendly_shipping/services/ship_engine/rates_package_options'
|
20
|
+
require 'friendly_shipping/services/ship_engine/rates_options'
|
13
21
|
require 'friendly_shipping/services/ship_engine/label_options'
|
14
22
|
require 'friendly_shipping/services/ship_engine/label_package_options'
|
15
23
|
|
16
24
|
module FriendlyShipping
|
17
25
|
module Services
|
26
|
+
# API service class for ShipEngine, a shipping API supporting UPS, USPS, etc.
|
27
|
+
# @see https://www.shipengine.com/docs/ ShipEngine API docs
|
18
28
|
class ShipEngine
|
29
|
+
# The API base URL.
|
19
30
|
API_BASE = "https://api.shipengine.com/v1/"
|
31
|
+
|
32
|
+
# The API paths. Used when constructing endpoint URLs.
|
20
33
|
API_PATHS = {
|
21
34
|
carriers: "carriers",
|
22
35
|
labels: "labels"
|
23
36
|
}.freeze
|
24
37
|
|
25
|
-
|
38
|
+
# @param token [String] the API token
|
39
|
+
# @param test [Boolean] whether to use test API endpoints
|
40
|
+
# @param client [HttpClient] optional custom HTTP client to use for requests
|
41
|
+
def initialize(token:, test: true, client: nil)
|
26
42
|
@token = token
|
27
43
|
@test = test
|
28
|
-
|
44
|
+
|
45
|
+
error_handler = ApiErrorHandler.new(api_error_class: ShipEngine::ApiError)
|
46
|
+
@client = client || HttpClient.new(error_handler: error_handler)
|
29
47
|
end
|
30
48
|
|
31
|
-
# Get configured carriers
|
49
|
+
# Get configured carriers.
|
32
50
|
#
|
33
|
-
# @
|
51
|
+
# @param debug [Boolean] whether to attach debugging information to the response
|
52
|
+
# @return [ApiResult<Array<Carrier>>, Failure<ApiFailure>] carriers configured in your account
|
34
53
|
def carriers(debug: false)
|
35
54
|
request = FriendlyShipping::Request.new(
|
36
55
|
url: API_BASE + API_PATHS[:carriers],
|
@@ -43,15 +62,35 @@ module FriendlyShipping
|
|
43
62
|
end
|
44
63
|
end
|
45
64
|
|
46
|
-
# Get
|
65
|
+
# Get rates.
|
47
66
|
#
|
48
|
-
# @param [Physical::Shipment] shipment
|
67
|
+
# @param shipment [Physical::Shipment] the shipment for which we're getting rates
|
68
|
+
# @param options [RatesOptions] the options for getting rates (see object description)
|
49
69
|
#
|
50
|
-
# @
|
70
|
+
# @return [Success<ApiResult<Array<Rate>>>, Failure<ApiFailure<Array<String>>] When successfully parsing, an
|
71
|
+
# array of rates in a Success Monad. When the parsing is not successful or ShipEngine can't give us rates,
|
72
|
+
# a Failure monad containing something that can be serialized into an error message using `to_s`.
|
73
|
+
def rates(shipment, options:, debug: false)
|
74
|
+
request = FriendlyShipping::Request.new(
|
75
|
+
url: "#{FriendlyShipping::Services::ShipEngine::API_BASE}rates",
|
76
|
+
http_method: "POST",
|
77
|
+
body: SerializeRatesRequest.call(shipment: shipment, options: options).to_json,
|
78
|
+
headers: request_headers,
|
79
|
+
debug: debug
|
80
|
+
)
|
81
|
+
client.post(request).bind do |response|
|
82
|
+
ParseRatesResponse.call(response: response, request: request, options: options)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Get rate estimates.
|
87
|
+
#
|
88
|
+
# @param shipment [Physical::Shipment] the shipment for which we're getting rate estimates
|
89
|
+
# @param options [RateEstimatesOptions] the options for getting rate estimates (see object description)
|
51
90
|
#
|
52
|
-
# @return [
|
53
|
-
# When the parsing is not successful or ShipEngine can't give us rates,
|
54
|
-
# can be serialized into an error message using `to_s`.
|
91
|
+
# @return [Success<ApiResult<Array<Rate>>>, Failure<ApiFailure<Array<String>>>] When successfully parsing, an
|
92
|
+
# array of rates in a Success Monad. When the parsing is not successful or ShipEngine can't give us rates,
|
93
|
+
# a Failure monad containing something that can be serialized into an error message using `to_s`.
|
55
94
|
def rate_estimates(shipment, options: FriendlyShipping::Services::ShipEngine::RateEstimatesOptions.new, debug: false)
|
56
95
|
request = FriendlyShipping::Request.new(
|
57
96
|
url: "#{API_BASE}rates/estimate",
|
@@ -61,19 +100,20 @@ module FriendlyShipping
|
|
61
100
|
debug: debug
|
62
101
|
)
|
63
102
|
client.post(request).bind do |response|
|
64
|
-
|
103
|
+
ParseRateEstimatesResponse.call(response: response, request: request, options: options)
|
65
104
|
end
|
66
105
|
end
|
67
106
|
|
68
|
-
#
|
107
|
+
# ShipEngine returns timings as part of the rate estimates response
|
108
|
+
alias_method :timings, :rate_estimates
|
109
|
+
|
110
|
+
# Get shipping labels.
|
69
111
|
#
|
70
|
-
# @param [Physical::Shipment]
|
112
|
+
# @param shipment [Physical::Shipment] The shipment for which we're getting labels.
|
71
113
|
# Note: Some ShipEngine carriers, notably USPS, only support one package per shipment, and that's
|
72
114
|
# all that the integration supports at this point.
|
73
|
-
# @param [
|
74
|
-
#
|
75
|
-
# @return [Result<ApiResult<Array<FriendlyShipping::Label>>>] The label returned.
|
76
|
-
#
|
115
|
+
# @param options [LabelOptions] the options for getting labels (see object description)
|
116
|
+
# @return [ApiResult<Array<Label>>, Failure<ApiFailure>] the shipping labels
|
77
117
|
def labels(shipment, options:)
|
78
118
|
request = FriendlyShipping::Request.new(
|
79
119
|
url: API_BASE + API_PATHS[:labels],
|
@@ -86,6 +126,11 @@ module FriendlyShipping
|
|
86
126
|
end
|
87
127
|
end
|
88
128
|
|
129
|
+
# Void a shipping label.
|
130
|
+
#
|
131
|
+
# @param label [Label] the label to void
|
132
|
+
# @param debug [Boolean] whether to include debugging information in the result
|
133
|
+
# @return [Success<ApiResult<String>>, Failure<ApiFailure<String>>] the success or failure message
|
89
134
|
def void(label, debug: false)
|
90
135
|
request = FriendlyShipping::Request.new(
|
91
136
|
url: "#{API_BASE}labels/#{label.id}/void",
|
@@ -99,14 +144,42 @@ module FriendlyShipping
|
|
99
144
|
end
|
100
145
|
end
|
101
146
|
|
147
|
+
# Validate an address using ShipEngine
|
148
|
+
#
|
149
|
+
# @param location [Physical::Location] the address to validate
|
150
|
+
# @return [Success<ApiResult<Array<Physical::Location>>>, Failure<ApiFailure>]
|
151
|
+
def validate_address(location, debug: false)
|
152
|
+
request = FriendlyShipping::Request.new(
|
153
|
+
url: "#{API_BASE}addresses/validate",
|
154
|
+
http_method: "POST",
|
155
|
+
body: SerializeAddressValidationRequest.call(location: location).to_json,
|
156
|
+
headers: request_headers,
|
157
|
+
debug: debug
|
158
|
+
)
|
159
|
+
client.post(request).bind do |response|
|
160
|
+
ParseAddressValidationResponse.call(response: response, request: request)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
alias_method :city_state_lookup, :validate_address
|
165
|
+
|
102
166
|
private
|
103
167
|
|
104
|
-
|
168
|
+
# @return [String] the API token
|
169
|
+
attr_reader :token
|
170
|
+
|
171
|
+
# @return [Boolean] whether to use test API endpoints
|
172
|
+
attr_reader :test
|
173
|
+
|
174
|
+
# @return [HttpClient] the HTTP client to use for requests
|
175
|
+
attr_reader :client
|
105
176
|
|
177
|
+
# Returns the content type and API key as a headers hash.
|
178
|
+
# @return [Hash]
|
106
179
|
def request_headers
|
107
180
|
{
|
108
181
|
content_type: :json,
|
109
|
-
|
182
|
+
'api-key': token
|
110
183
|
}
|
111
184
|
end
|
112
185
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FriendlyShipping
|
4
|
+
module Services
|
5
|
+
class ShipEngineLTL
|
6
|
+
# Item options for rate quotes.
|
7
|
+
# @deprecated Please use `PackageOptions` instead.
|
8
|
+
class ItemOptions < FriendlyShipping::ItemOptions
|
9
|
+
# @return [String] the packaging code
|
10
|
+
attr_reader :packaging_code
|
11
|
+
|
12
|
+
# @return [String] the freight class
|
13
|
+
attr_reader :freight_class
|
14
|
+
|
15
|
+
# @return [String] the NMFC code
|
16
|
+
attr_reader :nmfc_code
|
17
|
+
|
18
|
+
# @return [Boolean] whether the item is stackable
|
19
|
+
attr_reader :stackable
|
20
|
+
|
21
|
+
# @return [Boolean] whether the item contains hazardous materials
|
22
|
+
attr_reader :hazardous_materials
|
23
|
+
|
24
|
+
# @param packaging_code [String] the packaging code
|
25
|
+
# @param freight_class [String] the freight class
|
26
|
+
# @param nmfc_code [String] the NMFC code
|
27
|
+
# @param stackable [Boolean] whether the item is stackable
|
28
|
+
# @param hazardous_materials [Boolean] whether the item contains hazardous materials
|
29
|
+
# @param kwargs [Hash]
|
30
|
+
# @option kwargs [String] :item_id the ID for the item that belongs to these options
|
31
|
+
def initialize(
|
32
|
+
packaging_code: nil,
|
33
|
+
freight_class: nil,
|
34
|
+
nmfc_code: nil,
|
35
|
+
stackable: true,
|
36
|
+
hazardous_materials: false,
|
37
|
+
**kwargs
|
38
|
+
)
|
39
|
+
warn "[DEPRECATION] `ItemOptions` is deprecated. Please use `PackageOptions` instead."
|
40
|
+
@packaging_code = packaging_code
|
41
|
+
@freight_class = freight_class
|
42
|
+
@nmfc_code = nmfc_code
|
43
|
+
@stackable = stackable
|
44
|
+
@hazardous_materials = hazardous_materials
|
45
|
+
super(**kwargs)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FriendlyShipping
|
4
|
+
module Services
|
5
|
+
class ShipEngineLTL
|
6
|
+
# Package options for rate quotes.
|
7
|
+
class PackageOptions < FriendlyShipping::PackageOptions
|
8
|
+
# @return [String]
|
9
|
+
attr_reader :packaging_code
|
10
|
+
|
11
|
+
# @return [String]
|
12
|
+
attr_reader :freight_class
|
13
|
+
|
14
|
+
# @return [String]
|
15
|
+
attr_reader :nmfc_code
|
16
|
+
|
17
|
+
# @return [Boolean]
|
18
|
+
attr_reader :stackable
|
19
|
+
|
20
|
+
# @return [Boolean]
|
21
|
+
attr_reader :hazardous_materials
|
22
|
+
|
23
|
+
# @param packaging_code [String]
|
24
|
+
# @param freight_class [String]
|
25
|
+
# @param nmfc_code [String]
|
26
|
+
# @param stackable [Boolean]
|
27
|
+
# @param hazardous_materials [Boolean]
|
28
|
+
# @param kwargs [Hash]
|
29
|
+
# @option kwargs [String] :package_id the ID for the package that belongs to these options
|
30
|
+
# @option kwargs [Array<ItemOptions>] :item_options the options for items in this package
|
31
|
+
# @option kwargs [Class] :item_options_class the class to use for item options when none are provided
|
32
|
+
def initialize(
|
33
|
+
packaging_code: nil,
|
34
|
+
freight_class: nil,
|
35
|
+
nmfc_code: nil,
|
36
|
+
stackable: true,
|
37
|
+
hazardous_materials: false,
|
38
|
+
**kwargs
|
39
|
+
)
|
40
|
+
@packaging_code = packaging_code
|
41
|
+
@freight_class = freight_class
|
42
|
+
@nmfc_code = nmfc_code
|
43
|
+
@stackable = stackable
|
44
|
+
@hazardous_materials = hazardous_materials
|
45
|
+
super(**kwargs.reverse_merge(item_options_class: ItemOptions))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module FriendlyShipping
|
6
|
+
module Services
|
7
|
+
class ShipEngineLTL
|
8
|
+
# Parses the carriers API response.
|
9
|
+
class ParseCarrierResponse
|
10
|
+
extend Dry::Monads::Result::Mixin
|
11
|
+
|
12
|
+
class << self
|
13
|
+
# @param request [Request] the request to attach to the API result
|
14
|
+
# @param response [Response] the response to parse
|
15
|
+
# @return [Success<ApiResult<Array<Carrier>>>, Failure<ApiFailure<Array<String>>>] the parsed carriers or errors
|
16
|
+
def call(request:, response:)
|
17
|
+
parsed_json = JSON.parse(response.body)
|
18
|
+
carriers = parsed_json.fetch('carriers', []).map do |carrier_data|
|
19
|
+
FriendlyShipping::Carrier.new(
|
20
|
+
id: carrier_data['carrier_id'],
|
21
|
+
name: carrier_data['name'],
|
22
|
+
data: {
|
23
|
+
countries: carrier_data['countries'],
|
24
|
+
features: carrier_data['features'],
|
25
|
+
scac: carrier_data['scac']
|
26
|
+
}
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
if carriers.any?
|
31
|
+
Success(
|
32
|
+
ApiResult.new(
|
33
|
+
carriers,
|
34
|
+
original_request: request,
|
35
|
+
original_response: response
|
36
|
+
)
|
37
|
+
)
|
38
|
+
else
|
39
|
+
errors = parsed_json.fetch('errors', [{ 'message' => 'Unknown error' }])
|
40
|
+
Failure(
|
41
|
+
ApiResult.new(
|
42
|
+
errors.map { |e| e['message'] },
|
43
|
+
original_request: request,
|
44
|
+
original_response: response
|
45
|
+
)
|
46
|
+
)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module FriendlyShipping
|
6
|
+
module Services
|
7
|
+
class ShipEngineLTL
|
8
|
+
# Parses the rate quotes API response.
|
9
|
+
class ParseQuoteResponse
|
10
|
+
extend Dry::Monads::Result::Mixin
|
11
|
+
|
12
|
+
class << self
|
13
|
+
# @param request [Request] the request to attach to the API result
|
14
|
+
# @param response [Response] the response to parse
|
15
|
+
# @return [Success<ApiResult<Array<Rate>>>, Failure<ApiFailure<Array<String>>>] the parsed rates or errors
|
16
|
+
def call(request:, response:)
|
17
|
+
parsed_json = JSON.parse(response.body)
|
18
|
+
rates = build_rates(parsed_json)
|
19
|
+
if rates.any?
|
20
|
+
Success(
|
21
|
+
ApiResult.new(
|
22
|
+
rates,
|
23
|
+
original_request: request,
|
24
|
+
original_response: response
|
25
|
+
)
|
26
|
+
)
|
27
|
+
else
|
28
|
+
errors = parsed_json.fetch('errors', [{ 'message' => 'Unknown error' }])
|
29
|
+
Failure(
|
30
|
+
ApiResult.new(
|
31
|
+
errors.map { |e| e['message'] },
|
32
|
+
original_request: request,
|
33
|
+
original_response: response
|
34
|
+
)
|
35
|
+
)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
# @param parsed_json [Hash] the parsed JSON
|
42
|
+
# @return [Array<Rate>] the parsed rates
|
43
|
+
def build_rates(parsed_json)
|
44
|
+
total = build_total(parsed_json)
|
45
|
+
return [] unless total.positive?
|
46
|
+
|
47
|
+
[
|
48
|
+
FriendlyShipping::Rate.new(
|
49
|
+
shipping_method: build_shipping_method(parsed_json),
|
50
|
+
amounts: { total: total }
|
51
|
+
)
|
52
|
+
]
|
53
|
+
end
|
54
|
+
|
55
|
+
# @param parsed_json [Hash] the parsed JSON
|
56
|
+
# @return [ShippingMethod] the parsed shipping method
|
57
|
+
def build_shipping_method(parsed_json)
|
58
|
+
description = parsed_json.dig("service", "carrier_description")
|
59
|
+
code = parsed_json.dig("service", "code")
|
60
|
+
|
61
|
+
FriendlyShipping::ShippingMethod.new(
|
62
|
+
name: description,
|
63
|
+
service_code: code,
|
64
|
+
multi_package: true
|
65
|
+
)
|
66
|
+
end
|
67
|
+
|
68
|
+
# @param parsed_json [Hash] the parsed JSON
|
69
|
+
# @return [Money] the parsed total charges
|
70
|
+
def build_total(parsed_json)
|
71
|
+
total_charges = parsed_json.fetch("charges", []).detect { |e| e['type'] == "total" }
|
72
|
+
return 0 unless total_charges
|
73
|
+
|
74
|
+
currency = Money::Currency.new(total_charges.dig("amount", "currency"))
|
75
|
+
value = total_charges.dig("amount", "value")
|
76
|
+
Money.new(value * currency.subunit_to_unit, currency)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FriendlyShipping
|
4
|
+
module Services
|
5
|
+
class ShipEngineLTL
|
6
|
+
# Options for rate quote requests.
|
7
|
+
class QuoteOptions < ShipmentOptions
|
8
|
+
# @return [String] the service code to use when getting rates
|
9
|
+
attr_reader :service_code
|
10
|
+
|
11
|
+
# @return [Time] the pickup date
|
12
|
+
attr_reader :pickup_date
|
13
|
+
|
14
|
+
# @return [Array<String>] the accessorial service codes
|
15
|
+
attr_reader :accessorial_service_codes
|
16
|
+
|
17
|
+
# @return [Class] the class to use for serializing structures
|
18
|
+
attr_reader :structures_serializer_class
|
19
|
+
|
20
|
+
# @return [Class] the class to use for serializing packages
|
21
|
+
# @deprecated Use {#structures_serializer_class} instead.
|
22
|
+
attr_reader :packages_serializer_class
|
23
|
+
|
24
|
+
# @param service_code [String] the service code to use when getting rates
|
25
|
+
# @param pickup_date [Time] the pickup date
|
26
|
+
# @param accessorial_service_codes [Array<String>] the accessorial service codes (if any)
|
27
|
+
# @param packages_serializer_class [Class] the class to use for serializing packages (DEPRECATED: use `structures_serializer_class` instead)
|
28
|
+
# @param structures_serializer_class [Class] the class to use for serializing structures
|
29
|
+
# @param kwargs [Hash]
|
30
|
+
# @option kwargs [Array<PackageOptions>] :package_options the options for packages in this shipment (DEPRECATED: use `structure_options` instead)
|
31
|
+
# @option kwargs [Class] :package_options_class the class to use for package options when none are provided (DEPRECATED: use `structure_options_class` instead)
|
32
|
+
# @option kwargs [Array<StructureOptions>] :structure_options the options for structures in this shipment
|
33
|
+
# @option kwargs [Class] :structure_options_class the class to use for structure options when none are provided
|
34
|
+
def initialize(
|
35
|
+
service_code: nil,
|
36
|
+
pickup_date: nil,
|
37
|
+
accessorial_service_codes: [],
|
38
|
+
structures_serializer_class: SerializeStructures,
|
39
|
+
packages_serializer_class: SerializePackages,
|
40
|
+
**kwargs
|
41
|
+
)
|
42
|
+
@service_code = service_code
|
43
|
+
@pickup_date = pickup_date
|
44
|
+
@accessorial_service_codes = accessorial_service_codes
|
45
|
+
@structures_serializer_class = structures_serializer_class
|
46
|
+
@packages_serializer_class = packages_serializer_class
|
47
|
+
super(**kwargs)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FriendlyShipping
|
4
|
+
module Services
|
5
|
+
class ShipEngineLTL
|
6
|
+
# Serializes packages for the rate quote API request.
|
7
|
+
# @deprecated Use {SerializeStructures} instead.
|
8
|
+
class SerializePackages
|
9
|
+
class << self
|
10
|
+
# @param packages [Array<Physical::Package>]
|
11
|
+
# @param options [QuoteOptions]
|
12
|
+
# @return [Array<Hash>]
|
13
|
+
def call(packages:, options:)
|
14
|
+
warn "[DEPRECATION] `SerializePackages` is deprecated. Please use `SerializeStructures` instead."
|
15
|
+
|
16
|
+
packages.flat_map do |package|
|
17
|
+
package_options = options.options_for_package(package)
|
18
|
+
package.items.map do |item|
|
19
|
+
item_options = package_options.options_for_item(item)
|
20
|
+
{
|
21
|
+
code: item_options.packaging_code,
|
22
|
+
freight_class: item_options.freight_class,
|
23
|
+
nmfc_code: item_options.nmfc_code,
|
24
|
+
description: item.description || "Commodities",
|
25
|
+
dimensions: {
|
26
|
+
width: item.width.convert_to(:inches).value.ceil,
|
27
|
+
height: item.height.convert_to(:inches).value.ceil,
|
28
|
+
length: item.length.convert_to(:inches).value.ceil,
|
29
|
+
unit: "inches"
|
30
|
+
},
|
31
|
+
weight: {
|
32
|
+
value: item.weight.convert_to(:pounds).value.ceil,
|
33
|
+
unit: "pounds"
|
34
|
+
},
|
35
|
+
quantity: 1, # we don't support this yet
|
36
|
+
stackable: item_options.stackable,
|
37
|
+
hazardous_materials: item_options.hazardous_materials
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|