Class: EasyPost::Shipment

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

Instance Attribute Summary

Attributes inherited from EasyPostObject

#api_key

Class Method Summary collapse

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

.track_with_code(params = {}) ⇒ Object



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

def self.track_with_code(params={})
  response, api_key = EasyPost.request(:get, url + '/track', @api_key, params)

  return response
end

Instance Method Details

#barcode(params = {}) ⇒ Object



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

def barcode(params={})
  response, api_key = EasyPost.request(:get, url + '/barcode', @api_key, params)

  return response[:barcode_url]
end

#buy(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/easypost/shipment.rb', line 17

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

  response, api_key = EasyPost.request(:post, url + '/buy', @api_key, params)
  self.refresh_from(response, @api_key, true)

  return self
end

#get_rates(params = {}) ⇒ Object



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

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

  return self
end

#insure(params = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/easypost/shipment.rb', line 30

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

  response, api_key = EasyPost.request(:post, url + '/insure', @api_key, params)
  self.refresh_from(response, @api_key, true)

  return self
end

#label(params = {}) ⇒ Object



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

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

  response, api_key = EasyPost.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
129
130
131
132
133
134
135
136
# File 'lib/easypost/shipment.rb', line 75

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

  self.get_rates unless self.rates
  
  if !carriers.is_a?(Array)
    carriers = carriers.split(',')
  end
  carriers.map!(&:downcase)
  carriers.map!(&:strip)

  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

  if !services.is_a?(Array)
    services = services.split(',')
  end
  services.map!(&:downcase)
  services.map!(&:strip)

  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 Error.new('No rates found.') if lowest == nil
  
  return lowest
end

#refund(params = {}) ⇒ Object



43
44
45
46
47
48
# File 'lib/easypost/shipment.rb', line 43

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

  return self
end

#stamp(params = {}) ⇒ Object



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

def stamp(params={})
  response, api_key = EasyPost.request(:get, url + '/stamp', @api_key, params)

  return response[:stamp_url]
end