Class: FriendlyShipping::Services::ShipEngineLTL

Inherits:
Object
  • Object
show all
Includes:
Dry::Monads::Result::Mixin
Defined in:
lib/friendly_shipping/services/ship_engine_ltl.rb,
lib/friendly_shipping/services/ship_engine_ltl/api_error.rb,
lib/friendly_shipping/services/ship_engine_ltl/item_options.rb,
lib/friendly_shipping/services/ship_engine_ltl/quote_options.rb,
lib/friendly_shipping/services/ship_engine_ltl/package_options.rb,
lib/friendly_shipping/services/ship_engine_ltl/shipment_options.rb,
lib/friendly_shipping/services/ship_engine_ltl/structure_options.rb,
lib/friendly_shipping/services/ship_engine_ltl/serialize_packages.rb,
lib/friendly_shipping/services/ship_engine_ltl/parse_quote_response.rb,
lib/friendly_shipping/services/ship_engine_ltl/serialize_structures.rb,
lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb,
lib/friendly_shipping/services/ship_engine_ltl/serialize_quote_request.rb

Overview

API service class for ShipEngine LTL, a shipping API supporting Freight carriers.

Defined Under Namespace

Classes: ApiError, ItemOptions, PackageOptions, ParseCarrierResponse, ParseQuoteResponse, QuoteOptions, SerializePackages, SerializeQuoteRequest, SerializeStructures, ShipmentOptions, StructureOptions

Constant Summary collapse

API_BASE =

The API base URL.

"https://api.shipengine.com/v-beta/ltl/"
API_PATHS =

The API paths. Used when constructing endpoint URLs.

{
  connections: "connections",
  carriers: "carriers",
  quotes: "quotes"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(token:, test: true, client: nil) ⇒ ShipEngineLTL

Returns a new instance of ShipEngineLTL.

Parameters:

  • token (String)

    the API token

  • test (Boolean) (defaults to: true)

    whether to use test API endpoints

  • client (HttpClient) (defaults to: nil)

    optional custom HTTP client to use for requests



25
26
27
28
29
30
31
# File 'lib/friendly_shipping/services/ship_engine_ltl.rb', line 25

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

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

Instance Method Details

#carriers(debug: false) ⇒ Success<ApiResult<Array<Carrier>>>, Failure<ApiResult<Array<String>>>

Get configured LTL carriers.

Parameters:

  • debug (Boolean) (defaults to: false)

    whether to attach debugging information to the response

Returns:

  • (Success<ApiResult<Array<Carrier>>>, Failure<ApiResult<Array<String>>>)

    On success, LTL carriers configured in your account. On failure, a list of error messages.



38
39
40
41
42
43
44
45
46
47
# File 'lib/friendly_shipping/services/ship_engine_ltl.rb', line 38

def carriers(debug: false)
  request = FriendlyShipping::Request.new(
    url: API_BASE + API_PATHS[:carriers],
    headers: request_headers,
    debug: debug
  )
  client.get(request).bind do |response|
    ParseCarrierResponse.call(request: request, response: response)
  end
end

#connect_carrier(credentials, scac, debug: false) ⇒ Success<ApiResult<Hash>>, Failure<ApiResult<String>>

Connect an LTL carrier.

Parameters:

  • credentials (Hash)

    the carrier's connection information

  • scac (String)

    Standard Carrier Alpha Code

  • debug (Boolean) (defaults to: false)

    whether to attach debugging information to the response

Returns:

See Also:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/friendly_shipping/services/ship_engine_ltl.rb', line 56

def connect_carrier(credentials, scac, debug: false)
  request = FriendlyShipping::Request.new(
    url: API_BASE + API_PATHS[:connections] + "/#{scac}",
    body: { credentials: credentials }.to_json,
    headers: request_headers,
    debug: debug
  )
  client.post(request).bind do |response|
    Success(
      ApiResult.new(
        JSON.parse(response.body),
        original_request: request,
        original_response: response
      )
    )
  end
end

#request_quote(carrier_id, shipment, options, debug: false) ⇒ Success<ApiResult<Hash>>, Failure<ApiResult<String>>

Request an LTL price quote.

Parameters:

  • carrier_id (String)

    the carrier ID

  • shipment (Physical::Shipment)

    the shipment to quote

  • options (QuoteOptions)

    the options for the quote

  • debug (Boolean) (defaults to: false)

    whether to attach debugging information to the response

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/friendly_shipping/services/ship_engine_ltl.rb', line 107

def request_quote(carrier_id, shipment, options, debug: false)
  request = FriendlyShipping::Request.new(
    url: API_BASE + API_PATHS[:quotes] + "/#{carrier_id}",
    http_method: "POST",
    body: SerializeQuoteRequest.call(shipment: shipment, options: options).to_json,
    headers: request_headers,
    debug: debug
  )
  client.post(request).bind do |response|
    ParseQuoteResponse.call(request: request, response: response)
  end
end

#update_carrier(credentials, scac, carrier_id, debug: false) ⇒ Success<ApiResult<Hash>>, Failure<ApiResult<String>>

Update an existing LTL carrier.

Parameters:

  • credentials (Hash)

    the carrier's connection information

  • scac (String)

    Standard Carrier Alpha Code

  • carrier_id (String)

    the ID for the carrier you want to update

  • debug (Boolean) (defaults to: false)

    whether to attach debugging information to the response

Returns:

See Also:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/friendly_shipping/services/ship_engine_ltl.rb', line 82

def update_carrier(credentials, scac, carrier_id, debug: false)
  request = FriendlyShipping::Request.new(
    url: API_BASE + API_PATHS[:connections] + "/#{scac}/#{carrier_id}",
    body: { credentials: credentials }.to_json,
    headers: request_headers,
    debug: debug
  )
  client.put(request).bind do |response|
    Success(
      ApiResult.new(
        JSON.parse(response.body),
        original_request: request,
        original_response: response
      )
    )
  end
end