class EasyPost::Services::Shipment

Constants

MODEL_CLASS

The workhorse of the EasyPost API, a Shipment is made up of a “to” and “from” Address, the Parcel being shipped, and any customs forms required for international deliveries.

Public Instance Methods

all(params = {}) click to toggle source

Retrieve a list of Shipments

# File lib/easypost/services/shipment.rb, line 24
def all(params = {})
  filters = {
    key: 'shipments',
    purchased: params[:purchased],
    include_children: params[:include_children],
  }

  get_all_helper('shipments', MODEL_CLASS, params, filters)
end
buy(id, params = {}, end_shipper_id = nil) click to toggle source

Buy a Shipment.

# File lib/easypost/services/shipment.rb, line 60
def buy(id, params = {}, end_shipper_id = nil)
  if params.instance_of?(EasyPost::Models::Rate)
    params = { rate: params.clone }
  end

  params[:end_shipper_id] = end_shipper_id if end_shipper_id
  response = @client.make_request(:post, "shipments/#{id}/buy", params)

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
create(params = {}) click to toggle source

Create a Shipment.

# File lib/easypost/services/shipment.rb, line 9
def create(params = {})
  wrapped_params = { shipment: params }
  response = @client.make_request(:post, 'shipments', wrapped_params)

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
generate_form(id, form_type, form_options = {}) click to toggle source

Generate a form for a Shipment.

# File lib/easypost/services/shipment.rb, line 101
def generate_form(id, form_type, form_options = {})
  params = {}
  params[:type] = form_type
  merged_params = params.merge(form_options)
  wrapped_params = {
    form: merged_params,
  }
  response = @client.make_request(:post, "shipments/#{id}/forms", wrapped_params)

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
get_next_page(collection, page_size = nil) click to toggle source

Get the next page of shipments.

# File lib/easypost/services/shipment.rb, line 35
def get_next_page(collection, page_size = nil)
  raise EasyPost::Errors::EndOfPaginationError.new unless more_pages?(collection)

  params = { before_id: collection.shipments.last.id }
  params[:page_size] = page_size unless page_size.nil?
  params.merge!(collection[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).delete(:key)

  all(params)
end
get_smart_rates(id) click to toggle source

Get the SmartRates of a Shipment.

# File lib/easypost/services/shipment.rb, line 53
def get_smart_rates(id)
  response = @client.make_request(:get, "shipments/#{id}/smartrate")

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS).result || []
end
insure(id, params = {}) click to toggle source

Insure a Shipment.

# File lib/easypost/services/shipment.rb, line 72
def insure(id, params = {})
  params = { amount: params } if params.is_a?(Integer) || params.is_a?(Float)
  response = @client.make_request(:post, "shipments/#{id}/insure", params)

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
label(id, params = {}) click to toggle source

Convert the label format of a Shipment.

# File lib/easypost/services/shipment.rb, line 87
def label(id, params = {})
  params = { file_format: params } if params.is_a?(String)
  response = @client.make_request(:get, "shipments/#{id}/label", params)

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
lowest_smart_rate(id, delivery_days, delivery_accuracy) click to toggle source

Get the lowest SmartRate of a Shipment.

# File lib/easypost/services/shipment.rb, line 95
def lowest_smart_rate(id, delivery_days, delivery_accuracy)
  smart_rates = get_smart_rates(id)
  EasyPost::Util.get_lowest_smart_rate(smart_rates, delivery_days, delivery_accuracy)
end
refund(id, params = {}) click to toggle source

Refund a Shipment.

# File lib/easypost/services/shipment.rb, line 80
def refund(id, params = {})
  response = @client.make_request(:post, "shipments/#{id}/refund", params)

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
regenerate_rates(id) click to toggle source

Regenerate the rates of a Shipment.

# File lib/easypost/services/shipment.rb, line 46
def regenerate_rates(id)
  response = @client.make_request(:post, "shipments/#{id}/rerate")

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
retrieve(id) click to toggle source

Retrieve a Shipment.

# File lib/easypost/services/shipment.rb, line 17
def retrieve(id)
  response = @client.make_request(:get, "shipments/#{id}")

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
retrieve_estimated_delivery_date(id, planned_ship_date) click to toggle source

Retrieves the estimated delivery date of each Rate via SmartRate.

# File lib/easypost/services/shipment.rb, line 114
def retrieve_estimated_delivery_date(id, planned_ship_date)
  url = "shipments/#{id}/smartrate/delivery_date"
  params = { planned_ship_date: planned_ship_date }
  response = @client.make_request(:get, url, params)

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS).rates
end