Class: FriendlyShipping::Services::TForceFreight

Inherits:
Object
  • Object
show all
Includes:
Dry::Monads::Result::Mixin
Defined in:
lib/friendly_shipping/services/tforce_freight.rb,
lib/friendly_shipping/services/tforce_freight/api_error.rb,
lib/friendly_shipping/services/tforce_freight/bol_options.rb,
lib/friendly_shipping/services/tforce_freight/access_token.rb,
lib/friendly_shipping/services/tforce_freight/item_options.rb,
lib/friendly_shipping/services/tforce_freight/rates_options.rb,
lib/friendly_shipping/services/tforce_freight/pickup_options.rb,
lib/friendly_shipping/services/tforce_freight/package_options.rb,
lib/friendly_shipping/services/tforce_freight/document_options.rb,
lib/friendly_shipping/services/tforce_freight/shipment_options.rb,
lib/friendly_shipping/services/tforce_freight/shipping_methods.rb,
lib/friendly_shipping/services/tforce_freight/shipment_document.rb,
lib/friendly_shipping/services/tforce_freight/structure_options.rb,
lib/friendly_shipping/services/tforce_freight/rates_item_options.rb,
lib/friendly_shipping/services/tforce_freight/parse_rates_response.rb,
lib/friendly_shipping/services/tforce_freight/shipment_information.rb,
lib/friendly_shipping/services/tforce_freight/parse_pickup_response.rb,
lib/friendly_shipping/services/tforce_freight/rates_package_options.rb,
lib/friendly_shipping/services/tforce_freight/generate_location_hash.rb,
lib/friendly_shipping/services/tforce_freight/generate_reference_hash.rb,
lib/friendly_shipping/services/tforce_freight/parse_shipment_document.rb,
lib/friendly_shipping/services/tforce_freight/parse_create_bol_response.rb,
lib/friendly_shipping/services/tforce_freight/generate_rates_request_hash.rb,
lib/friendly_shipping/services/tforce_freight/generate_handling_units_hash.rb,
lib/friendly_shipping/services/tforce_freight/generate_pickup_request_hash.rb,
lib/friendly_shipping/services/tforce_freight/generate_commodity_information.rb,
lib/friendly_shipping/services/tforce_freight/generate_document_options_hash.rb,
lib/friendly_shipping/services/tforce_freight/generate_create_bol_request_hash.rb

Defined Under Namespace

Classes: AccessToken, ApiError, BOLOptions, DocumentOptions, GenerateCommodityInformation, GenerateCreateBOLRequestHash, GenerateDocumentOptionsHash, GenerateHandlingUnitsHash, GenerateLocationHash, GeneratePickupRequestHash, GenerateRatesRequestHash, GenerateReferenceHash, ItemOptions, PackageOptions, ParseCreateBOLResponse, ParsePickupResponse, ParseRatesResponse, ParseShipmentDocument, PickupOptions, RatesOptions, ShipmentDocument, ShipmentInformation, ShipmentOptions, StructureOptions

Constant Summary collapse

CARRIER =

The TForce Freight carrier

FriendlyShipping::Carrier.new(
  id: 'tforce_freight',
  name: 'TForce Freight',
  code: 'tforce_freight-freight',
  shipping_methods: SHIPPING_METHODS
)
BASE_URL =

The base URL for TForce API requests

'https://api.tforcefreight.com'
RESOURCES =

The TForce API endpoints

{
  rates: '/rating/getRate',
  create_pickup: '/pickup/request',
  create_bol: '/shipping/bol/create'
}.freeze
ORIGIN_COUNTRIES =
%w(
  CA MX PR US
).map { |country_code| Carmen::Country.coded(country_code) }.freeze
SHIPPING_METHODS =
[
  ['308', 'TForce Freight LTL'],
  ['309', 'TForce Freight LTL - Guaranteed'],
  ['334', 'TForce Freight LTL - Guaranteed A.M.'],
  ['349', 'TForce Standard LTL']
].freeze.map do |code, name|
  FriendlyShipping::ShippingMethod.new(
    name: name,
    service_code: code,
    origin_countries: ORIGIN_COUNTRIES,
    multi_package: true
  ).freeze
end
RatesItemOptions =
Deprecated.

use ItemOptions instead

ItemOptions
RatesPackageOptions =
Deprecated.

use PackageOptions instead

PackageOptions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token:, test: true, client: nil) ⇒ TForceFreight

Returns a new instance of TForceFreight.

