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

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

Instance Method Details

#buy(params = {}) ⇒ Object

Buy a Shipment.



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

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.



15
16
17
18
19
# File 'lib/easypost/shipment.rb', line 15

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.



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

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.



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

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.

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
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
115
116
117
118
119
120
121
122
123
124
# File 'lib/easypost/shipment.rb', line 72

def lowest_rate(carriers = [], services = [])
  lowest = nil

  get_rates unless rates

  carriers = EasyPost::Util.normalize_string_list(carriers)

  negative_carriers = []
  carriers_copy = carriers.clone
  carriers_copy.each do |carrier|
    if carrier[0, 1] == '!'
      negative_carriers << carrier[1..-1]
      carriers.delete(carrier)
    end
  end

  services = EasyPost::Util.normalize_string_list(services)

  negative_services = []
  services_copy = services.clone
  services_copy.each do |service|
    if service[0, 1] == '!'
      negative_services << service[1..-1]
      services.delete(service)
    end
  end

  rates.each do |k|
    rate_carrier = k.carrier.downcase
    if carriers.size.positive? && !carriers.include?(rate_carrier)
      next
    end
    if negative_carriers.size.positive? && negative_carriers.include?(rate_carrier)
      next
    end

    rate_service = k.service.downcase
    if services.size.positive? && !services.include?(rate_service)
      next
    end
    if negative_services.size.positive? && negative_services.include?(rate_service)
      next
    end

    if lowest.nil? || k.rate.to_f < lowest.rate.to_f
      lowest = k
    end
  end

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

  lowest
end

#refund(params = {}) ⇒ Object

Refund a Shipment.



50
51
52
53
54
55
# File 'lib/easypost/shipment.rb', line 50

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.



7
8
9
10
11
12
# File 'lib/easypost/shipment.rb', line 7

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

  self
end