shippo 0.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e17e4581ec82ca7bb1242373d0f42045696f9a8
4
- data.tar.gz: b159bf9011321abef4e546f7a60b276de4b09d93
3
+ metadata.gz: 3b70f8f58f5b9ba939df519f8843c57a600e8f1c
4
+ data.tar.gz: ca1964b87ce5f092e4af8880cf9c5568b0598297
5
5
  SHA512:
6
- metadata.gz: 48150a7adda38b277e70b3e031229456086a74472e5836892c3d42158445fe1e747e7bc3aef25de24c9a078354c424f53dae96de3b92671b3905180297a3436b
7
- data.tar.gz: 5d2a381f23176b9c45a8c772fee6588facf4134e00e974581fb93edec09fa000d3b0b7d3b8b5d11ea9855e23505bcd207b1a4ca8af226ad99240bac15738db8d
6
+ metadata.gz: 69cab04b54f2063bdd876d9b11d0ae41a90094f54a08d697c92c29d54c52f0aff692216397786bd3e32dd4776d3eb4bdcb11d26942764697498f3518f6dd39b7
7
+ data.tar.gz: 48ee7d5df7a153fc00136ae19a5e6173b6c50eeab1a7a5900fbf5ab3188a61c7bee4a07ebe0d15c349a0ac6e415810b95bbdadac0a414f2e0bf2692b0ce8343c
data/example.rb CHANGED
@@ -1,9 +1,16 @@
1
+ # This example demonstrates how to purchase a label for an international shipment.
2
+ # Creating domestic shipment would follow a similiar proccess but would not require
3
+ # the creation of CustomsItems and CustomsDeclaration objects.
1
4
  require 'shippo'
2
- Shippo::api_user = 'YOUR_API_USER'
3
- Shippo::api_pass = 'YOUR_API_PASS'
5
+ require 'timeout'
4
6
 
5
- from = Shippo::Address.create(
6
- :object_purchase => 'QUOTE',
7
+ # replace <USERNAME> and <PASSWORD> with your credentials
8
+ Shippo::api_user = '<USERNAME>'
9
+ Shippo::api_pass = '<PASSWORD>'
10
+
11
+ # Create address_from object
12
+ address_from = {
13
+ :object_purpose => 'PURCHASE',
7
14
  :name => 'Laura Behrens Wu',
8
15
  :company => 'Shippo',
9
16
  :street1 => 'Clayton St.',
@@ -14,46 +21,98 @@ from = Shippo::Address.create(
14
21
  :zip => '94117',
15
22
  :country => 'US',
16
23
  :phone => '+1 555 341 9393',
17
- :email => 'laura@goshippo.com',
18
- :ip => '',
19
- :metadata => 'Customer ID 123456'
20
- )
21
- puts from
22
- to = Shippo::Address.create(
23
- :object_purchase => 'QUOTE',
24
- :name => 'Mr. John Doe',
25
- :company => 'ACME Inc.',
26
- :street1 => nil,
27
- :street_no => '',
28
- :street2 => '',
29
- :city => 'Berlin',
30
- :country => 'DE',
31
- :ip => '',
32
- :metadata => ''
33
- )
34
- puts to
35
- parcel = Shippo::Parcel.create(
24
+ :email => 'laura@goshippo.com'}
25
+
26
+ # Create address_to object
27
+ address_to = {
28
+ :object_purpose => 'PURCHASE',
29
+ :name => 'Mr Hippo"',
30
+ :company => 'London Zoo"',
31
+ :street1 => 'Regents Park',
32
+ :street2 => 'Outer Cir',
33
+ :city => 'LONDON',
34
+ :state => '',
35
+ :zip => 'NW1 4RY',
36
+ :country => 'GB',
37
+ :phone => '+1 555 341 9393',
38
+ :email => 'mrhippo@goshippo.com'}
39
+
40
+ # Create parcel object
41
+ parcel = {
36
42
  :length => 5,
37
43
  :width => 1,
38
44
  :height => 5.555,
39
45
  :distance_unit => :cm,
40
- :weight => '2.122',
41
- :mass_unit => :lb,
42
- :metadata => 'Customer ID 123456'
43
- )
44
- puts parcel
46
+ :weight => 2,
47
+ :mass_unit => :lb}
48
+
49
+ #example CustomsItems object. This is only required for int'l shipment only.
50
+ customs_item = {
51
+ :description => "T-Shirt",
52
+ :quantity => 2,
53
+ :net_weight => "400",
54
+ :mass_unit => "g",
55
+ :value_amount => "20",
56
+ :value_currency => "USD",
57
+ :origin_country => "US"}
58
+
59
+ #Creating the CustomsDeclaration
60
+ #(CustomsDeclarations are only required for international shipments)
61
+ customs_declaration = Shippo::Customs_Declaration.create(
62
+ :contents_type => "MERCHANDISE",
63
+ :contents_explanation => "T-Shirt purchase",
64
+ :non_delivery_option => "RETURN",
65
+ :certify => true,
66
+ :certify_signer => "Laura Behrens Wu",
67
+ :items => [customs_item])
68
+
69
+ # Creating the shipment object. In this example, the objects are directly passed to the
70
+ # Shipment.create method, Alternatively, the Address and Parcel objects could be created
71
+ # using Address.create(..) and Parcel.create(..) functions respectively
72
+ puts "Creating shipment object.."
45
73
  shipment = Shippo::Shipment.create(
46
- :object_purpose => 'QUOTE',
47
- # you can also put in the object_id directly, but this is more convenient
48
- :address_from => from,
49
- :address_to => to,
74
+ :object_purpose => 'PURCHASE',
75
+ :submission_type => 'DROPOFF',
76
+ :address_from => address_from,
77
+ :address_to => address_to,
50
78
  :parcel => parcel,
51
- :metadata => 'Quote Shipment'
52
- )
53
- puts shipment
54
- # never do this in real life
55
- sleep(5)
79
+ :customs_declaration => customs_declaration)
80
+
81
+ puts "Shipment created. Waiting for rates to be generated.."
82
+
83
+ # Wait for rates to be generated
84
+ timeout_rates_request = 10 # seconds
85
+ while ["QUEUED","WAITING"].include? shipment.object_status do
86
+ Timeout::timeout(timeout_rates_request) do
87
+ shipment = Shippo::Shipment.get(shipment["object_id"])
88
+ end
89
+ end
90
+
91
+ # Get all rates for shipment.
92
+ rates = shipment.rates()
93
+
94
+ # Get the first rate in the rates results
95
+ rate = rates[0]
96
+
97
+ puts "Rates generated. Purchasing a #{rate.provider} #{rate.servicelevel_name} label"
98
+
99
+ # Purchase the desired rate (create a Transaction object)
100
+ transaction = Shippo::Transaction.create(:rate => rate["object_id"])
56
101
 
