auspost-drc 0.1.0

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Darcy Laycock
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.md ADDED
@@ -0,0 +1,46 @@
1
+ # auspost-drc #
2
+
3
+ A rubygem for accessing the Australia Post Delivery Rate Calculator.
4
+
5
+ Please note that in order to use this service, you must agree to the [Australia Post DRC terms and conditions](http://www.edeliver.com.au/Templates/ifs/IFS_DRC_Terms.htm) - I take no responsibility for
6
+ how you use this library, it is simply a wrapper around the DRC api.
7
+
8
+ ## Usage ##
9
+
10
+ require 'aus_post/drc'
11
+
12
+ result = AusPost::DRC.calculate(:length => 100, :height => 100, :width => 100,
13
+ :destination => 'XXXX', :pickup => 'YYYY', :country => 'AU', :service_type => :express,
14
+ :weight => 1, :quantity => 1)
15
+
16
+ The following are optional with the following defaults:
17
+
18
+ * `:quantity` - defaults to `1`
19
+ * `:service_type` - defaults to `:standard`
20
+ * `:country` - defaults to `'AU'`. As a 2 character country code
21
+
22
+ The calculator has the following methods:
23
+
24
+ - `result.charge` - charge as a float, nil on error
25
+ - `result.days` - days as integer, nil on error
26
+ - `result.charge_as_string` - charge as string, nil on error
27
+
28
+ With exception-throwing variants:
29
+
30
+ - `result.charge!` - charge as a float, throwing an error otherwise.
31
+ - `result.days!` - days as integer, throwing an error otherwise.
32
+ - `result.charge_as_string!` - charge as string, throwing an error otherwise.
33
+
34
+ ## Note on Patches/Pull Requests ##
35
+
36
+ 1. Fork the project.
37
+ 2. Make your feature addition or bug fix.
38
+ 3. Add tests for it. This is important so I don't break it in a future version unintentionally.
39
+ 4. Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
40
+ 5. Send me a pull request. Bonus points for topic branches.
41
+
42
+ ## Copyright ##
43
+
44
+ Copyright (c) 2010 Darcy Laycock. See LICENSE for details.
45
+
46
+ Australia Post and other assorted trademarks are copyright Australia Post.
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require "lib/aus_post/drc"
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "auspost-drc"
10
+ gem.summary = %Q{AusPost Delivery Rate Calculator Gem}
11
+ gem.description = %Q{Gem for AusPost Delivery Rate Calculator - Making it easy to calculate shipping}
12
+ gem.email = "sutto@sutto.net"
13
+ gem.homepage = "http://github.com/Sutto/auspost-drc"
14
+ gem.authors = ["Darcy Laycock"]
15
+ gem.version = AusPost::DRC::VERSION
16
+ gem.add_development_dependency "shoulda", ">= 0"
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/test_*.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ task :test => :check_dependencies
31
+
32
+ task :default => :test
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
37
+
38
+ rdoc.rdoc_dir = 'rdoc'
39
+ rdoc.title = "auspost_drc #{version}"
40
+ rdoc.rdoc_files.include('README*')
41
+ rdoc.rdoc_files.include('lib/**/*.rb')
42
+ end
@@ -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{auspost-drc}
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 = ["Darcy Laycock"]
12
+ s.date = %q{2010-05-07}
13
+ s.description = %q{Gem for AusPost Delivery Rate Calculator - Making it easy to calculate shipping}
14
+ s.email = %q{sutto@sutto.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "auspost-drc.gemspec",
26
+ "lib/aus_post/drc.rb",
27
+ "lib/aus_post/drc/calculator.rb",
28
+ "lib/aus_post/drc/parcel.rb",
29
+ "lib/aus_post/drc/service_type.rb",
30
+ "test/helper.rb",
31
+ "test/test_auspost_drc.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/Sutto/auspost-drc}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.6}
37
+ s.summary = %q{AusPost Delivery Rate Calculator Gem}
38
+ s.test_files = [
39
+ "test/helper.rb",
40
+ "test/test_auspost_drc.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<shoulda>, [">= 0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<shoulda>, [">= 0"])
54
+ end
55
+ end
56
+
@@ -0,0 +1,16 @@
1
+ module AusPost
2
+ module DRC
3
+
4
+ VERSION = "0.1.0".freeze
5
+ Error = Class.new(StandardError)
6
+
7
+ autoload :Parcel, 'aus_post/drc/parcel'
8
+ autoload :Calculator, 'aus_post/drc/calculator'
9
+ autoload :ServiceType, 'aus_post/drc/service_type'
10
+
11
+ def self.calculate(params = {})
12
+ Calculator.new(Parcel.new(params))
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,103 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+
4
+ module AusPost
5
+ module DRC
6
+ class Calculator
7
+
8
+ DRC_BASE_URI = "http://drc.edeliver.com.au/rateCalc.asp"
9
+
10
+ def initialize(parcel)
11
+ @parcel = parcel
12
+ @calculated = false
13
+ @charge = nil
14
+ @charge_as_string = nil
15
+ @days = nil
16
+ end
17
+
18
+ # Once off thing.
19
+ def calculate
20
+ unless @calculated
21
+ uri = URI.parse("#{DRC_BASE_URI}?#{@parcel.to_params}")
22
+ response = Net::HTTP.get(uri)
23
+ process_response response
24
+ @calculated = true
25
+ end
26
+ end
27
+
28
+ def has_charge?
29
+ calculate
30
+ !@charge.nil?
31
+ end
32
+
33
+ def has_days?
34
+ calculate
35
+ !@days.nil?
36
+ end
37
+
38
+ def charge
39
+ calculate
40
+ @charge
41
+ end
42
+
43
+ def days
44
+ calculate
45
+ @days
46
+ end
47
+
48
+ def charge_as_string
49
+ calculate
50
+ @charge_as_string
51
+ end
52
+
53
+ def charge!
54
+ value_with_exception :charge
55
+ end
56
+
57
+ def days!
58
+ value_with_exception :days
59
+ end
60
+
61
+ def charge_as_string!
62
+ value_with_exception :charge_as_string
63
+ end
64
+
65
+ protected
66
+
67
+ def process_response(r)
68
+ body = body_to_hash(r)
69
+ if body[:err_msg].to_s.upcase == "OK"
70
+ process_success_body body
71
+ else
72
+ process_error_body body[:err_msg]
73
+ end
74
+ end
75
+
76
+ def process_success_body(body)
77
+ @exception = nil
78
+ @charge = body[:charge].to_f
79
+ @days = body[:days].to_i
80
+ @charge_as_string = body[:charge]
81
+ end
82
+
83
+ def process_error_body(body)
84
+ @exception = Error.new(body)
85
+ end
86
+
87
+ def value_with_exception(name)
88
+ calculate
89
+ @exception.nil? ? send(name) : raise(@exception)
90
+ end
91
+
92
+ def body_to_hash(body)
93
+ hash = {}
94
+ body.each_line do |line|
95
+ key, value = line.strip.split("=", 2)
96
+ hash[key.to_sym] = value
97
+ end
98
+ hash
99
+ end
100
+
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,63 @@
1
+ require 'cgi'
2
+
3
+ module AusPost
4
+ module DRC
5
+ class Parcel
6
+
7
+ attr_accessor :destination, :pickup, :country, :service_type, :weight,
8
+ :length, :width, :height, :quantity
9
+
10
+ def initialize(options = {})
11
+ options.each_pair do |key, value|
12
+ real_key = :"#{key}="
13
+ send(real_key, value) if respond_to?(real_key)
14
+ end
15
+ end
16
+
17
+ def country
18
+ @country ||= 'AU'
19
+ end
20
+
21
+ def quantity
22
+ @quantity.nil? ? 1 : @quantity.to_i
23
+ end
24
+
25
+ def destination
26
+ @destination.nil? ? nil : @destination.to_s
27
+ end
28
+
29
+ def pickup
30
+ @pickup.nil? ? nil : @pickup.to_s
31
+ end
32
+
33
+ def service_type
34
+ @service_type.nil? ? 'STANDARD' : @service_type
35
+ end
36
+
37
+ def service_type=(value)
38
+ @service_type = value.to_s.strip.empty? ? nil : ServiceType[value.to_s]
39
+ end
40
+
41
+ def to_params_hash
42
+ {'Pickup_Postcode' => self.pickup,
43
+ 'Destination_Postcode' => self.destination,
44
+ 'Country' => self.country,
45
+ 'Service_Type' => self.service_type,
46
+ 'Weight' => self.weight,
47
+ 'Length' => self.length,
48
+ 'Width' => self.width,
49
+ 'Height' => self.height,
50
+ 'Quantity' => self.quantity}
51
+ end
52
+
53
+ def to_params
54
+ params = []
55
+ to_params_hash.each_pair do |key, value|
56
+ params << "#{key}=#{value}"
57
+ end
58
+ params.join("&")
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,49 @@
1
+ module AusPost
2
+ module DRC
3
+ class ServiceType
4
+
5
+ def self.mapping
6
+ @mapping ||= []
7
+ end
8
+
9
+ def self.add_mapping(short_name, code, description)
10
+ mapping << ServiceType.new(short_name, code, description)
11
+ end
12
+
13
+ def self.[](code)
14
+ item = mapping.detect { |st| st.matches?(code) }
15
+ item && item.code
16
+ end
17
+
18
+ def self.for_select
19
+ mapping.map { |st| [st.description, st.short_name.to_s] }
20
+ end
21
+
22
+ attr_accessor :code, :short_name, :description
23
+
24
+ def initialize(short_name, code, description)
25
+ @short_name = short_name.to_s
26
+ @code = code.to_s
27
+ @description = description.to_s
28
+ end
29
+
30
+ def to_s
31
+ @code
32
+ end
33
+
34
+ def matches?(text)
35
+ text = text.to_s.strip
36
+ !text.empty? && [short_name, code, description].any? { |v| v == text }
37
+ end
38
+
39
+ add_mapping :standard, 'Standard', 'Regular'
40
+ add_mapping :express, 'Express', 'Express'
41
+ add_mapping :express_platinum, 'Exp_Plt', 'Express Platinum'
42
+ add_mapping :express_courier_international_document, 'ECI_D', 'Express Courier International Document'
43
+ add_mapping :express_courier_international_merchandise, 'ECI_M', 'Express Courier International Merchandise'
44
+ add_mapping :international_air, 'Air', 'International Air'
45
+ add_mapping :international_sea, 'Sea', 'International Sea'
46
+
47
+ end
48
+ end
49
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'auspost_drc'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestAuspostDrc < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auspost-drc
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Darcy Laycock
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-07 00:00:00 +08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ description: Gem for AusPost Delivery Rate Calculator - Making it easy to calculate shipping
33
+ email: sutto@sutto.net
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - LICENSE
40
+ - README.md
41
+ files:
42
+ - .document
43
+ - .gitignore
44
+ - LICENSE
45
+ - README.md
46
+ - Rakefile
47
+ - auspost-drc.gemspec
48
+ - lib/aus_post/drc.rb
49
+ - lib/aus_post/drc/calculator.rb
50
+ - lib/aus_post/drc/parcel.rb
51
+ - lib/aus_post/drc/service_type.rb
52
+ - test/helper.rb
53
+ - test/test_auspost_drc.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/Sutto/auspost-drc
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --charset=UTF-8
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.3.6
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: AusPost Delivery Rate Calculator Gem
84
+ test_files:
85
+ - test/helper.rb
86
+ - test/test_auspost_drc.rb