Parameters:

  • access_token (AccessToken)

    the access token

  • test (Boolean) (defaults to: true)

    whether to use the test API version

  • client (HttpClient) (defaults to: nil)

    optional HTTP client to use for requests



40
41
42
43
44
45
46
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 40

def initialize(access_token:, test: true, client: nil)
  @access_token = access_token
  @test = test

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

Instance Attribute Details

#access_tokenAccessToken (readonly)

Returns the access token.

Returns:



11
12
13
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 11

def access_token
  @access_token
end

#clientHttpClient (readonly)

Returns the HTTP client.

Returns:



17
18
19
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 17

def client
  @client
end

#testBoolean (readonly)

Returns whether to use the test API version.

Returns:

  • (Boolean)

    whether to use the test API version



14
15
16
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 14

def test
  @test
end

Instance Method Details

#carriersArray<Carrier>

Returns:



49
50
51
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 49

def carriers
  Success([CARRIER])
end

#create_access_token(token_endpoint:, client_id:, client_secret:, scope:, grant_type: "client_credentials", debug: false) ⇒ ApiResult<AccessToken>

Creates an access token that can be used for future API requests.

Parameters:

  • token_endpoint (String)

    the Token Endpoint from the Application Integration section of your OAuth Client dialog

  • client_id (String)

    the Client ID of your application from the OAuth Client dialog

  • client_secret (String)

    the secret you created in the OAuth Client Secrets section of the OAuth Client dialog

  • scope (String)

    the Scope value from the Application Integration section your OAuth Client dialog

  • grant_type (String) (defaults to: "client_credentials")

    defaults to "client_credentials" to indicate you wish to obtain a token representing your confidential client application

  • debug (Boolean) (defaults to: false)

    whether to append debug information to the API result

Returns:

See Also:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 63

def create_access_token(
  token_endpoint:,
  client_id:,
  client_secret:,
  scope:,
  grant_type: "client_credentials",
  debug: false
)
  request = FriendlyShipping::Request.new(
    url: token_endpoint,
    http_method: "POST",
    body: "client_id=#{client_id}&" \
          "client_secret=#{client_secret}&" \
          "grant_type=#{grant_type}&" \
          "scope=#{scope}",
    headers: {
      Content_Type: "application/x-www-form-urlencoded",
      Accept: "application/json"
    },
    debug: debug
  )
  client.post(request).fmap do |response|
    hash = JSON.parse(response.body)
    FriendlyShipping::ApiResult.new(
      AccessToken.new(
        token_type: hash['token_type'],
        expires_in: hash['expires_in'],
        ext_expires_in: hash['ext_expires_in'],
        raw_token: hash['access_token']
      ),
      original_request: request,
      original_response: response
    )
  end
end

#create_bol(shipment, options:, debug: false) ⇒ Result<ApiResult>

Create a Bill of Lading (BOL)

Parameters:

  • shipment (Physical::Shipment)

    the shipment for which to create a BOL

  • options (BOLOptions)

    options for the BOL

  • debug (Boolean) (defaults to: false)

    whether to append debug information to the API result

Returns:

  • (Result<ApiResult>)

    the BOL returned from TForce encoded in a ApiResult object

See Also:



138
139
140
141
142
143
144
145
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 138

def create_bol(shipment, options:, debug: false)
  bol_request_hash = GenerateCreateBOLRequestHash.call(shipment: shipment, options: options)
  request = build_request(:create_bol, bol_request_hash, debug)

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

#create_pickup(shipment, options:, debug: false) ⇒ Result<ApiResult>

Create a pickup request

Parameters:

  • shipment (Physical::Shipment)

    the shipment for which to create a pickup request

  • options (PickupOptions)

    options for the pickup request

  • debug (Boolean) (defaults to: false)

    whether to append debug information to the API result

Returns:

  • (Result<ApiResult>)

    the pickup returned from TForce encoded in a ApiResult object

See Also:



122
123
124
125
126
127
128
129
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 122

def create_pickup(shipment, options:, debug: false)
  pickup_request_hash = GeneratePickupRequestHash.call(shipment: shipment, options: options)
  request = build_request(:create_pickup, pickup_request_hash, debug)

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

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

Get rates for a shipment

Parameters:

  • shipment (Physical::Shipment)

    the shipment for which we want to get rates

  • options (RatesOptions)

    options for obtaining rates for this shipment

  • debug (Boolean) (defaults to: false)

    whether to append debug information to the API result

Returns:

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

    the rates returned from TForce encoded in a ApiResult object

See Also:



106
107
108
109
110
111
112
113
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 106

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

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