shippo 1.0.4 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.atom-build.json +22 -0
- data/.codeclimate.yml +30 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.rubocop.yml +1156 -0
- data/.travis.yml +16 -0
- data/CHANGELOG.md +50 -0
- data/Gemfile +5 -0
- data/Guardfile +39 -0
- data/README.md +288 -0
- data/Rakefile +24 -0
- data/bin/example +114 -0
- data/lib/shippo.rb +23 -97
- data/lib/shippo/api.rb +52 -0
- data/lib/shippo/api/api_object.rb +133 -0
- data/lib/shippo/api/category.rb +49 -0
- data/lib/shippo/api/category/base.rb +144 -0
- data/lib/shippo/api/category/purpose.rb +13 -0
- data/lib/shippo/api/category/source.rb +16 -0
- data/lib/shippo/api/category/state.rb +13 -0
- data/lib/shippo/api/category/status.rb +17 -0
- data/lib/shippo/api/extend/operation.rb +21 -0
- data/lib/shippo/api/extend/transformers.rb +12 -0
- data/lib/shippo/api/extend/url.rb +26 -0
- data/lib/shippo/api/operations.rb +8 -0
- data/lib/shippo/api/operations/create.rb +33 -0
- data/lib/shippo/api/operations/list.rb +22 -0
- data/lib/shippo/api/operations/rates.rb +16 -0
- data/lib/shippo/api/operations/update.rb +12 -0
- data/lib/shippo/api/operations/validate.rb +12 -0
- data/lib/shippo/api/request.rb +159 -0
- data/lib/shippo/api/resource.rb +104 -0
- data/lib/shippo/api/transformers/list.rb +73 -0
- data/lib/shippo/api/version.rb +5 -0
- data/lib/shippo/exceptions.rb +7 -0
- data/lib/shippo/exceptions/api_error.rb +20 -0
- data/lib/shippo/exceptions/error.rb +22 -0
- data/lib/shippo/model/address.rb +5 -0
- data/lib/shippo/model/carrieraccount.rb +5 -0
- data/lib/shippo/model/customs_declaration.rb +6 -0
- data/lib/shippo/model/customs_item.rb +5 -0
- data/lib/shippo/model/manifest.rb +5 -0
- data/lib/shippo/model/parcel.rb +5 -0
- data/lib/shippo/model/rate.rb +5 -0
- data/lib/shippo/model/refund.rb +5 -0
- data/lib/shippo/model/shipment.rb +5 -0
- data/lib/shippo/model/transaction.rb +5 -0
- data/lib/shippo/tasks/shippo.rb +22 -0
- data/shippo.gemspec +34 -0
- metadata +226 -40
- data/example.rb +0 -71
- data/lib/shippo/address.rb +0 -10
- data/lib/shippo/api_object.rb +0 -89
- data/lib/shippo/carrieraccount.rb +0 -8
- data/lib/shippo/container_object.rb +0 -28
- data/lib/shippo/create.rb +0 -18
- data/lib/shippo/customs_declaration.rb +0 -7
- data/lib/shippo/customs_item.rb +0 -7
- data/lib/shippo/error.rb +0 -18
- data/lib/shippo/list.rb +0 -23
- data/lib/shippo/manifest.rb +0 -6
- data/lib/shippo/parcel.rb +0 -6
- data/lib/shippo/rate.rb +0 -5
- data/lib/shippo/refund.rb +0 -6
- data/lib/shippo/resource.rb +0 -29
- data/lib/shippo/shipment.rb +0 -14
- data/lib/shippo/transaction.rb +0 -6
- data/lib/shippo/update.rb +0 -15
- data/test/test.rb +0 -26
data/example.rb
DELETED
@@ -1,71 +0,0 @@
|
|
1
|
-
# This example demonstrates how to purchase a label for a domestic US shipment.
|
2
|
-
require 'shippo'
|
3
|
-
|
4
|
-
# replace <YOUR_PRIVATE_KEY> with your ShippoToken key
|
5
|
-
Shippo::api_token = '<YOUR_PRIVATE_KEY>'
|
6
|
-
|
7
|
-
# Create address_from object
|
8
|
-
address_from = {
|
9
|
-
:object_purpose => 'PURCHASE',
|
10
|
-
:name => 'Mr Hippo',
|
11
|
-
:company => 'Shippo',
|
12
|
-
:street1 => '215 Clayton St.',
|
13
|
-
:street2 => '',
|
14
|
-
:city => 'San Francisco',
|
15
|
-
:state => 'CA',
|
16
|
-
:zip => '94117',
|
17
|
-
:country => 'US',
|
18
|
-
:phone => '+1 555 341 9393',
|
19
|
-
:email => 'support@goshippo.com'}
|
20
|
-
|
21
|
-
# Create address_to object
|
22
|
-
address_to = {
|
23
|
-
:object_purpose => 'PURCHASE',
|
24
|
-
:name => 'Mrs Hippo"',
|
25
|
-
:company => 'San Diego Zoo',
|
26
|
-
:street1 => '2920 Zoo Drive',
|
27
|
-
:city => 'San Diego',
|
28
|
-
:state => 'CA',
|
29
|
-
:zip => '92101',
|
30
|
-
:country => 'US',
|
31
|
-
:phone => '+1 555 341 9393',
|
32
|
-
:email => 'hippo@goshippo.com'}
|
33
|
-
|
34
|
-
# Create parcel object
|
35
|
-
parcel = {
|
36
|
-
:length => 5,
|
37
|
-
:width => 2,
|
38
|
-
:height => 5,
|
39
|
-
:distance_unit => :in,
|
40
|
-
:weight => 2,
|
41
|
-
:mass_unit => :lb}
|
42
|
-
|
43
|
-
# Creating the shipment object
|
44
|
-
puts "Creating shipment object.."
|
45
|
-
shipment = Shippo::Shipment.create(
|
46
|
-
:object_purpose => 'PURCHASE',
|
47
|
-
:address_from => address_from,
|
48
|
-
:address_to => address_to,
|
49
|
-
:parcel => parcel,
|
50
|
-
:async => false )
|
51
|
-
|
52
|
-
# Get the desired rate according to your business logic
|
53
|
-
# We select the first rate in this example
|
54
|
-
rate = shipment.rates()[0]
|
55
|
-
|
56
|
-
puts "Rates generated. Purchasing a #{rate.provider} #{rate.servicelevel_name} label"
|
57
|
-
|
58
|
-
# Purchase the desired rate (create a Transaction object)
|
59
|
-
transaction = Shippo::Transaction.create(
|
60
|
-
:rate => rate["object_id"],
|
61
|
-
:async => false )
|
62
|
-
|
63
|
-
# label_url and tracking_number
|
64
|
-
if transaction.object_status == "SUCCESS"
|
65
|
-
puts "Label sucessfully generated:"
|
66
|
-
puts "label_url: #{transaction.label_url}"
|
67
|
-
puts "tracking_number: #{transaction.tracking_number}"
|
68
|
-
else
|
69
|
-
puts "Error generating label:"
|
70
|
-
puts transaction.messages
|
71
|
-
end
|
data/lib/shippo/address.rb
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
module Shippo
|
2
|
-
class Address < Resource
|
3
|
-
include Shippo::Operations::List
|
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
|
9
|
-
end
|
10
|
-
end
|
data/lib/shippo/api_object.rb
DELETED
@@ -1,89 +0,0 @@
|
|
1
|
-
module Shippo
|
2
|
-
class ApiObject < ContainerObject
|
3
|
-
include Enumerable
|
4
|
-
|
5
|
-
def initialize(id=nil)
|
6
|
-
# parameter overloading!
|
7
|
-
if id.kind_of?(Hash)
|
8
|
-
id = id[:id]
|
9
|
-
end
|
10
|
-
|
11
|
-
@values = {}
|
12
|
-
@values[:id] = id if id
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.construct_from(values)
|
16
|
-
# recursive on arrays
|
17
|
-
case values
|
18
|
-
when Array
|
19
|
-
values.map { |v| self.construct_from(v) }
|
20
|
-
when Hash
|
21
|
-
obj = self.new(values[:id])
|
22
|
-
obj.refresh_from(values)
|
23
|
-
obj
|
24
|
-
else
|
25
|
-
# on scalar types, just identity
|
26
|
-
values
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def to_s(*args)
|
31
|
-
JSON.pretty_generate @values
|
32
|
-
end
|
33
|
-
|
34
|
-
def inspect()
|
35
|
-
id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
|
36
|
-
"#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + self.to_s
|
37
|
-
end
|
38
|
-
|
39
|
-
def refresh_from(values)
|
40
|
-
values.each do |k, v|
|
41
|
-
@values[k.to_sym] = v
|
42
|
-
end
|
43
|
-
instance_eval do
|
44
|
-
add_accessors(@values.keys)
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
def [](k)
|
49
|
-
@values[k.to_sym]
|
50
|
-
end
|
51
|
-
|
52
|
-
def []=(k, v)
|
53
|
-
send(:"#{k}=", v)
|
54
|
-
end
|
55
|
-
def keys
|
56
|
-
@values.keys
|
57
|
-
end
|
58
|
-
|
59
|
-
def values
|
60
|
-
@values.values
|
61
|
-
end
|
62
|
-
|
63
|
-
def to_json(*a)
|
64
|
-
JSON.dump(@values)
|
65
|
-
end
|
66
|
-
|
67
|
-
def as_json(*a)
|
68
|
-
@values.as_json(*a)
|
69
|
-
end
|
70
|
-
|
71
|
-
def to_hash
|
72
|
-
@values
|
73
|
-
end
|
74
|
-
|
75
|
-
def each(&blk)
|
76
|
-
@values.each(&blk)
|
77
|
-
end
|
78
|
-
|
79
|
-
if RUBY_VERSION < '1.9.2'
|
80
|
-
def respond_to?(symbol)
|
81
|
-
@values.has_key?(symbol) || super
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
|
89
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
module Shippo
|
2
|
-
class ContainerObject
|
3
|
-
protected
|
4
|
-
def create_accessor(k_name, k_index)
|
5
|
-
metaclass.instance_eval do
|
6
|
-
define_method(k_name) { @values[k_index] }
|
7
|
-
define_method(:"#{k_name}=") do |v|
|
8
|
-
@values[k_index] = v unless k_index == ''
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
def add_accessors(keys)
|
13
|
-
keys.each do |k|
|
14
|
-
#TODO raise something here, should filter this before
|
15
|
-
orig_k = k
|
16
|
-
while respond_to?(k) do
|
17
|
-
k = "_#{k}".to_sym
|
18
|
-
end
|
19
|
-
create_accessor(k, orig_k)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
def metaclass
|
23
|
-
class << self
|
24
|
-
self
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
data/lib/shippo/create.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
module Shippo
|
2
|
-
module Operations
|
3
|
-
module Create
|
4
|
-
module ClassMethods
|
5
|
-
def create(params={})
|
6
|
-
params.each do |k, v|
|
7
|
-
params[k] = v[:object_id] if v.is_a?(ApiObject)
|
8
|
-
end
|
9
|
-
response = Shippo.request(:post, "#{self.url}/", params)
|
10
|
-
self.construct_from(response)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
def self.included(base)
|
14
|
-
base.extend(ClassMethods)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
data/lib/shippo/customs_item.rb
DELETED
data/lib/shippo/error.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
module Shippo
|
2
|
-
class APIError < StandardError
|
3
|
-
attr_reader :message
|
4
|
-
def initialize(message=nil)
|
5
|
-
@message = message
|
6
|
-
end
|
7
|
-
|
8
|
-
def to_s
|
9
|
-
@message
|
10
|
-
end
|
11
|
-
end
|
12
|
-
class ConnectionError < APIError
|
13
|
-
end
|
14
|
-
class MissingDataError < APIError
|
15
|
-
end
|
16
|
-
class AuthError < APIError
|
17
|
-
end
|
18
|
-
end
|
data/lib/shippo/list.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
module Shippo
|
2
|
-
module Operations
|
3
|
-
module List
|
4
|
-
module ClassMethods
|
5
|
-
# return all items
|
6
|
-
def all(params={})
|
7
|
-
response = Shippo.request(:get, "#{url}/", params)
|
8
|
-
# Limiting to results array, does not allow user to see count,..
|
9
|
-
# self.construct_from(response[:results] || [])
|
10
|
-
self.construct_from(response)
|
11
|
-
end
|
12
|
-
# return a specific item
|
13
|
-
def get(id, params={})
|
14
|
-
response = Shippo.request(:get, "#{url}/#{CGI.escape(id)}/", params)
|
15
|
-
self.construct_from(response)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
def self.included(base)
|
19
|
-
base.extend(ClassMethods)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
data/lib/shippo/manifest.rb
DELETED
data/lib/shippo/parcel.rb
DELETED
data/lib/shippo/rate.rb
DELETED
data/lib/shippo/refund.rb
DELETED
data/lib/shippo/resource.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
module Shippo
|
2
|
-
class Resource < ApiObject
|
3
|
-
@non_standard_URL
|
4
|
-
def self.class_name
|
5
|
-
self.name.split('::')[-1]
|
6
|
-
end
|
7
|
-
def self.url()
|
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
|
16
|
-
end
|
17
|
-
def url
|
18
|
-
unless id = self['object_id']
|
19
|
-
raise MissingDataError.new("#{self.class} has no object_id")
|
20
|
-
end
|
21
|
-
"#{self.class.url}/#{CGI.escape(id)}"
|
22
|
-
end
|
23
|
-
def refresh
|
24
|
-
response, api_key = Shippo.request(:get, url, @retrieve_options)
|
25
|
-
refresh_from(response)
|
26
|
-
self
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
data/lib/shippo/shipment.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
module Shippo
|
2
|
-
class Shipment < Resource
|
3
|
-
include Shippo::Operations::List
|
4
|
-
include Shippo::Operations::Create
|
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
|
11
|
-
Shippo::Rate.construct_from(response[:results])
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
data/lib/shippo/transaction.rb
DELETED
data/lib/shippo/update.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
module Shippo
|
2
|
-
module Operations
|
3
|
-
module Update
|
4
|
-
module ClassMethods
|
5
|
-
def update(object_id, params={})
|
6
|
-
response = Shippo.request(:put, "#{url}/#{CGI.escape(object_id)}/", params)
|
7
|
-
self.construct_from(response)
|
8
|
-
end
|
9
|
-
end
|
10
|
-
def self.included(base)
|
11
|
-
base.extend(ClassMethods)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
data/test/test.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
gem 'mocha'
|
3
|
-
require 'test/unit'
|
4
|
-
require 'mocha/test_unit'
|
5
|
-
require 'shippo'
|
6
|
-
def parcel_list_result
|
7
|
-
JSON::parse File.open('./parcel_list.json').read
|
8
|
-
end
|
9
|
-
|
10
|
-
module Shippo
|
11
|
-
class ParcelTest < Test::Unit::TestCase
|
12
|
-
def test_import
|
13
|
-
# get an array of test parcels from JSON
|
14
|
-
ret = Shippo::Parcel.construct_from(parcel_list_result)
|
15
|
-
# test the various options of accessing the parcel data
|
16
|
-
assert_equal ret[:metadata], "Customer ID 123456"
|
17
|
-
ret["metadata"] = "Customer ID 007"
|
18
|
-
assert_equal ret.metadata, "Customer ID 007"
|
19
|
-
assert_equal ret.object_owner, "tobias.schottdorf@gmail.com"
|
20
|
-
ret[:object_owner] = "hans.wurst@gmail.com"
|
21
|
-
assert_equal ret["object_owner"], "hans.wurst@gmail.com"
|
22
|
-
assert_equal ret.object_state, "VALID"
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|