Class: FriendlyShipping::Services::UpsFreight

Inherits:
Object
  • Object
show all
Includes:
Dry::Monads::Result::Mixin
Defined in:
lib/friendly_shipping/services/ups_freight.rb,
lib/friendly_shipping/services/ups_freight/api_error.rb,
lib/friendly_shipping/services/ups_freight/label_options.rb,
lib/friendly_shipping/services/ups_freight/rates_options.rb,
lib/friendly_shipping/services/ups_freight/shipment_options.rb,
lib/friendly_shipping/services/ups_freight/shipping_methods.rb,
lib/friendly_shipping/services/ups_freight/shipment_document.rb,
lib/friendly_shipping/services/ups_freight/label_item_options.rb,
lib/friendly_shipping/services/ups_freight/rates_item_options.rb,
lib/friendly_shipping/services/ups_freight/label_email_options.rb,
lib/friendly_shipping/services/ups_freight/label_pickup_options.rb,
lib/friendly_shipping/services/ups_freight/shipment_information.rb,
lib/friendly_shipping/services/ups_freight/label_package_options.rb,
lib/friendly_shipping/services/ups_freight/rates_package_options.rb,
lib/friendly_shipping/services/ups_freight/generate_location_hash.rb,
lib/friendly_shipping/services/ups_freight/label_delivery_options.rb,
lib/friendly_shipping/services/ups_freight/label_document_options.rb,
lib/friendly_shipping/services/ups_freight/pickup_request_options.rb,
lib/friendly_shipping/services/ups_freight/generate_reference_hash.rb,
lib/friendly_shipping/services/ups_freight/label_structure_options.rb,
lib/friendly_shipping/services/ups_freight/parse_shipment_document.rb,
lib/friendly_shipping/services/ups_freight/rates_structure_options.rb,
lib/friendly_shipping/services/ups_freight/generate_email_options_hash.rb,
lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb,
lib/friendly_shipping/services/ups_freight/generate_handling_units_hash.rb,
lib/friendly_shipping/services/ups_freight/generate_pickup_options_hash.rb,
lib/friendly_shipping/services/ups_freight/generate_pickup_request_hash.rb,
lib/friendly_shipping/services/ups_freight/parse_freight_label_response.rb,
lib/friendly_shipping/services/ups_freight/generate_commodity_information.rb,
lib/friendly_shipping/services/ups_freight/generate_delivery_options_hash.rb,
lib/friendly_shipping/services/ups_freight/generate_document_options_hash.rb,
lib/friendly_shipping/services/ups_freight/generate_freight_rate_request_hash.rb,
lib/friendly_shipping/services/ups_freight/generate_freight_ship_request_hash.rb

Defined Under Namespace

Classes: ApiError, GenerateCommodityInformation, GenerateDeliveryOptionsHash, GenerateDocumentOptionsHash, GenerateEmailOptionsHash, GenerateFreightRateRequestHash, GenerateFreightShipRequestHash, GenerateHandlingUnitsHash, GenerateLocationHash, GeneratePickupOptionsHash, GeneratePickupRequestHash, GenerateReferenceHash, LabelDeliveryOptions, LabelDocumentOptions, LabelEmailOptions, LabelItemOptions, LabelOptions, LabelPackageOptions, LabelPickupOptions, LabelStructureOptions, ParseFreightLabelResponse, ParseFreightRateResponse, ParseShipmentDocument, PickupRequestOptions, RatesItemOptions, RatesOptions, RatesPackageOptions, RatesStructureOptions, ShipmentDocument, ShipmentInformation, ShipmentOptions

Constant Summary collapse

CARRIER =
FriendlyShipping::Carrier.new(
  id: 'ups_freight',
  name: 'United Parcel Service LTL',
  code: 'ups-freight',
  shipping_methods: SHIPPING_METHODS
)
TEST_URL =
'https://wwwcie.ups.com'
LIVE_URL =
'https://onlinetools.ups.com'
RESOURCES =
{
  rates: '/ship/v1801/freight/rating/ground',
  labels: '/ship/v1607/freight/shipments/Ground'
}.freeze
ORIGIN_COUNTRIES =
%w(
  CA MX PR US
).map { |country_code| Carmen::Country.coded(country_code) }.freeze
SHIPPING_METHODS =
[
  ['308', 'UPS Freight LTL'],
  ['309', 'UPS Freight LTL - Guaranteed'],
  ['334', 'UPS Freight LTL - Guaranteed A.M.'],
  ['349', 'UPS Standard LTL']
].freeze.map do |code, name|
  FriendlyShipping::ShippingMethod.new(
    name: name,
    service_code: code,
    origin_countries: ORIGIN_COUNTRIES,
    multi_package: true
  ).freeze
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, login:, password:, test: true, client: nil) ⇒ UpsFreight

Returns a new instance of UpsFreight.



27
28
29
30
31
32
33
34
35
# File 'lib/friendly_shipping/services/ups_freight.rb', line 27

def initialize(key:, login:, password:, test: true, client: nil)
  @key = key
  @login = 
  @password = password
  @test = test

  error_handler = ApiErrorHandler.new(api_error_class: UpsFreight::ApiError)
  @client = client || HttpClient.new(error_handler: error_handler)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/friendly_shipping/services/ups_freight.rb', line 10

def client
  @client
end

#keyObject (readonly)

Returns the value of attribute key.



10
11
12
# File 'lib/friendly_shipping/services/ups_freight.rb', line 10

def key
  @key
end

#loginObject (readonly)

Returns the value of attribute login.



10
11
12
# File 'lib/friendly_shipping/services/ups_freight.rb', line 10

def 
  @login
end

#passwordObject (readonly)

Returns the value of attribute password.



10
11
12
# File 'lib/friendly_shipping/services/ups_freight.rb', line 10

def password
  @password
end

#testObject (readonly)

Returns the value of attribute test.



10
11
12
# File 'lib/friendly_shipping/services/ups_freight.rb', line 10

def test
  @test
end

Instance Method Details

#carriersObject



37
38
39
# File 'lib/friendly_shipping/services/ups_freight.rb', line 37

def carriers
  Success([CARRIER])
end

#labels(shipment, options:, debug: false) ⇒ Result<ApiResult<ShipmentInformation>] The information that you need for shipping this shipment.

Get labels for a shipment

Parameters:

Returns:

  • (Result<ApiResult<ShipmentInformation>] The information that you need for shipping this shipment.)

    Result] The information that you need for shipping this shipment.



59
60
61
62
63
64
65
66
# File 'lib/friendly_shipping/services/ups_freight.rb', line 59

def labels(shipment, options:, debug: false)
  freight_ship_request_hash = GenerateFreightShipRequestHash.call(shipment: shipment, options: options)
  request = build_request(:labels, freight_ship_request_hash, debug)

  client.post(request).fmap do |response|
    ParseFreightLabelResponse.call(response: response, request: request)
  end
end

#rate_estimates(shipment, options:, debug: false) ⇒ Result<ApiResult<Array<Rate>>>

Get rates for a shipment

Parameters:

Returns:

  • (Result<ApiResult<Array<Rate>>>)

    The rates returned from UPS encoded in a FriendlyShipping::ApiResult object.



46
47
48
49
50
51
52
53
# File 'lib/friendly_shipping/services/ups_freight.rb', line 46

def rate_estimates(shipment, options:, debug: false)
  freight_rate_request_hash = GenerateFreightRateRequestHash.call(shipment: shipment, options: options)
  request = build_request(:rates, freight_rate_request_hash, debug)

  client.post(request).fmap do |response|
    ParseFreightRateResponse.call(response: response, request: request)
  end
end