Class: EasyPost::Shipment

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

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, #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



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)
  self.refresh_from(response, @api_key, true)

  return self
end

#get_rates(params = {}) ⇒ Object



2
3
4
5
6
7
# File 'lib/easypost/shipment.rb', line 2

def get_rates(params={})
  response = EasyPost.make_request(:get, url + '/rates', @api_key, params)
  self.refresh_from(response, @api_key, true)

  return self
end

#get_smartratesObject



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

def get_smartrates
  response = EasyPost.make_request(:get, url + '/smartrate', @api_key)

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

#insure(params = {}) ⇒ Object



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

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)
  self.refresh_from(response, @api_key, true)

  return self
end

#label(params = {}) ⇒ Object



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

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)
  self.refresh_from(response, @api_key, true)

  return self
end

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

Raises:



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
125
126
127
128
# File 'lib/easypost/shipment.rb', line 75

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

  self.get_rates unless self.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

  self.rates.each do |k|

    rate_carrier = k.carrier.downcase
    if carriers.size() > 0 && !carriers.include?(rate_carrier)
      next
    end
    if negative_carriers.size() > 0 && negative_carriers.include?(rate_carrier)
      next
    end

    rate_service = k.service.downcase
    if services.size() > 0 && !services.include?(rate_service)
      next
    end
    if negative_services.size() > 0 && 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

  return lowest
end


55
56
57
58
59
60
# File 'lib/easypost/shipment.rb', line 55

def print(params={})
  if params.instance_of?(EasyPost::Printer)
    return params.print(self.postage_label)
  end
  return false
end

#refund(params = {}) ⇒ Object



48
49
50
51
52
53
# File 'lib/easypost/shipment.rb', line 48

def refund(params={})
  response = EasyPost.make_request(:get, url + '/refund', @api_key, params)
  self.refresh_from(response, @api_key, true)

  return self
end

#regenerate_rates(params = {}) ⇒ Object



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)
  self.refresh_from(response, @api_key, true)

  return self
end