easypost 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/easypost.rb +56 -3
- data/lib/easypost/address.rb +14 -0
- data/lib/easypost/errors/authentication_error.rb +4 -0
- data/lib/easypost/errors/easypost_error.rb +20 -0
- data/lib/easypost/postage.rb +52 -0
- metadata +38 -2
data/lib/easypost.rb
CHANGED
@@ -1,5 +1,58 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
require 'typhoeus'
|
2
|
+
require 'base64'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module EasyPost
|
6
|
+
@@api_key = "cueqNZUb3ldeWTNX7MU3Mel8UXtaAMUi"
|
7
|
+
@@api_base = 'https://www.easypost.co/api/'
|
8
|
+
|
9
|
+
def self.api_url(args={})
|
10
|
+
raise ArgumentError unless args.keys.eql?([:type, :action])
|
11
|
+
return "#{@@api_base}#{args[:type]}/#{args[:action]}"
|
4
12
|
end
|
13
|
+
|
14
|
+
def self.api_key=(api_key)
|
15
|
+
@@api_key = api_key
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.api_key
|
19
|
+
@@api_key
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.symbolize_keys(hash={})
|
23
|
+
hash.keys.each do |key|
|
24
|
+
hash[(key.to_sym rescue key) || key] = hash.delete(key)
|
25
|
+
end
|
26
|
+
return hash
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.symbolize_keys_recursive(hash={})
|
30
|
+
hash.keys.each do |key|
|
31
|
+
keysym = (key.to_sym rescue key) || key
|
32
|
+
hash[keysym] = hash.delete(key)
|
33
|
+
if hash[keysym].is_a?(Hash)
|
34
|
+
hash[keysym] = symbolize_keys_recursive(hash[keysym])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
return hash
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.get(url, params={})
|
41
|
+
params = {:userpwd => @@api_key, :params => params}
|
42
|
+
@response = Typhoeus::Request.get(url, params)
|
43
|
+
return EasyPost.symbolize_keys_recursive(JSON.parse(@response.body))
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.post(url, params={})
|
47
|
+
params = {:userpwd => @@api_key, :params => params}
|
48
|
+
@response = Typhoeus::Request.post(url, params)
|
49
|
+
return EasyPost.symbolize_keys_recursive(JSON.parse(@response.body))
|
50
|
+
end
|
51
|
+
|
5
52
|
end
|
53
|
+
|
54
|
+
require 'easypost/address'
|
55
|
+
require 'easypost/postage'
|
56
|
+
require 'easypost/errors/easypost_error'
|
57
|
+
require 'easypost/errors/authentication_error'
|
58
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module EasyPost
|
2
|
+
class Address
|
3
|
+
@@type = "address"
|
4
|
+
|
5
|
+
def self.test
|
6
|
+
puts EasyPost.api_url("verify")
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.verify(address={})
|
10
|
+
@verified_address = EasyPost.get(EasyPost.api_url(:type => @@type, :action => "verify"), :address => address)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module EasyPost
|
2
|
+
class EasyPostError < StandardError
|
3
|
+
attr_reader :message
|
4
|
+
attr_reader :http_status
|
5
|
+
attr_reader :http_body
|
6
|
+
attr_reader :json_body
|
7
|
+
|
8
|
+
def initialize(message=nil, http_status=nil, http_body=nil, json_body=nil)
|
9
|
+
@message = message
|
10
|
+
@http_status = http_status
|
11
|
+
@http_body = http_body
|
12
|
+
@json_body = json_body
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
|
17
|
+
"#{status_string}#{@message}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module EasyPost
|
2
|
+
class Postage
|
3
|
+
@@type = "postage"
|
4
|
+
@@usps_package_types = %w(
|
5
|
+
Card
|
6
|
+
Letter
|
7
|
+
Flat
|
8
|
+
Parcel
|
9
|
+
LargeParcel
|
10
|
+
IrregularParcel
|
11
|
+
FlatRateEnvelope
|
12
|
+
FlatRateLegalEnvelope
|
13
|
+
FlatRatePaddedEnvelope
|
14
|
+
FlatRateGiftCardEnvelope
|
15
|
+
FlatRateWindowEnvelope
|
16
|
+
FlatRateCardboardEnvelope
|
17
|
+
SmallFlatRateEnvelope
|
18
|
+
SmallFlatRateBox
|
19
|
+
MediumFlatRateBox
|
20
|
+
LargeFlatRateBox
|
21
|
+
RegionalRateBoxA
|
22
|
+
RegionalRateBoxB
|
23
|
+
RegionalRateBoxC
|
24
|
+
LargeFlatRateBoardGameBox
|
25
|
+
)
|
26
|
+
|
27
|
+
def self.usps_package_types
|
28
|
+
return @@usps_package_types
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.rates(data={})
|
32
|
+
@rates = EasyPost.get(EasyPost.api_url(:type => @@type, :action => "rates"), data)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.compare(data={})
|
36
|
+
return rates(data)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.buy(data={})
|
40
|
+
@rates = EasyPost.post(EasyPost.api_url(:type => @@type, :action => "buy"), data)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.get(filename)
|
44
|
+
@rates = EasyPost.get(EasyPost.api_url(:type => @@type, :action => "get"), :label_file_name => filename)
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.list()
|
48
|
+
@rates = EasyPost.get(EasyPost.api_url(:type => @@type, :action => "list"))
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easypost
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,39 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
date: 2012-10-30 00:00:00.000000000 Z
|
13
|
-
dependencies:
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: typhoeus
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: json
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
14
46
|
description: Client library for accessing the EasyPost Shipping API via Ruby
|
15
47
|
email: contact@easypost.co
|
16
48
|
executables: []
|
@@ -18,6 +50,10 @@ extensions: []
|
|
18
50
|
extra_rdoc_files: []
|
19
51
|
files:
|
20
52
|
- lib/easypost.rb
|
53
|
+
- lib/easypost/address.rb
|
54
|
+
- lib/easypost/postage.rb
|
55
|
+
- lib/easypost/errors/easypost_error.rb
|
56
|
+
- lib/easypost/errors/authentication_error.rb
|
21
57
|
homepage: https://www.easypost.co/docs
|
22
58
|
licenses: []
|
23
59
|
post_install_message:
|