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
30
31
32
# 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) ⇒ Object

Buy a Shipment.



60
61
62
63
64
65
66
67
68
69
# 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 = {}) ⇒ Object

Create a Shipment.



9
10
11
12
13
14
# 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 = {}) ⇒ Object

Generate a form for a Shipment.



101
102
103
104
105
106
107
108
109
110
111
# 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) ⇒ Object

Get the next page of shipments.



35
36
37
38
39
40
41
42
43
# 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) ⇒ Object

Get the SmartRates of a Shipment.



53
54
55
56
57
# 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 = {}) ⇒ Object

Insure a Shipment.



72
73
74
75
76
77
# 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 = {}) ⇒ Object

Convert the label format of a Shipment.



87
88
89
90
91
92
# 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) ⇒ Object

Get the lowest SmartRate of a Shipment.



95
96
97
98
# 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

#recommend_ship_date(id, desired_delivery_date) ⇒ Object

Retrieve a recommended ship date for an existing Shipment via the Precision Shipping API, based on a specific desired delivery date.



123
124
125
126
127
128
129
# File 'lib/easypost/services/shipment.rb', line 123

def recommend_ship_date(id, desired_delivery_date)
  url = "shipments/#{id}/smartrate/precision_shipping"
  params = { desired_delivery_date: desired_delivery_date }
  response = @client.make_request(:get, url, params)

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

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

Refund a Shipment.



80
81
82
83
84
# 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) ⇒ Object

Regenerate the rates of a Shipment.



46
47
48
49
50
# 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) ⇒ Object

Retrieve a Shipment.



17
18
19
20
21
# 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) ⇒ Object

Retrieves the estimated delivery date of each Rate via SmartRate.



114
115
116
117
118
119
120
# 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