57
- rates = shipment.rates
102
+ # Wait for transaction to be proccessed
103
+ timeout_label_request = 10 # seconds
104
+ while ["QUEUED","WAITING"].include? transaction.object_status do
105
+ Timeout::timeout(timeout_label_request) do
106
+ transaction = Shippo::Transaction.get(transaction["object_id"])
107
+ end
108
+ end
58
109
 
59
- puts "Shipment #{shipment[:object_id]} has the following rates:\n\n#{rates}"
110
+ # label_url and tracking_number
111
+ if transaction.object_status == "SUCCESS"
112
+ puts "Label sucessfully generated:"
113
+ puts "label_url: #{transaction.label_url}"
114
+ puts "tracking_number: #{transaction.tracking_number}"
115
+ else
116
+ puts "Error generating label:"
117
+ puts transaction.messages
118
+ end
data/lib/shippo.rb CHANGED
@@ -2,17 +2,21 @@ require 'rest_client'
2
2
  require 'json'
3
3
  require 'set'
4
4
 
5
- require 'shippo/error.rb'
6
- require 'shippo/container_object.rb'
7
- require 'shippo/api_object.rb'
8
- require 'shippo/list.rb'
9
- require 'shippo/create.rb'
10
- require 'shippo/resource.rb'
11
- require 'shippo/address.rb'
12
- require 'shippo/parcel.rb'
13
- require 'shippo/shipment.rb'
14
- require 'shippo/transaction.rb'
15
- require 'shippo/rate.rb'
5
+ require_relative 'shippo/error.rb'
6
+ require_relative 'shippo/container_object.rb'
7
+ require_relative 'shippo/api_object.rb'
8
+ require_relative 'shippo/list.rb'
9
+ require_relative 'shippo/create.rb'
10
+ require_relative 'shippo/resource.rb'
11
+ require_relative 'shippo/address.rb'
12
+ require_relative 'shippo/parcel.rb'
13
+ require_relative 'shippo/shipment.rb'
14
+ require_relative 'shippo/transaction.rb'
15
+ require_relative 'shippo/rate.rb'
16
+ require_relative 'shippo/manifest.rb'
17
+ require_relative 'shippo/customs_item.rb'
18
+ require_relative 'shippo/customs_declaration.rb'
19
+ require_relative 'shippo/refund.rb'
16
20
 
