Class: EasyPost::Services::Shipment

Inherits:
Service
  • Object
show all
Defined in:
lib/easypost/services/shipment.rb

Constant Summary collapse

MODEL_CLASS =
EasyPost::Models::Shipment

Instance Method Summary collapse

Methods inherited from Service

#initialize

Constructor Details

This class inherits a constructor from EasyPost::Services::Service

Instance Method Details

#all(params = {}) ⇒ Object

Retrieve a list of Shipments



24
25
26
27
28
29
# File 'lib/easypost/services/shipment.rb', line 24

def all(params = {})
  response = @client.make_request(:get, 'shipments', MODEL_CLASS, params)
  response.define_singleton_method(:purchased) { params[:purchased] }
  response.define_singleton_method(:include_children) { params[:include_children] }
  response
end

#buy(id, params = {}, with_carbon_offset = false, end_shipper_id = nil) ⇒ Object

Buy a Shipment.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/easypost/services/shipment.rb', line 49

def buy(id, params = {}, with_carbon_offset = false, end_shipper_id = nil)
  if params.instance_of?(EasyPost::Models::Rate)
    params = { rate: params.clone }
  end

  params[:carbon_offset] = params[:with_carbon_offset] || with_carbon_offset
  params.delete(:with_carbon_offset)

  params[:end_shipper_id] = end_shipper_id if end_shipper_id

  @client.make_request(:post, "shipments/#{id}/buy", MODEL_CLASS, params)
end

#create(params = {}, with_carbon_offset = false) ⇒ Object

Create a Shipment.



9
10
11
12
13
14
15
16
# File 'lib/easypost/services/shipment.rb', line 9

def create(params = {}, with_carbon_offset = false)
  wrapped_params = {
    shipment: params,
    carbon_offset: with_carbon_offset,
  }

  @client.make_request(:post, 'shipments', MODEL_CLASS, wrapped_params)
end

#generate_form(id, form_type, form_options = {}) ⇒ Object

Generate a form for a Shipment.



88
89
90
91
92
93
94
95
96
97
# File 'lib/easypost/services/shipment.rb', line 88

def generate_form(id, form_type, form_options = {})
  params = {}
  params[:type] = form_type
  merged_params = params.merge(form_options)
  wrapped_params = {
    form: merged_params,
  }

  @client.make_request(:post, "shipments/#{id}/forms", MODEL_CLASS, wrapped_params)
end

#get_next_page(collection, page_size = nil) ⇒ Object

Get the next page of shipments.



32
33
34
# File 'lib/easypost/services/shipment.rb', line 32

def get_next_page(collection, page_size = nil)
  get_next_page_helper(collection, collection.shipments, 'shipments', MODEL_CLASS, page_size)
end

#get_smart_rates(id) ⇒ Object

Get the SmartRates of a Shipment.



44
45
46
# File 'lib/easypost/services/shipment.rb', line 44

def get_smart_rates(id)
  @client.make_request(:get, "shipments/#{id}/smartrate", MODEL_CLASS).result || []
end

#insure(id, params = {}) ⇒ Object

Insure a Shipment.



63
64
65
66
67
# File 'lib/easypost/services/shipment.rb', line 63

def insure(id, params = {})
  params = { amount: params } if params.is_a?(Integer) || params.is_a?(Float)

  @client.make_request(:post, "shipments/#{id}/insure", MODEL_CLASS, params)
end

#label(id, params = {}) ⇒ Object

Convert the label format of a Shipment.



75
76
77
78
79
# File 'lib/easypost/services/shipment.rb', line 75

def label(id, params = {})
  params = { file_format: params } if params.is_a?(String)

  @client.make_request(:get, "shipments/#{id}/label", MODEL_CLASS, params)
end

#lowest_smart_rate(id, delivery_days, delivery_accuracy) ⇒ Object

Get the lowest SmartRate of a Shipment.



82
83
84
85
# File 'lib/easypost/services/shipment.rb', line 82

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 = {}) ⇒ Object

Refund a Shipment.



70
71
72
# File 'lib/easypost/services/shipment.rb', line 70

def refund(id, params = {})
  @client.make_request(:post, "shipments/#{id}/refund", MODEL_CLASS, params)
end

#regenerate_rates(id, with_carbon_offset = false) ⇒ Object

Regenerate the rates of a Shipment.



37
38
39
40
41
# File 'lib/easypost/services/shipment.rb', line 37

def regenerate_rates(id, with_carbon_offset = false)
  params = { carbon_offset: with_carbon_offset }

  @client.make_request(:post, "shipments/#{id}/rerate", MODEL_CLASS, params)
end

#retrieve(id) ⇒ Object

Retrieve a Shipment.



19
20
21
# File 'lib/easypost/services/shipment.rb', line 19

def retrieve(id)
  @client.make_request(:get, "shipments/#{id}", MODEL_CLASS)
end

#retrieve_estimated_delivery_date(id, planned_ship_date) ⇒ Object

Retrieves the estimated delivery date of each Rate via SmartRate.



100
101
102
103
104
105
# File 'lib/easypost/services/shipment.rb', line 100

def retrieve_estimated_delivery_date(id, planned_ship_date)
  url = "shipments/#{id}/smartrate/delivery_date"
  params = { planned_ship_date: planned_ship_date }

  @client.make_request(:get, url, MODEL_CLASS, params).rates
end