Class: EasyPost::Shipment

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

Instance Attribute Summary

Attributes inherited from EasyPostObject

#api_key

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

Instance Method Details

#barcode(params = {}) ⇒ Object



38
39
40
41
42
# File 'lib/easypost/shipment.rb', line 38

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

  return response[:barcode_url]
end

#buy(params = {}) ⇒ Object



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

def buy(params={})
  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



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

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

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

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/easypost/shipment.rb', line 44

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)

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

  self.rates.each do |k|

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

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



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

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



32
33
34
35
36
# File 'lib/easypost/shipment.rb', line 32

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

  return response[:stamp_url]
end

#track(params = {}) ⇒ Object



25
26
27
28
29
30
# File 'lib/easypost/shipment.rb', line 25

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

  return self
end