postmaster 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +57 -0
- data/Rakefile +18 -0
- data/lib/example.rb +42 -0
- data/lib/postmaster.rb +208 -0
- data/lib/postmaster/address.rb +17 -0
- data/lib/postmaster/api_operations/create.rb +16 -0
- data/lib/postmaster/api_operations/list.rb +16 -0
- data/lib/postmaster/api_resource.rb +36 -0
- data/lib/postmaster/errors/api_connection_error.rb +4 -0
- data/lib/postmaster/errors/api_error.rb +4 -0
- data/lib/postmaster/errors/authentication_error.rb +4 -0
- data/lib/postmaster/errors/invalid_request_error.rb +10 -0
- data/lib/postmaster/errors/postmaster_error.rb +20 -0
- data/lib/postmaster/json.rb +21 -0
- data/lib/postmaster/package.rb +6 -0
- data/lib/postmaster/postmaster_object.rb +172 -0
- data/lib/postmaster/shipment.rb +21 -0
- data/lib/postmaster/tracking.rb +9 -0
- data/lib/postmaster/util.rb +68 -0
- data/lib/postmaster/version.rb +3 -0
- data/postmaster.gemspec +26 -0
- data/test/test_address.rb +57 -0
- data/test/test_helper.rb +63 -0
- data/test/test_postmaster.rb +171 -0
- data/test/test_shipment.rb +107 -0
- metadata +185 -0
@@ -0,0 +1,171 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'test/unit'
|
3
|
+
require 'postmaster'
|
4
|
+
require 'shoulda'
|
5
|
+
require 'mocha'
|
6
|
+
require 'rest-client'
|
7
|
+
require 'cgi'
|
8
|
+
require 'uri'
|
9
|
+
|
10
|
+
class TestPostmasterRuby < Test::Unit::TestCase
|
11
|
+
include Mocha
|
12
|
+
|
13
|
+
context "Util" do
|
14
|
+
should "symbolize_names should convert names to symbols" do
|
15
|
+
start = {
|
16
|
+
'foo' => 'bar',
|
17
|
+
'array' => [{ 'foo' => 'bar' }],
|
18
|
+
'nested' => {
|
19
|
+
1 => 2,
|
20
|
+
:symbol => 9,
|
21
|
+
'string' => nil
|
22
|
+
}
|
23
|
+
}
|
24
|
+
finish = {
|
25
|
+
:foo => 'bar',
|
26
|
+
:array => [{ :foo => 'bar' }],
|
27
|
+
:nested => {
|
28
|
+
1 => 2,
|
29
|
+
:symbol => 9,
|
30
|
+
:string => nil
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
symbolized = Postmaster::Util.symbolize_names(start)
|
35
|
+
assert_equal(finish, symbolized)
|
36
|
+
end
|
37
|
+
|
38
|
+
should "flatten_params should convert data" do
|
39
|
+
start = {
|
40
|
+
:foo => 'bar',
|
41
|
+
:array => [
|
42
|
+
'foo',
|
43
|
+
'bar',
|
44
|
+
{ :foo => 'bar' }
|
45
|
+
],
|
46
|
+
|
47
|
+
:nested => {
|
48
|
+
:foo => 'bar',
|
49
|
+
:bar => 'foo'
|
50
|
+
}
|
51
|
+
}
|
52
|
+
finish = [
|
53
|
+
["array[]", "bar"],
|
54
|
+
["array[]", "foo"],
|
55
|
+
["array[foo]", "bar"],
|
56
|
+
["foo", "bar"],
|
57
|
+
["nested[bar]", "foo"],
|
58
|
+
["nested[foo]", "bar"]
|
59
|
+
]
|
60
|
+
|
61
|
+
flattened = Postmaster::Util.flatten_params(start)
|
62
|
+
assert_equal(finish.sort, flattened.sort)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "PostmasterObject" do
|
67
|
+
should "behave like Hash" do
|
68
|
+
obj = Postmaster::PostmasterObject.construct_from({
|
69
|
+
:param1 => "value1",
|
70
|
+
:param2 => "value2",
|
71
|
+
})
|
72
|
+
|
73
|
+
assert(obj.method_exists?(:param1))
|
74
|
+
assert(obj.method_exists?(:param2))
|
75
|
+
assert_equal('value1', obj.param1)
|
76
|
+
assert_equal('value2', obj.param2)
|
77
|
+
|
78
|
+
assert(obj.has_key?(:param1))
|
79
|
+
assert(obj.has_key?(:param2))
|
80
|
+
assert_equal('value1', obj[:param1])
|
81
|
+
assert_equal('value2', obj[:param2])
|
82
|
+
|
83
|
+
assert_equal([:param1, :param2], obj.keys.sort_by {|sym| sym.to_s})
|
84
|
+
assert_equal(['value1', 'value2'], obj.values.sort)
|
85
|
+
end
|
86
|
+
|
87
|
+
should "set and unset accessors" do
|
88
|
+
obj = Postmaster::PostmasterObject.new()
|
89
|
+
|
90
|
+
assert(!obj.method_exists?(:foo))
|
91
|
+
|
92
|
+
obj[:foo] = 'bar'
|
93
|
+
assert(obj.method_exists?(:foo))
|
94
|
+
assert_equal('bar', obj.foo)
|
95
|
+
|
96
|
+
obj.delete(:foo)
|
97
|
+
assert(!obj.method_exists?(:foo))
|
98
|
+
end
|
99
|
+
|
100
|
+
should "be serializable" do
|
101
|
+
obj = Postmaster::PostmasterObject.construct_from({
|
102
|
+
:foo => "bar",
|
103
|
+
})
|
104
|
+
|
105
|
+
assert_equal('{"foo":"bar"}', obj.to_s)
|
106
|
+
assert_equal('{"foo":"bar"}', obj.to_json)
|
107
|
+
assert(obj.inspect.include? 'JSON: {"foo":"bar"}')
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
context "API Bindings" do
|
113
|
+
setup do
|
114
|
+
@mock = mock
|
115
|
+
Postmaster.mock_rest_client = @mock
|
116
|
+
end
|
117
|
+
|
118
|
+
teardown do
|
119
|
+
Postmaster.mock_rest_client = nil
|
120
|
+
end
|
121
|
+
|
122
|
+
should "creating a new APIResource should not fetch over the network" do
|
123
|
+
@mock.expects(:get).never
|
124
|
+
c = Postmaster::Shipment.new(123)
|
125
|
+
end
|
126
|
+
|
127
|
+
should "creating a new APIResource from a hash should not fetch over the network" do
|
128
|
+
@mock.expects(:get).never
|
129
|
+
c = Postmaster::Shipment.construct_from({
|
130
|
+
:id => 123,
|
131
|
+
:param => "value"
|
132
|
+
})
|
133
|
+
end
|
134
|
+
|
135
|
+
should "not fetch if only accessing id" do
|
136
|
+
@mock.expects(:get).never
|
137
|
+
c = Postmaster::Shipment.new(123);
|
138
|
+
c.id
|
139
|
+
end
|
140
|
+
|
141
|
+
should "specifying invalid api credentials should raise an exception" do
|
142
|
+
Postmaster.api_key = "invalid"
|
143
|
+
response = test_response(test_invalid_api_key_error, 401)
|
144
|
+
assert_raises Postmaster::AuthenticationError do
|
145
|
+
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 401))
|
146
|
+
Postmaster::Shipment.retrieve(-1)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
should "AuthenticationErrors should have an http status, http body, and JSON body" do
|
151
|
+
Postmaster.api_key = "invalid"
|
152
|
+
response = test_response(test_invalid_api_key_error, 401)
|
153
|
+
begin
|
154
|
+
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 401))
|
155
|
+
Postmaster::Shipment.retrieve(-1)
|
156
|
+
rescue Postmaster::AuthenticationError => e
|
157
|
+
assert_equal(401, e.http_status)
|
158
|
+
assert_equal(true, !!e.http_body)
|
159
|
+
assert_equal(true, !!e.json_body[:error][:message])
|
160
|
+
assert_equal(test_invalid_api_key_error['error']['message'], e.json_body[:error][:message])
|
161
|
+
assert_equal('(Status 401) Invalid API Key provided: invalid', e.to_s)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
should "raise an exception if objest id is not specified" do
|
166
|
+
assert_raises Postmaster::InvalidRequestError do
|
167
|
+
Postmaster::Shipment.new().refresh
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'test/unit'
|
3
|
+
require 'postmaster'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'shoulda'
|
6
|
+
require 'mocha'
|
7
|
+
require 'rest-client'
|
8
|
+
require 'cgi'
|
9
|
+
require 'uri'
|
10
|
+
|
11
|
+
class TestShipmentRuby < Test::Unit::TestCase
|
12
|
+
include Mocha
|
13
|
+
|
14
|
+
sample_shipment = {
|
15
|
+
:to => {
|
16
|
+
:company => "ASLS",
|
17
|
+
:contact => "Joe Smith",
|
18
|
+
:line1 => "1110 Algarita Ave",
|
19
|
+
:city => "Austin",
|
20
|
+
:state => "TX",
|
21
|
+
:zip_code => "78704-4429",
|
22
|
+
:phone_no => "919-720-7941",
|
23
|
+
:country => "US"
|
24
|
+
},
|
25
|
+
:from_ => {
|
26
|
+
:company => "ASLS",
|
27
|
+
:contact => "Joe Smith",
|
28
|
+
:line1 => "1110 Algarita Ave",
|
29
|
+
:city => "Austin",
|
30
|
+
:state => "TX",
|
31
|
+
:zip_code => "78704-4429",
|
32
|
+
:phone_no => "919-720-7941",
|
33
|
+
:country => "US"
|
34
|
+
},
|
35
|
+
:carrier => "ups",
|
36
|
+
:service => "2DAY",
|
37
|
+
:package => {
|
38
|
+
:value => 55,
|
39
|
+
:weight => 1.5,
|
40
|
+
:length => 10,
|
41
|
+
:width => 6,
|
42
|
+
:height => 8,
|
43
|
+
},
|
44
|
+
:reference => "Order # 54321"
|
45
|
+
}
|
46
|
+
|
47
|
+
context "Shipment" do
|
48
|
+
|
49
|
+
should "be created" do
|
50
|
+
result = Postmaster::Shipment.create(params=sample_shipment)
|
51
|
+
assert_instance_of(Postmaster::Shipment, result)
|
52
|
+
assert(result.keys.include?(:status))
|
53
|
+
assert_equal("Processing", result[:status])
|
54
|
+
assert(result.keys.include?(:package))
|
55
|
+
assert_instance_of(Postmaster::Package, result[:package])
|
56
|
+
assert(result[:package].keys.include?(:type_))
|
57
|
+
assert_equal("CUSTOM", result[:package][:type_])
|
58
|
+
assert_instance_of(Postmaster::Address, result[:to])
|
59
|
+
assert_instance_of(Postmaster::Address, result[:from_])
|
60
|
+
end
|
61
|
+
|
62
|
+
should "be the same after retreave" do
|
63
|
+
shipment1 = Postmaster::Shipment.create(params=sample_shipment)
|
64
|
+
shipment2 = Postmaster::Shipment.retrieve(shipment1.id)
|
65
|
+
|
66
|
+
shipment1hash = shipment1.to_hash
|
67
|
+
shipment2hash = shipment2.to_hash
|
68
|
+
# label_urls can be different, so ignore it during check
|
69
|
+
shipment1hash[:package].delete(:label_url)
|
70
|
+
shipment2hash[:package].delete(:label_url)
|
71
|
+
shipment1hash[:packages][0].delete(:label_url)
|
72
|
+
shipment2hash[:packages][0].delete(:label_url)
|
73
|
+
assert_equal(shipment1hash, shipment2hash)
|
74
|
+
end
|
75
|
+
|
76
|
+
should "list" do
|
77
|
+
shipments = Postmaster::Shipment.all()
|
78
|
+
|
79
|
+
assert_kind_of(Array, shipments)
|
80
|
+
shipments.each do |shipment|
|
81
|
+
assert_instance_of(Postmaster::Shipment, shipment)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
should "track" do
|
86
|
+
shipment = Postmaster::Shipment.create(params=sample_shipment)
|
87
|
+
result = shipment.track()
|
88
|
+
|
89
|
+
assert_kind_of(Array, result)
|
90
|
+
assert(!result.empty?)
|
91
|
+
assert_instance_of(Postmaster::Tracking, result[0])
|
92
|
+
|
93
|
+
assert(result[0].keys.include?(:status))
|
94
|
+
assert(result[0].keys.include?(:history))
|
95
|
+
assert(!result[0].history.empty?)
|
96
|
+
assert_instance_of(Postmaster::TrackingHistory, result[0].history[0])
|
97
|
+
end
|
98
|
+
|
99
|
+
should "void" do
|
100
|
+
shipment = Postmaster::Shipment.create(params=sample_shipment)
|
101
|
+
result = shipment.void()
|
102
|
+
|
103
|
+
assert(result.is_a?(TrueClass))
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
end
|
metadata
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: postmaster
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Postmaster
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2013-02-13 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rest-client
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 7
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 4
|
32
|
+
version: "1.4"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: multi_json
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 31
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 0
|
47
|
+
- 4
|
48
|
+
version: 1.0.4
|
49
|
+
- - <
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
hash: 7
|
52
|
+
segments:
|
53
|
+
- 2
|
54
|
+
version: "2"
|
55
|
+
type: :runtime
|
56
|
+
version_requirements: *id002
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: mocha
|
59
|
+
prerelease: false
|
60
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
type: :development
|
70
|
+
version_requirements: *id003
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: shoulda
|
73
|
+
prerelease: false
|
74
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
type: :development
|
84
|
+
version_requirements: *id004
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
name: test-unit
|
87
|
+
prerelease: false
|
88
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
hash: 3
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
version: "0"
|
97
|
+
type: :development
|
98
|
+
version_requirements: *id005
|
99
|
+
- !ruby/object:Gem::Dependency
|
100
|
+
name: rake
|
101
|
+
prerelease: false
|
102
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
hash: 3
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
version: "0"
|
111
|
+
type: :development
|
112
|
+
version_requirements: *id006
|
113
|
+
description: Postmaster takes the pain out of sending shipments via UPS, Fedex, and USPS. Save money before you ship, while you ship, and after you ship. See https://postmaster.io for details.
|
114
|
+
email:
|
115
|
+
- support@postmaster.io
|
116
|
+
executables: []
|
117
|
+
|
118
|
+
extensions: []
|
119
|
+
|
120
|
+
extra_rdoc_files: []
|
121
|
+
|
122
|
+
files:
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- lib/example.rb
|
126
|
+
- lib/postmaster.rb
|
127
|
+
- lib/postmaster/address.rb
|
128
|
+
- lib/postmaster/api_operations/create.rb
|
129
|
+
- lib/postmaster/api_operations/list.rb
|
130
|
+
- lib/postmaster/api_resource.rb
|
131
|
+
- lib/postmaster/errors/api_connection_error.rb
|
132
|
+
- lib/postmaster/errors/api_error.rb
|
133
|
+
- lib/postmaster/errors/authentication_error.rb
|
134
|
+
- lib/postmaster/errors/invalid_request_error.rb
|
135
|
+
- lib/postmaster/errors/postmaster_error.rb
|
136
|
+
- lib/postmaster/json.rb
|
137
|
+
- lib/postmaster/package.rb
|
138
|
+
- lib/postmaster/postmaster_object.rb
|
139
|
+
- lib/postmaster/shipment.rb
|
140
|
+
- lib/postmaster/tracking.rb
|
141
|
+
- lib/postmaster/util.rb
|
142
|
+
- lib/postmaster/version.rb
|
143
|
+
- postmaster.gemspec
|
144
|
+
- test/test_address.rb
|
145
|
+
- test/test_helper.rb
|
146
|
+
- test/test_postmaster.rb
|
147
|
+
- test/test_shipment.rb
|
148
|
+
homepage: https://postmaster.io
|
149
|
+
licenses: []
|
150
|
+
|
151
|
+
post_install_message:
|
152
|
+
rdoc_options: []
|
153
|
+
|
154
|
+
require_paths:
|
155
|
+
- lib
|
156
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
157
|
+
none: false
|
158
|
+
requirements:
|
159
|
+
- - ">="
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
hash: 3
|
162
|
+
segments:
|
163
|
+
- 0
|
164
|
+
version: "0"
|
165
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
166
|
+
none: false
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
hash: 3
|
171
|
+
segments:
|
172
|
+
- 0
|
173
|
+
version: "0"
|
174
|
+
requirements: []
|
175
|
+
|
176
|
+
rubyforge_project:
|
177
|
+
rubygems_version: 1.8.24
|
178
|
+
signing_key:
|
179
|
+
specification_version: 3
|
180
|
+
summary: Library for postmaster.io service
|
181
|
+
test_files:
|
182
|
+
- test/test_address.rb
|
183
|
+
- test/test_helper.rb
|
184
|
+
- test/test_postmaster.rb
|
185
|
+
- test/test_shipment.rb
|