has_tracking 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ test/*.log
2
+ test/*.db
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,17 @@
1
+ HasTracking
2
+ ===========
3
+
4
+ Package tracking for Posten / Bring (Norway), might implement other solutions later.
5
+
6
+ Example
7
+ =======
8
+
9
+ Add the has\_tracking to your model (if the field containing the tracking number is not named "tracking\_number" you can supply your own field with the :tracking\_number option).
10
+ <class Package < ActiveRecord::Base
11
+ has_tracking :tracking_number => "number">
12
+
13
+ <@package.track.details> will now get you the packages weight (in kg), volume (in cm3) and the height, lenght and width (in cm).
14
+ <@package.track.received?> will return true or false if package has arrived at its destination
15
+ <@package.track.events> returns an array of the shipment route so far
16
+
17
+ Copyright (c) 2009 Erlend Finvåg, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the has_tracking plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the has_tracking plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'HasTracking'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ begin
26
+ require 'jeweler'
27
+ Jeweler::Tasks.new do |gemspec|
28
+ gemspec.name = "has_tracking"
29
+ gemspec.summary = "Package tracking for Bring"
30
+ gemspec.description = "Adds quick and dirty package tracking for Bring (Posten Norway)"
31
+ gemspec.email = "erlend.finvag@gmail.com"
32
+ gemspec.homepage = "http://github.com/zircon/has_tracking"
33
+ gemspec.authors = ["Erlend Finvåg"]
34
+ gemspec.has_rdoc = false
35
+ end
36
+ rescue LoadError
37
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
38
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,56 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{has_tracking}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Erlend Finv\303\245g"]
12
+ s.date = %q{2009-11-30}
13
+ s.description = %q{Adds quick and dirty package tracking for Bring (Posten Norway)}
14
+ s.email = %q{erlend.finvag@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "MIT-LICENSE",
21
+ "README.markdown",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "has_tracking.gemspec",
25
+ "init.rb",
26
+ "install.rb",
27
+ "lib/has_tracking.rb",
28
+ "tasks/has_tracking_tasks.rake",
29
+ "test/database.yml",
30
+ "test/has_tracking_test.rb",
31
+ "test/schema.rb",
32
+ "test/test_helper.rb",
33
+ "uninstall.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/zircon/has_tracking}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.5}
39
+ s.summary = %q{Package tracking for Bring}
40
+ s.test_files = [
41
+ "test/has_tracking_test.rb",
42
+ "test/schema.rb",
43
+ "test/test_helper.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
+ else
52
+ end
53
+ else
54
+ end
55
+ end
56
+
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "has_tracking"
2
+ require "json"
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,68 @@
1
+ module HasTracking
2
+ def self.included(base)
3
+ base.send :extend, ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ def has_tracking(options = {})
8
+ cattr_accessor :tracking_number
9
+ self.tracking_number = (options[:tracking_number] || :tracking_number).to_s
10
+ send :include, InstanceMethods
11
+ end
12
+ end
13
+
14
+ module InstanceMethods
15
+ def track
16
+ return Track.new(read_attribute(self.class.tracking_number))
17
+ end
18
+ end
19
+ end
20
+
21
+ class Track
22
+ def initialize(number)
23
+ begin
24
+ require "open-uri"
25
+ buffer = open("http://sporing.bring.no/sporing.json?q=#{number}").read
26
+ @result = JSON.parse(buffer)["consignmentSet"][0]["packageSet"][0]
27
+ rescue OpenURI::HTTPError
28
+ return false
29
+ end
30
+ end
31
+
32
+ def received?
33
+ begin
34
+ if self.events.last["postalcode"].blank?
35
+ return true
36
+ else
37
+ return false
38
+ end
39
+ rescue
40
+ return false
41
+ end
42
+ end
43
+
44
+ def events
45
+ events = []
46
+ for event in @result["eventSet"]
47
+ e = {}
48
+ e["city"] = event["city"]
49
+ e["postalcode"] = event["postalCode"]
50
+ e["date"] = DateTime.parse(event["dateIso"])
51
+ e["description"] = event["description"]
52
+ events << e
53
+ end
54
+ return events.reverse!
55
+ end
56
+
57
+ def details
58
+ details = {}
59
+ details["weight"] = @result["weightInKgs"]
60
+ details["length"] = @result["lengthInCm"]
61
+ details["height"] = @result["heightInCm"]
62
+ details["width"] = @result["widthInCm"]
63
+ details["volume"] = @result["volumeInDm3"]
64
+ return details
65
+ end
66
+ end
67
+
68
+ ActiveRecord::Base.send :include, HasTracking
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :has_tracking do
3
+ # # Task goes here
4
+ # end
data/test/database.yml ADDED
@@ -0,0 +1,7 @@
1
+ sqlite:
2
+ :adapter: sqlite
3
+ :database: vendor/plugins/has_tracking/test/has_tracking_plugin.sqlite.db
4
+
5
+ sqlite3:
6
+ :adapter: sqlite3
7
+ :database: vendor/plugins/has_tracking/test/has_tracking_plugin.sqlite3.db
@@ -0,0 +1,31 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class HasTrackingTest < Test::Unit::TestCase
4
+ load_schema
5
+
6
+ class Package < ActiveRecord::Base
7
+ has_tracking :tracking_number => :number
8
+ end
9
+
10
+ def test_tracking_should_get_package_details
11
+ @tracking = Package.first.track
12
+ assert_equal 0.9, @tracking.details["weight"]
13
+ assert_equal 26, @tracking.details["length"]
14
+ assert_equal 18, @tracking.details["height"]
15
+ assert_equal 25, @tracking.details["width"]
16
+ assert_equal 11.7, @tracking.details["volume"]
17
+ end
18
+
19
+ def test_tracking_should_get_package_events
20
+ @tracking = Package.first.track
21
+ assert_equal 4, @tracking.events.length
22
+ assert_equal "OSLO", @tracking.events.first["city"]
23
+ assert_equal "0024", @tracking.events.first["postalcode"]
24
+ assert_equal DateTime, @tracking.events.first["date"].class
25
+ assert_not_equal nil, @tracking.events.first["description"]
26
+ end
27
+
28
+ def test_tracking_should_get_package_received
29
+ assert_equal true, Package.first.track.received?
30
+ end
31
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,7 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :packages, :force => true do |t|
3
+ t.string :number
4
+ end
5
+
6
+ Package.create(:number => "CH140585246NO")
7
+ end
@@ -0,0 +1,34 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+ ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
3
+
4
+ require 'test/unit'
5
+ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
6
+
7
+ def load_schema
8
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
9
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
10
+
11
+ db_adapter = ENV['DB']
12
+
13
+ # no db passed, try one of these fine config-free DBs before bombing.
14
+ db_adapter ||=
15
+ begin
16
+ require 'rubygems'
17
+ require 'sqlite'
18
+ 'sqlite'
19
+ rescue MissingSourceFile
20
+ begin
21
+ require 'sqlite3'
22
+ 'sqlite3'
23
+ rescue MissingSourceFile
24
+ end
25
+ end
26
+
27
+ if db_adapter.nil?
28
+ raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
29
+ end
30
+
31
+ ActiveRecord::Base.establish_connection(config[db_adapter])
32
+ load(File.dirname(__FILE__) + "/schema.rb")
33
+ # require File.dirname(__FILE__) + '/../rails/init.rb'
34
+ end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_tracking
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - "Erlend Finv\xC3\xA5g"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-30 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Adds quick and dirty package tracking for Bring (Posten Norway)
17
+ email: erlend.finvag@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.markdown
24
+ files:
25
+ - .gitignore
26
+ - MIT-LICENSE
27
+ - README.markdown
28
+ - Rakefile
29
+ - VERSION
30
+ - has_tracking.gemspec
31
+ - init.rb
32
+ - install.rb
33
+ - lib/has_tracking.rb
34
+ - tasks/has_tracking_tasks.rake
35
+ - test/database.yml
36
+ - test/has_tracking_test.rb
37
+ - test/schema.rb
38
+ - test/test_helper.rb
39
+ - uninstall.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/zircon/has_tracking
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.5
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Package tracking for Bring
68
+ test_files:
69
+ - test/has_tracking_test.rb
70
+ - test/schema.rb
71
+ - test/test_helper.rb