17
21
  module Shippo
18
22
  @api_base = 'https://api.goshippo.com/v1'
@@ -29,46 +33,51 @@ module Shippo
29
33
  end
30
34
 
31
35
  def self.request(method, url, params = {}, headers = {})
32
- unless @api_user && @api_pass
33
- raise AuthError.new("API credentials missing! Make sure to set Shippo.api_user, Shippo.api_Pass")
34
- end
35
- begin
36
- payload = {}
37
- url = api_url(url)
38
- case method
39
- when :get
40
- pairs = []
41
- params.each { |k, v|
42
- pairs.push "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
43
- }
44
- url += "?#{pairs.join('&')}" unless pairs.empty?
45
- when :post
46
- payload = params
47
- end
48
- opts = { :headers => headers,
49
- :method => method,
50
- :payload => payload,
51
- :url => url,
52
- :open_timeout => 15,
53
- :timeout => 30,
54
- :user => @api_user,
55
- :password => @api_pass
56
- }
57
- res = make_request(opts)
58
- rescue => e
59
- case e
60
- when RestClient::ServerBrokeConnection, RestClient::RequestTimeout
61
- msg = "Could not connect to the Shippo API at #{@api_base}. " +
62
- "Please proceed to check your connection, try again and " +
63
- "contact Shippo support should the issue persist."
64
- when SocketError
65
- msg = "Unexpected error connecting to the Shippo API at #{@api_base}."
66
- else
67
- msg = "Connection error"
68
- end
69
- raise ConnectionError.new msg + "\n\n(e.message)"
70
- end
71
- parse(res)
36
+ unless @api_user && @api_pass
37
+ raise AuthError.new("API credentials missing! Make sure to set Shippo.api_user, Shippo.api_Pass")
38
+ end
39
+ begin
40
+ payload = {}
41
+ url = api_url(url)
42
+ headers.merge!(:accept => :json, :content_type => :json)
43
+ case method
44
+ when :get
45
+ pairs = []
46
+ params.each { |k, v|
47
+ pairs.push "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
48
+ }
49
+ url += "?#{pairs.join('&')}" unless pairs.empty?
50
+ when :post
51
+ payload = params.to_json
52
+ end
53
+ opts = { :headers => headers,
54
+ :method => method,
55
+ :payload => payload,
56
+ :url => url,
57
+ :open_timeout => 15,
58
+ :timeout => 30,
59
+ :user => @api_user,
60
+ :password => @api_pass,
61
+ :user_agent => "Shippo/v1 RubyBindings"
62
+ }
63
+ res = make_request(opts)
64
+ rescue => e
65
+ case e
66
+ when RestClient::ServerBrokeConnection, RestClient::RequestTimeout
67
+ msg = "Could not connect to the Shippo API at #{@api_base}. " +
68
+ "Please proceed to check your connection, try again and " +
69
+ "contact Shippo support should the issue persist."
70
+ raise ConnectionError.new msg + "\n\n(e.message)"
71
+ when SocketError
72
+ msg = "Unexpected error connecting to the Shippo API at #{@api_base}."
73
+ when RestClient::ExceptionWithResponse
74
+ msg = "error: #{e} #{e.http_body}"
75
+ else
76
+ msg = "error: #{e}"
77
+ end
78
+ raise APIError.new msg
79
+ end
80
+ parse(res)
72
81
  end
73
82
  def self.parse(response)
74
83
  JSON::parse(response.body, { :symbolize_names => true })
@@ -83,4 +92,3 @@ def make_request(opts)
83
92
  end
84
93
  }
85
94
  end
86
-
@@ -1,6 +1,10 @@
1
- module Shippo
1
+ module Shippo
2
2
  class Address < Resource
3
3
  include Shippo::Operations::List
4
4
  include Shippo::Operations::Create
5
+ def validate(params={})
6
+ response = Shippo.request(:get, "#{url}/validate/", params)
7
+ Shippo::Address.construct_from(response)
8
+ end
5
9
  end
6
10
  end
@@ -41,7 +41,7 @@ module Shippo
41
41
  @values[k.to_sym] = v
42
42
  end
43
43
  instance_eval do
44
- add_accessors(@values.keys)
44
+ add_accessors(@values.keys)
45
45
  end
46
46
 
47
47
  end
data/lib/shippo/create.rb CHANGED
@@ -10,7 +10,6 @@ module Shippo
10
10
  self.construct_from(response)
11
11
  end
12
12
  end
13
-
14
13
  def self.included(base)
15
14
  base.extend(ClassMethods)
16
15
  end
