Class: EasyPost::Shipment

Inherits:
Resource show all
Defined in:
lib/easypost/shipment.rb

Overview

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.

Instance Attribute Summary

Attributes inherited from EasyPostObject

#api_key, #name, #parent, #unsaved_values

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

all, class_name, create, #delete, each, #refresh, retrieve, #save, url, #url

Methods inherited from EasyPostObject

#[], #[]=, #as_json, construct_from, #deconstruct_keys, #each, #id, #id=, #initialize, #inspect, #keys, #refresh_from, #to_hash, #to_json, #to_s, #values

Constructor Details

This class inherits a constructor from EasyPost::EasyPostObject

Class Method Details

.get_lowest_smartrate(smartrates, delivery_days, delivery_accuracy) ⇒ Object

Get the lowest smartrate from a list of smartrates.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/easypost/shipment.rb', line 85

def self.get_lowest_smartrate(smartrates, delivery_days, delivery_accuracy)
  valid_delivery_accuracy_values = Set[
    'percentile_50',
    'percentile_75',
    'percentile_85',
    'percentile_90',
    'percentile_95',
    'percentile_97',
    'percentile_99',
  ]
  lowest_smartrate = nil

  unless valid_delivery_accuracy_values.include?(delivery_accuracy.downcase)
    raise EasyPost::Error.new("Invalid delivery accuracy value, must be one of: #{valid_delivery_accuracy_values}")
  end

  smartrates.each do |rate|
    next if rate['time_in_transit'][delivery_accuracy] > delivery_days.to_i

    if lowest_smartrate.nil? || rate['rate'].to_f < lowest_smartrate['rate'].to_f
      lowest_smartrate = rate
    end
  end

  if lowest_smartrate.nil?
    raise EasyPost::Error.new('No rates found.')
  end

  lowest_smartrate
end

Instance Method Details

#buy(params = {}) ⇒ Object

Buy a Shipment.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/easypost/shipment.rb', line 24

def buy(params = {})
  if params.instance_of?(EasyPost::Rate)
    temp = params.clone
    params = {}
    params[:rate] = temp
  end

  response = EasyPost.make_request(:post, "#{url}/buy", @api_key, params)
  refresh_from(response, @api_key)

  self
end

#get_smartratesObject

Get the SmartRates of a Shipment.



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

def get_smartrates # rubocop:disable Naming/AccessorMethodName
  response = EasyPost.make_request(:get, "#{url}/smartrate", @api_key)

  response.fetch('result', [])
end

#insure(params = {}) ⇒ Object

Insure a Shipment.



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/easypost/shipment.rb', line 38

def insure(params = {})
  if params.is_a?(Integer) || params.is_a?(Float)
    temp = params.clone
    params = {}
    params[:amount] = temp
  end

  response = EasyPost.make_request(:post, "#{url}/insure", @api_key, params)
  refresh_from(response, @api_key)

  self
end

#label(params = {}) ⇒ Object

Convert the label format of a Shipment.



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/easypost/shipment.rb', line 60

def label(params = {})
  if params.is_a?(String)
    temp = params.clone
    params = {}
    params[:file_format] = temp
  end

  response = EasyPost.make_request(:get, "#{url}/label", @api_key, params)
  refresh_from(response, @api_key)

  self
end

#lowest_rate(carriers = [], services = []) ⇒ Object

Get the lowest rate of a Shipment (can exclude by having ‘’!‘` as the first element of your optional filter lists).



74
75
76
# File 'lib/easypost/shipment.rb', line 74

def lowest_rate(carriers = [], services = [])
  EasyPost::Util.get_lowest_object_rate(self, carriers, services)
end

#lowest_smartrate(delivery_days, delivery_accuracy) ⇒ Object

Get the lowest smartrate of a Shipment.



79
80
81
82
# File 'lib/easypost/shipment.rb', line 79

def lowest_smartrate(delivery_days, delivery_accuracy)
  smartrates = get_smartrates
  EasyPost::Shipment.get_lowest_smartrate(smartrates, delivery_days, delivery_accuracy)
end

#refund(params = {}) ⇒ Object

Refund a Shipment.



52
53
54
55
56
57
# File 'lib/easypost/shipment.rb', line 52

def refund(params = {})
  response = EasyPost.make_request(:get, "#{url}/refund", @api_key, params)
  refresh_from(response, @api_key)

  self
end

#regenerate_rates(params = {}) ⇒ Object

Regenerate the rates of a Shipment.



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

def regenerate_rates(params = {})
  response = EasyPost.make_request(:post, "#{url}/rerate", @api_key, params)
  refresh_from(response, @api_key)

  self
end