dhlgm-track 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/dhlgm-track.gemspec +24 -0
- data/lib/dhlgm-track.rb +27 -0
- data/lib/dhlgm-track/client.rb +40 -0
- data/lib/dhlgm-track/configuration.rb +11 -0
- data/lib/dhlgm-track/errors.rb +35 -0
- data/lib/dhlgm-track/request/tracking.rb +21 -0
- data/lib/dhlgm-track/response/shipment_detail.rb +21 -0
- data/lib/dhlgm-track/response/track_detail.rb +19 -0
- data/lib/dhlgm-track/response/tracking.rb +51 -0
- data/lib/dhlgm-track/version.rb +3 -0
- data/spec/dhlgm-track/track_spec.rb +5 -0
- data/spec/spec_helper.rb +4 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 55f08aff64e7cde66738c31709241d06176253b7
|
4
|
+
data.tar.gz: 89dc7ed41c8f538d63c3211d3cdc20e96a231978
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 91d9f7d5f9dd0b5b52dd382a0fd13582f2e827300cae06cd2d94a82ac6fc17b9288318d22b8da92e742c932b35a2dc579ca095b10d347bb642e0f676070e93ed
|
7
|
+
data.tar.gz: 59abbb5678230b1b39d0208207ef8099d50615a0c0b6def64b087256f6f0c86f0b2667f4f47eaa0445d83a5fed55513f837f9e7f8737d65b938652a8c29b6352
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Scott Rocher
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# DHLGM::Track
|
2
|
+
|
3
|
+
Track packages through the DHL Global Mail API.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'dhlgm-track'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install dhlgm-track
|
18
|
+
|
19
|
+
## Rails Installation
|
20
|
+
|
21
|
+
Setup an initializer `config/initializers/dhlgm-track.rb' to configure the gem to use your DHL Global Mail API credentials.
|
22
|
+
|
23
|
+
DHLGM.configure do |config|
|
24
|
+
config.username = 'your_username'
|
25
|
+
config.password = 'your_password'
|
26
|
+
end
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
Tracking packages is easy. Once configured, simply call track and pass your tracking number.
|
30
|
+
|
31
|
+
DHLGM::track('GMXXXXXXXXXXXXXXXX')
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/dhlgm-track.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dhlgm-track/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "dhlgm-track"
|
8
|
+
gem.version = DHLGM::VERSION
|
9
|
+
gem.authors = ["Scott Rocher"]
|
10
|
+
gem.email = ["scottrocher@gmail.com"]
|
11
|
+
gem.description = %q{Track packages through the DHL Global Mail API}
|
12
|
+
gem.summary = %q{Request, parse, and work with DHL Global Mail API tracking events.}
|
13
|
+
gem.homepage = "https://github.com/rochers/dhlgm-track"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_runtime_dependency "nokogiri", ["~> 1.6"]
|
21
|
+
gem.add_runtime_dependency "typhoeus", ["~> 0.6"]
|
22
|
+
|
23
|
+
gem.add_development_dependency "rspec", ["~> 2.14"]
|
24
|
+
end
|
data/lib/dhlgm-track.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "dhlgm-track/client"
|
2
|
+
require "dhlgm-track/configuration"
|
3
|
+
require "dhlgm-track/version"
|
4
|
+
|
5
|
+
module DHLGM
|
6
|
+
class << self
|
7
|
+
attr_writer :configuration
|
8
|
+
|
9
|
+
def client
|
10
|
+
@client ||= Client.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def configuration
|
14
|
+
@configuration ||= Configuration.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def configure
|
18
|
+
yield(configuration)
|
19
|
+
end
|
20
|
+
|
21
|
+
def track(tracking_number)
|
22
|
+
client.request(DHLGM::Request::Tracking.new(tracking_number))
|
23
|
+
end
|
24
|
+
|
25
|
+
alias_method :config, :configuration
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'typhoeus'
|
3
|
+
|
4
|
+
require "dhlgm-track/errors"
|
5
|
+
require "dhlgm-track/request/tracking"
|
6
|
+
require "dhlgm-track/response/tracking"
|
7
|
+
|
8
|
+
module DHLGM
|
9
|
+
class Client
|
10
|
+
|
11
|
+
def request(request, &block)
|
12
|
+
response = Typhoeus::Request.get(tracking_api_url, {
|
13
|
+
timeout: DHLGM.config.timeout,
|
14
|
+
params: {
|
15
|
+
uname: DHLGM.config.username,
|
16
|
+
pass: DHLGM.config.password,
|
17
|
+
}.merge(request.api_params)
|
18
|
+
})
|
19
|
+
|
20
|
+
# Parse the request
|
21
|
+
xml = Nokogiri::XML.parse(response.body)
|
22
|
+
|
23
|
+
if (error = xml.search('error')).any?
|
24
|
+
code = error.search('code').text
|
25
|
+
description = error.search('description').text
|
26
|
+
|
27
|
+
raise Error.for_code(code).new(code, description)
|
28
|
+
end
|
29
|
+
|
30
|
+
request.response_handler(xml)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def tracking_api_url
|
36
|
+
"https://api.dhlglobalmail.com/api.cfm"
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module DHLGM
|
2
|
+
class Error < StandardError
|
3
|
+
attr_reader :code, :description
|
4
|
+
|
5
|
+
def initialize(code, description)
|
6
|
+
super(description)
|
7
|
+
@code = code
|
8
|
+
@description = description
|
9
|
+
end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def for_code(code)
|
13
|
+
case code
|
14
|
+
when '100'; UsernameNotFoundError
|
15
|
+
when '110'; PasswordNotFoundError
|
16
|
+
when '120'; TrackingNumberNotFoundError
|
17
|
+
when '130'; AuthenticationError
|
18
|
+
when '200'; ShippingSiteError
|
19
|
+
when '300'; DatabaseError
|
20
|
+
when '400'; BadTrackingNumberError
|
21
|
+
else ; Error
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class ValidationError < Error; end
|
28
|
+
class UsernameNotFoundError < ValidationError; end
|
29
|
+
class PasswordNotFoundError < ValidationError; end
|
30
|
+
class TrackingNumberNotFoundError < ValidationError; end
|
31
|
+
class AuthenticationError < Error; end
|
32
|
+
class ShippingSiteError < Error; end
|
33
|
+
class DatabaseError < Error; end
|
34
|
+
class BadTrackingNumberError < Error; end
|
35
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module DHLGM::Request
|
2
|
+
class Tracking
|
3
|
+
|
4
|
+
attr_accessor :tracking_number
|
5
|
+
|
6
|
+
def initialize(tracking_number)
|
7
|
+
@tracking_number = tracking_number
|
8
|
+
end
|
9
|
+
|
10
|
+
def api_params
|
11
|
+
{
|
12
|
+
trackingnumber: tracking_number
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
def response_handler(xml)
|
17
|
+
DHLGM::Response::Tracking.new(xml)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module DHLGM::Response
|
2
|
+
class ShipmentDetail
|
3
|
+
attr_accessor :tracking_number,
|
4
|
+
:delivery_confirm,
|
5
|
+
:customer_confirm,
|
6
|
+
:address1,
|
7
|
+
:address2,
|
8
|
+
:city,
|
9
|
+
:state,
|
10
|
+
:zip,
|
11
|
+
:weight,
|
12
|
+
:shipped
|
13
|
+
|
14
|
+
def initialize(init)
|
15
|
+
init.each_pair do |key, val|
|
16
|
+
instance_variable_set('@' + key.to_s, val)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module DHLGM::Response
|
2
|
+
class TrackDetail
|
3
|
+
attr_accessor :code,
|
4
|
+
:date,
|
5
|
+
:time,
|
6
|
+
:city,
|
7
|
+
:state,
|
8
|
+
:description,
|
9
|
+
:postalcode,
|
10
|
+
:country
|
11
|
+
|
12
|
+
def initialize(init)
|
13
|
+
init.each_pair do |key, val|
|
14
|
+
instance_variable_set('@' + key.to_s, val)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module DHLGM::Response
|
2
|
+
class Tracking
|
3
|
+
require 'dhlgm-track/response/shipment_detail'
|
4
|
+
require 'dhlgm-track/response/track_detail'
|
5
|
+
|
6
|
+
attr_accessor :tracking_events, :shipment_detail
|
7
|
+
|
8
|
+
def initialize(xml)
|
9
|
+
@shipment_detail = parse_shipment_detail(xml.search("shipment"))
|
10
|
+
@tracking_events = []
|
11
|
+
xml.search("event").each do |event|
|
12
|
+
@tracking_events << parse_tracking_detail(event)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def parse_shipment_detail(node)
|
19
|
+
DHLGM::Response::ShipmentDetail.new(
|
20
|
+
{
|
21
|
+
tracking_number: node.search('trackingnumber').text,
|
22
|
+
delivery_confirm: node.search('deliveryconfirm').text,
|
23
|
+
customer_confirm: node.search('customerconfirm').text,
|
24
|
+
address1: node.search('address1').text,
|
25
|
+
address2: node.search('address2').text,
|
26
|
+
city: node.search('city').text,
|
27
|
+
state: node.search('state').text,
|
28
|
+
zip: node.search('zip').text,
|
29
|
+
weight: node.search('weight').text,
|
30
|
+
shipped: node.search('shipped').text
|
31
|
+
}
|
32
|
+
)
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse_tracking_detail(node)
|
36
|
+
DHLGM::Response::TrackDetail.new(
|
37
|
+
{
|
38
|
+
code: node.search('code').text,
|
39
|
+
date: node.search('date').text,
|
40
|
+
time: node.search('time').text,
|
41
|
+
city: node.search('city').text,
|
42
|
+
state: node.search('state').text,
|
43
|
+
description: node.search('description').text,
|
44
|
+
postalcode: node.search('postalcode').text,
|
45
|
+
country: node.search('country').text
|
46
|
+
}
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dhlgm-track
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Scott Rocher
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: typhoeus
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.6'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.14'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.14'
|
55
|
+
description: Track packages through the DHL Global Mail API
|
56
|
+
email:
|
57
|
+
- scottrocher@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- dhlgm-track.gemspec
|
69
|
+
- lib/dhlgm-track.rb
|
70
|
+
- lib/dhlgm-track/client.rb
|
71
|
+
- lib/dhlgm-track/configuration.rb
|
72
|
+
- lib/dhlgm-track/errors.rb
|
73
|
+
- lib/dhlgm-track/request/tracking.rb
|
74
|
+
- lib/dhlgm-track/response/shipment_detail.rb
|
75
|
+
- lib/dhlgm-track/response/track_detail.rb
|
76
|
+
- lib/dhlgm-track/response/tracking.rb
|
77
|
+
- lib/dhlgm-track/version.rb
|
78
|
+
- spec/dhlgm-track/track_spec.rb
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
homepage: https://github.com/rochers/dhlgm-track
|
81
|
+
licenses: []
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.2.0
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: Request, parse, and work with DHL Global Mail API tracking events.
|
103
|
+
test_files:
|
104
|
+
- spec/dhlgm-track/track_spec.rb
|
105
|
+
- spec/spec_helper.rb
|