Class: FriendlyShipping::Services::ShipEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/services/ship_engine.rb,
lib/friendly_shipping/services/ship_engine/bad_request.rb,
lib/friendly_shipping/services/ship_engine/label_options.rb,
lib/friendly_shipping/services/ship_engine/label_item_options.rb,
lib/friendly_shipping/services/ship_engine/bad_request_handler.rb,
lib/friendly_shipping/services/ship_engine/parse_void_response.rb,
lib/friendly_shipping/services/ship_engine/parse_label_response.rb,
lib/friendly_shipping/services/ship_engine/label_customs_options.rb,
lib/friendly_shipping/services/ship_engine/label_package_options.rb,
lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb,
lib/friendly_shipping/services/ship_engine/rate_estimates_options.rb,
lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb,
lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb,
lib/friendly_shipping/services/ship_engine/serialize_rate_estimate_request.rb

Defined Under Namespace

Classes: BadRequest, BadRequestHandler, LabelCustomsOptions, LabelItemOptions, LabelOptions, LabelPackageOptions, ParseCarrierResponse, ParseLabelResponse, ParseRateEstimateResponse, ParseVoidResponse, RateEstimatesOptions, SerializeLabelShipment, SerializeRateEstimateRequest

Constant Summary collapse

API_BASE =
"https://api.shipengine.com/v1/"
API_PATHS =
{
  carriers: "carriers",
  labels: "labels"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(token:, test: true, client: FriendlyShipping::HttpClient.new(error_handler: BadRequestHandler)) ⇒ ShipEngine

Returns a new instance of ShipEngine.



25
26
27
28
29
# 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

Instance Method Details

#carriers(debug: false) ⇒ Result<ApiResult<Array<Carrier>>>

Get configured carriers from ShipEngine

Returns:



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/friendly_shipping/services/ship_engine.rb', line 34

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

#labels(shipment, options:) ⇒ Result<ApiResult<Array<FriendlyShipping::Label>>>

Get label(s) from ShipEngine

Parameters:

  • shipment (Physical::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.

  • options (FriendlyShipping::Services::ShipEngine::LabelOptions)

    The options relevant to estimating rates. See object description.

Returns:



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/friendly_shipping/services/ship_engine.rb', line 77

def labels(shipment, options:)
  request = FriendlyShipping::Request.new(
    url: API_BASE + API_PATHS[:labels],
    http_method: "POST",
    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) ⇒ Result<ApiResult<Array<FriendlyShipping::Rate>>>

Get rate estimates from ShipEngine

Parameters:

  • shipment (Physical::Shipment)

    The shipment object we’re trying to get results for

Returns:

  • (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`.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/friendly_shipping/services/ship_engine.rb', line 55

def rate_estimates(shipment, options: FriendlyShipping::Services::ShipEngine::RateEstimatesOptions.new, debug: false)
  request = FriendlyShipping::Request.new(
    url: "#{API_BASE}rates/estimate",
    http_method: "POST",
    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) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/friendly_shipping/services/ship_engine.rb', line 89

def void(label, debug: false)
  request = FriendlyShipping::Request.new(
    url: "#{API_BASE}labels/#{label.id}/void",
    http_method: "PUT",
    body: '',
    headers: request_headers,
    debug: debug
  )
  client.put(request).bind do |response|
    ParseVoidResponse.call(request: request, response: response)
  end
end