class FriendlyShipping::Services::ShipEngine

Constants

API_BASE
API_PATHS

Attributes

client[R]
test[R]
token[R]

Public Class Methods

new(token:, test: true, client: FriendlyShipping::HttpClient.new(error_handler: BadRequestHandler)) click to toggle source
# File lib/friendly_shipping/services/ship_engine.rb, line 25
def initialize(token:, test: true, client: FriendlyShipping::HttpClient.new(error_handler: BadRequestHandler))
  @token = token
  @test = test
  @client = client
end

Public Instance Methods

carriers(debug: false) click to toggle source

Get configured carriers from USPS

@return [Result<ApiResult<Array<Carrier>>>] Carriers configured in your shipstation account

# File lib/friendly_shipping/services/ship_engine.rb, line 34
def carriers(debug: false)
  request = FriendlyShipping::Request.new(
    url: API_BASE + API_PATHS[:carriers],
    headers: request_headers,
    debug: debug
  )
  client.get(request).fmap do |response|
    ParseCarrierResponse.call(request: request, response: response)
  end
end
labels(shipment, options:) click to toggle source

Get label(s) from ShipEngine

@param [Physical::Shipment] shipment The shipment object we're trying to get labels for

Note: Some ShipEngine carriers, notably USPS, only support one package per shipment, and that's
all that the integration supports at this point.

@param [FriendlyShipping::Services::ShipEngine::LabelOptions] The options relevant to estimating rates. See object description.

@return [Result<ApiResult<Array<FriendlyShipping::Label>>>] The label returned.

# File lib/friendly_shipping/services/ship_engine.rb, line 75
def labels(shipment, options:)
  request = FriendlyShipping::Request.new(
    url: API_BASE + API_PATHS[:labels],
    body: SerializeLabelShipment.call(shipment: shipment, options: options, test: test).to_json,
    headers: request_headers
  )
  client.post(request).fmap do |response|
    ParseLabelResponse.call(request: request, response: response)
  end
end
rate_estimates(shipment, options: FriendlyShipping::Services::ShipEngine::RateEstimatesOptions.new, debug: false) click to toggle source

Get rate estimates from ShipEngine

@param [Physical::Shipment] shipment The shipment object we're trying to get results for

@options [FriendlyShipping::Services::ShipEngine::RateEstimatesOptions] The options relevant to estimating rates. See object description.

@return [Result<ApiResult<Array<FriendlyShipping::Rate>>>] When successfully parsing, an array of rates in a Success Monad.

When the parsing is not successful or ShipEngine can't give us rates, a Failure monad containing something that
can be serialized into an error message using `to_s`.
# File lib/friendly_shipping/services/ship_engine.rb, line 54
def rate_estimates(shipment, options: FriendlyShipping::Services::ShipEngine::RateEstimatesOptions.new, debug: false)
  request = FriendlyShipping::Request.new(
    url: API_BASE + 'rates/estimate',
    body: SerializeRateEstimateRequest.call(shipment: shipment, options: options).to_json,
    headers: request_headers,
    debug: debug
  )
  client.post(request).bind do |response|
    ParseRateEstimateResponse.call(response: response, request: request, options: options)
  end
end
void(label, debug: false) click to toggle source
# File lib/friendly_shipping/services/ship_engine.rb, line 86
def void(label, debug: false)
  request = FriendlyShipping::Request.new(
    url: "#{API_BASE}labels/#{label.id}/void",
    body: '',
    headers: request_headers,
    debug: debug
  )
  client.put(request).bind do |response|
    ParseVoidResponse.call(request: request, response: response)
  end
end

Private Instance Methods

request_headers() click to toggle source
# File lib/friendly_shipping/services/ship_engine.rb, line 102
def request_headers
  {
    content_type: :json,
    "api-key": token
  }
end