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.
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_token ⇒ AccessToken (readonly)
Returns the access token.
11 12 13 |
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 11 def access_token @access_token end |
#client ⇒ HttpClient (readonly)
Returns the HTTP client.
17 18 19 |
# File 'lib/friendly_shipping/services/tforce_freight.rb', line 17 def client @client end |
#test ⇒ Boolean (readonly)
Returns 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
#carriers ⇒ Array<Carrier>
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.
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)
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: ) 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
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: ) 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
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: ) request = build_request(:rates, freight_rate_request_hash, debug) client.post(request).fmap do |response| ParseRatesResponse.call(response: response, request: request) end end |