@@ -0,0 +1,7 @@
1
+ module Shippo
2
+ class Customs_Declaration < Resource
3
+ @non_standard_URL = "/customs/declarations"
4
+ include Shippo::Operations::List
5
+ include Shippo::Operations::Create
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Shippo
2
+ class Customs_Item < Resource
3
+ @non_standard_URL = "/customs/items"
4
+ include Shippo::Operations::List
5
+ include Shippo::Operations::Create
6
+ end
7
+ end
data/lib/shippo/list.rb CHANGED
@@ -5,7 +5,9 @@ module Shippo
5
5
  # return all items
6
6
  def all(params={})
7
7
  response = Shippo.request(:get, "#{url}/", params)
8
- self.construct_from(response[:results] || [])
8
+ # Limiting to results array, does not allow user to see count,..
9
+ # self.construct_from(response[:results] || [])
10
+ self.construct_from(response)
9
11
  end
10
12
  # return a specific item
11
13
  def get(id, params={})
@@ -13,7 +15,6 @@ module Shippo
13
15
  self.construct_from(response)
14
16
  end
15
17
  end
16
-
17
18
  def self.included(base)
18
19
  base.extend(ClassMethods)
19
20
  end
@@ -0,0 +1,6 @@
1
+ module Shippo
2
+ class Manifest < Resource
3
+ include Shippo::Operations::List
4
+ include Shippo::Operations::Create
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Shippo
2
+ class Refund < Resource
3
+ include Shippo::Operations::List
4
+ include Shippo::Operations::Create
5
+ end
6
+ end
@@ -1,11 +1,18 @@
1
1
  module Shippo
2
2
  class Resource < ApiObject
3
+ @non_standard_URL
3
4
  def self.class_name
4
5
  self.name.split('::')[-1]
5
6
  end
6
7
  def self.url()
7
- dc = class_name.downcase
8
- "/" + dc + (dc[-1] == 's' ? 'es' : 's')
8
+ # Process non standard URL
9
+ if defined? @non_standard_URL
10
+ return @non_standard_URL
11
+ else
12
+ # Process standard url
13
+ dc = class_name.downcase
14
+ "/" + dc + (dc[-1] == 's' ? 'es' : 's')
15
+ end
9
16
  end
10
17
  def url
11
18
  unless id = self['object_id']
@@ -13,7 +20,6 @@ module Shippo
13
20
  end
14
21
  "#{self.class.url}/#{CGI.escape(id)}"
15
22
  end
16
-
17
23
  def refresh
18
24
  response, api_key = Shippo.request(:get, url, @retrieve_options)
19
25
  refresh_from(response)
@@ -2,8 +2,12 @@ module Shippo
2
2
  class Shipment < Resource
3
3
  include Shippo::Operations::List
4
4
  include Shippo::Operations::Create
5
- def rates(params={})
6
- response = Shippo.request(:get, "#{url}/rates/", params)
5
+ def rates(currency=nil, params={})
6
+ if !currency.nil?
7
+ response = Shippo.request(:get, "#{url}/rates/#{currency}/", params)
8
+ else
9
+ response = Shippo.request(:get, "#{url}/rates/", params)
10
+ end
7
11
  Shippo::Rate.construct_from(response[:results])
8
12
  end
9
13
  end
metadata CHANGED
@@ -1,17 +1,65 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shippo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
- - Tobias Schottdorf
7
+ - Shippo & Contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-03 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2014-08-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mime-types
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '1.25'
34
+ - - <
35
+ - !ruby/object:Gem::Version
36
+ version: '3.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '1.25'
44
+ - - <
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: json
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ version: 1.8.1
13
61
  description: Quick and easy access to the Shippo API
14
- email: tobias.schottdorf@gmail.com
62
+ email: support@goshippo.com
15
63
  executables: []
16
64
  extensions: []
17
65
  extra_rdoc_files: []
@@ -23,10 +71,14 @@ files:
23
71
  - ./lib/shippo/api_object.rb
24
72
  - ./lib/shippo/container_object.rb
25
73
  - ./lib/shippo/create.rb
74
+ - ./lib/shippo/customs_declaration.rb
75
+ - ./lib/shippo/customs_item.rb
26
76
  - ./lib/shippo/error.rb
27
77
  - ./lib/shippo/list.rb
78
+ - ./lib/shippo/manifest.rb
28
79
  - ./lib/shippo/parcel.rb
29
80
  - ./lib/shippo/rate.rb
81
+ - ./lib/shippo/refund.rb
30
82
  - ./lib/shippo/resource.rb
31
83
  - ./lib/shippo/shipment.rb
32
84
  - ./lib/shippo/transaction.rb