Class: FriendlyShipping::Services::TForceFreight
- Inherits:
-
Object
- Object
- FriendlyShipping::Services::TForceFreight
- 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
-
#access_token ⇒ AccessToken
readonly
The access token.
-
#client ⇒ HttpClient
readonly
The HTTP client.
-
#test ⇒ Boolean
readonly
Whether to use the test API version.
Instance Method Summary collapse
- #carriers ⇒ Array<Carrier>
-
#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.
-
#create_bol(shipment, options:, debug: false) ⇒ Result<ApiResult>
Create a Bill of Lading (BOL).
-
#create_pickup(shipment, options:, debug: false) ⇒ Result<ApiResult>
Create a pickup request.
-
#initialize(access_token:, test: true, client: nil) ⇒ TForceFreight
constructor
A new instance of TForceFreight.
-
#rates(shipment, options:, debug: false) ⇒ Result<ApiResult<Array<Rate>>>
(also: #rate_estimates)
Get rates for a shipment.
Constructor Details
#initialize(access_token:, test: true, client: nil) ⇒ TForceFreight
Returns a new instance of TForceFreight.
66 67 68 69 70 71 72 |
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 66 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_token ⇒ AccessToken (readonly)
Returns the access token.
37 38 39 |
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 37 def access_token @access_token end |
#client ⇒ HttpClient (readonly)
Returns the HTTP client.
43 44 45 |
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 43 def client @client end |
#test ⇒ Boolean (readonly)
Returns whether to use the test API version.
40 41 42 |
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 40 def test @test end |
Instance Method Details
#carriers ⇒ Array<Carrier>
75 76 77 |
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 75 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.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 89 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)
164 165 166 167 168 169 170 171 |
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 164 def create_bol(shipment, options:, debug: false) bol_request_hash = GenerateCreateBOLRequestHash.call(shipment: shipment, 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
148 149 150 151 152 153 154 155 |
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 148 def create_pickup(shipment, options:, debug: false) pickup_request_hash = GeneratePickupRequestHash.call(shipment: shipment, 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
132 133 134 135 136 137 138 139 |
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 132 def rates(shipment, options:, debug: false) freight_rate_request_hash = GenerateRatesRequestHash.call(shipment: shipment, options: ) request = build_request(:rates, freight_rate_request_hash, debug) client.post(request).fmap do |response| ParseRatesResponse.call(response: response, request: request) end end |