polar_express 0.0.1.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in polar_express.gemspec
4
+ gemspec
5
+ gem 'nokogiri', '~> 1.5.6'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 José Tomás Albornoz
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.
@@ -0,0 +1,46 @@
1
+ # Polar Express
2
+
3
+ Get on board of The Polar Express and let's have a train ride to package tracking!
4
+
5
+ ## Gem in development
6
+ Note: this gem is in current development, so don't expect anything to work yet (not even the usage example!!!)
7
+
8
+ Feel free to contribute to the project :)
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'polar_express'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install polar_express
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ @tracker = PolarExpress.new('DHL', '017219678663')
28
+ info = @tracker.track!
29
+ info.status # => :delivered
30
+ ```
31
+
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
40
+
41
+
42
+ ### What can I help with?
43
+ - work on DHL implementation
44
+ - write more tests
45
+ - documentation. rdoc?
46
+ - discuss about the structure of the gem. is there space for improvement?
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
@@ -0,0 +1,12 @@
1
+ require "polar_express/version"
2
+ require "polar_express/polar_express"
3
+ require "polar_express/tracker"
4
+ require "polar_express/dhl"
5
+ require "polar_express/shipping_info"
6
+
7
+ module PolarExpress
8
+ # I don't really know if doing the requires in here helps to keep them under this scope.
9
+ require 'open-uri'
10
+ require 'nokogiri'
11
+ require 'date'
12
+ end
@@ -0,0 +1,52 @@
1
+ require 'pp'
2
+ module PolarExpress
3
+ module DHL
4
+ class Tracker
5
+ attr_accessor :number
6
+ def initialize(number)
7
+ @number = number
8
+ end
9
+ def track!
10
+ info = ShippingInfo.new(@number)
11
+ info.percentage = package_percentage
12
+ info.statuses = timeline
13
+ info
14
+ end
15
+ private
16
+ def tracking_url
17
+ "http://nolp.dhl.de/nextt-online-public/set_identcodes.do?lang=en&idc=#{@number}"
18
+ end
19
+ def page
20
+ @page ||= Nokogiri::HTML open tracking_url
21
+ end
22
+ def package_percentage
23
+ page.css("#progress-#{@number}_1").text.scan(/([\d]+)/).flatten.first.to_i
24
+ end
25
+ def timeline
26
+ page.css("tr#toggle-#{@number}_1 table tbody tr").map do |tr|
27
+ {
28
+ date: DateTime.parse(tr.css('td.overflow').text.strip),
29
+ city: tr.css('td.location').text.strip,
30
+ status: text_to_status(tr.css('td.status').text.strip)
31
+ }
32
+ end
33
+ end
34
+ def text_to_status(text)
35
+ case text
36
+ when 'The shipment has been successfully delivered'
37
+ :delivered
38
+ when "The shipment has been loaded onto the delivery vehicle"
39
+ :in_delivery_vehicle
40
+ when "The shipment has been processed in the destination parcel center"
41
+ :destination_city
42
+ when "The shipment has been processed in the parcel center of origin"
43
+ :origin_city
44
+ when "The instruction data for this shipment have been provided by the sender to DHL electronically"
45
+ :shipping_instructions_received
46
+ else
47
+ :other
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,7 @@
1
+ module PolarExpress
2
+ # let's have the module behave like a class
3
+ # http://stackoverflow.com/questions/318850/private-module-methods-in-ruby
4
+ def self.new(*args)
5
+ Tracker.new(*args)
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ module PolarExpress
2
+ class ShippingInfo
3
+ attr_accessor :percentage, :shipping_number, :statuses
4
+ def initialize(number = nil)
5
+ @shipping_number = number
6
+ @statuses = []
7
+ end
8
+ def status
9
+ statuses.last[:status] if statuses.length > 0
10
+ end
11
+ def last_event_at
12
+ statuses.last[:date] if statuses.length > 0
13
+ end
14
+ def last_event_city
15
+ statuses.last[:city] if statuses.length > 0
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ module PolarExpress
2
+ class Tracker
3
+ attr_reader :courier, :shipping_number
4
+ def initialize(courier, shipping_number)
5
+ @courier = identify_courier(courier)
6
+ @shipping_number = shipping_number
7
+ @courier_tracker = Object.const_get("PolarExpress::#{@courier}::Tracker").new(@shipping_number)
8
+ end
9
+ def track!
10
+ @courier_tracker.track!
11
+ end
12
+
13
+ private
14
+ # returns a courier's symbol given it's name.
15
+ # ideally it should be much smarter, supporting company's name, etc.
16
+ def identify_courier(name)
17
+ case name.upcase
18
+ when 'DHL'
19
+ :DHL
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module PolarExpress
2
+ VERSION = "0.0.1.alpha"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'polar_express/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "polar_express"
8
+ spec.version = PolarExpress::VERSION
9
+ spec.authors = ["José Tomás Albornoz"]
10
+ spec.email = ["jojo@eljojo.net"]
11
+ spec.description = %q{Package tracking done right.}
12
+ spec.summary = %q{An easy an fun way of tracking packages. info = PolarExpress.new('DHL', '123456789').track!}
13
+ spec.homepage = "https://github.com/eljojo/polar_express"
14
+ spec.license = "mit"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3.0.pre"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "shoulda"
25
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+ describe PolarExpress do
3
+ context 'Gem Basics' do
4
+ it "creates a new instance of the gem" do
5
+ @tracker = PolarExpress.new('DHL', '017219678663')
6
+ @tracker.shipping_number.should == '017219678663'
7
+ end
8
+ end
9
+ context 'DHL' do
10
+ before do
11
+ @tracker = PolarExpress.new('DHL', '017219678663')
12
+ end
13
+ it "recognizes DHL" do
14
+ @tracker.courier.should eq :DHL
15
+ end
16
+ it "tracks DHL" do
17
+ info = @tracker.track!
18
+ info.status.should eq :delivered
19
+ end
20
+ end
21
+ end
@@ -0,0 +1 @@
1
+ require 'polar_express'
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polar_express
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.alpha
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - José Tomás Albornoz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.3.0.pre
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.3.0.pre
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: shoulda
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Package tracking done right.
79
+ email:
80
+ - jojo@eljojo.net
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - lib/polar_express.rb
92
+ - lib/polar_express/dhl.rb
93
+ - lib/polar_express/polar_express.rb
94
+ - lib/polar_express/shipping_info.rb
95
+ - lib/polar_express/tracker.rb
96
+ - lib/polar_express/version.rb
97
+ - polar_express.gemspec
98
+ - spec/polar_express/polar_express_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: https://github.com/eljojo/polar_express
101
+ licenses:
102
+ - mit
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - '>'
117
+ - !ruby/object:Gem::Version
118
+ version: 1.3.1
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.25
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: An easy an fun way of tracking packages. info = PolarExpress.new('DHL', '123456789').track!
125
+ test_files:
126
+ - spec/polar_express/polar_express_spec.rb
127
+ - spec/spec_helper.rb