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 +4 -4
- data/example.rb +98 -39
- data/lib/shippo.rb +60 -52
- data/lib/shippo/address.rb +5 -1
- data/lib/shippo/api_object.rb +1 -1
- data/lib/shippo/create.rb +0 -1
- data/lib/shippo/customs_declaration.rb +7 -0
- data/lib/shippo/customs_item.rb +7 -0
- data/lib/shippo/list.rb +3 -2
- data/lib/shippo/manifest.rb +6 -0
- data/lib/shippo/refund.rb +6 -0
- data/lib/shippo/resource.rb +9 -3
- data/lib/shippo/shipment.rb +6 -2
- metadata +57 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b70f8f58f5b9ba939df519f8843c57a600e8f1c
|
4
|
+
data.tar.gz: ca1964b87ce5f092e4af8880cf9c5568b0598297
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
3
|
-
Shippo::api_pass = 'YOUR_API_PASS'
|
5
|
+
require 'timeout'
|
4
6
|
|
5
|
-
|
6
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
:
|
24
|
-
:
|
25
|
-
:
|
26
|
-
:
|
27
|
-
:
|
28
|
-
:
|
29
|
-
:
|
30
|
-
:
|
31
|
-
:
|
32
|
-
|
33
|
-
|
34
|
-
|
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 =>
|
41
|
-
:mass_unit => :lb
|
42
|
-
|
43
|
-
|
44
|
-
|
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 => '
|
47
|
-
|
48
|
-
:address_from =>
|
49
|
-
:address_to =>
|
74
|
+
:object_purpose => 'PURCHASE',
|
75
|
+
:submission_type => 'DROPOFF',
|
76
|
+
:address_from => address_from,
|
77
|
+
:address_to => address_to,
|
50
78
|
:parcel => parcel,
|
51
|
-
:
|
52
|
-
|
53
|
-
puts
|
54
|
-
|
55
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
-
|
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
|
-
|
data/lib/shippo/address.rb
CHANGED
@@ -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
|
data/lib/shippo/api_object.rb
CHANGED
data/lib/shippo/create.rb
CHANGED
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
|
-
|
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
|
data/lib/shippo/resource.rb
CHANGED
@@ -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
|
-
|
8
|
-
|
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)
|
data/lib/shippo/shipment.rb
CHANGED
@@ -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
|
-
|
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:
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Shippo & Contributors
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
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:
|
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
|