spree_temando 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
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,71 @@
1
+ # SpreeTemando
2
+
3
+ `spree_temando` adds support to Spree to calculate a shipping quote
4
+ using [Temando](https://www.temando.com).
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'spree_temando'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+ $ rake spree_temando:install:migrations
16
+ $ rake db:migrate
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install spree_temando
21
+
22
+ Finally, in an initializer, set up your authentication keys :
23
+
24
+ ```ruby
25
+ Temando::Api::Base.config.username = ENV['TEMANDO_USERNAME']
26
+ Temando::Api::Base.config.password = ENV['TEMANDO_PASSWORD']
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ The extension adds a new Shipping calculator.
32
+
33
+ Add a new Shipping Method from the Spree Admin (under Configuration >
34
+ Shipping Methods), and you can set the relevant calculator type to
35
+ `Temando`.
36
+
37
+ There are a few options you must fill out :
38
+
39
+ * `Origin Suburb` - the suburb you will be shipping from.
40
+ * `Origin Postcode` - the postcode you will be shipping from.
41
+ * `Express` - select this to find the fastest & cheapest shipping quote.
42
+
43
+ It is possible to create multiple calculators with different settings -
44
+ for example, a "Regular" method with Express disabled, and an "Express"
45
+ method with Express enabled.
46
+
47
+ In the calculations, the extension always attempts to find the cheapest
48
+ quote, but when `Express` is selected, only the quotes with the smallest
49
+ `maximum_eta` are considered.
50
+
51
+ ## Notes
52
+
53
+ The extension only makes requests to the Temando API when the order
54
+ changes in some way - either address details, line items, or the like.
55
+ This ensures that a customer going back and forth between the Address
56
+ and Delivery pages does not bombard the Temando API with spurious
57
+ requests.
58
+
59
+ However, each time the calculator is used *does* cause another request.
60
+ For example, the example of two shipping methods above will cause two
61
+ requests to the API for the relevant quotes, even though they expect the
62
+ same data.
63
+
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'SpreeTemando'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ require 'rake/testtask'
31
+
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib'
34
+ t.libs << 'test'
35
+ t.pattern = 'test/**/*_test.rb'
36
+ t.verbose = false
37
+ end
38
+
39
+
40
+ task :default => :test
@@ -0,0 +1,4 @@
1
+ module SpreeTemando
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module SpreeTemando
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,88 @@
1
+ module Spree
2
+ class Calculator::TemandoQuote < Calculator
3
+ # TODO: Add option to select insured
4
+
5
+ preference :origin_suburb, :string
6
+ preference :origin_postcode, :string
7
+ preference :express, :boolean
8
+
9
+ attr_accessible :preferred_origin_suburb, :preferred_origin_postcode, :preferred_express
10
+
11
+ def self.description
12
+ I18n.t(:temando)
13
+ end
14
+
15
+ def self.available?(object)
16
+ true
17
+ end
18
+
19
+ def cheapest(destination, line_items)
20
+ quotes = find_quotes(destination, line_items)
21
+ cheapest = quotes.sort_by { |q| q.total_price }.first
22
+ { :price => cheapest.total_price, :minimum_eta => cheapest.minimum_eta, :maximum_eta => cheapest.maximum_eta, :quote => cheapest }
23
+ end
24
+
25
+ def fastest(destination, line_items)
26
+ quotes = find_quotes(destination, line_items)
27
+ fastest_eta = quotes.collect(&:maximum_eta).min
28
+ cheapest = quotes.reject { |q| q.maximum_eta > fastest_eta }.sort_by { |q| q.total_price }.first
29
+ { :price => cheapest.total_price, :minimum_eta => cheapest.minimum_eta, :maximum_eta => cheapest.maximum_eta, :quote => cheapest }
30
+ end
31
+
32
+ def temando_origin
33
+ Temando::Location.new(:suburb => preferred_origin_suburb, :postcode => preferred_origin_postcode, :country => 'AU')
34
+ end
35
+
36
+ def compute(object)
37
+ if object.is_a?(Spree::Shipment) && object.temando_quotes.blank? then
38
+ object.temando_quotes = object.order.temando_quotes
39
+ end
40
+
41
+ existing_quote = object.temando_quotes.find_by_calculator_id(self.id)
42
+
43
+ if existing_quote.blank? || existing_quote.outdated? then
44
+ Spree::Order.transaction do
45
+ destination = case object
46
+ when Spree::Order
47
+ object.shipping_address
48
+ when Spree::Shipment
49
+ object.address
50
+ else
51
+ raise "Unknown object: #{object.inspect}"
52
+ end
53
+
54
+ if preferred_express then
55
+ data = fastest(destination.to_temando_location, object.line_items)
56
+ else
57
+ data = cheapest(destination.to_temando_location, object.line_items)
58
+ end
59
+
60
+ quote = Spree::TemandoQuote.new_or_update_from_quote(self, object, data[:quote], destination)
61
+
62
+ # Store the Quote data against the Order and these LineItems if they are persisted
63
+ if object.persisted? then
64
+ quote.save!
65
+
66
+ object.line_items
67
+ end
68
+
69
+ existing_quote = quote
70
+ end
71
+ end
72
+
73
+ existing_quote.total_price
74
+ end
75
+
76
+ private
77
+ def find_quotes(destination, line_items)
78
+ delivery = Temando::Delivery::DoorToDoor.new(self.temando_origin, destination)
79
+
80
+ request = Temando::Request.new
81
+ line_items.reject { |i| i.quantity < 1 }.each do |item|
82
+ request.items << item.to_temando_item
83
+ end
84
+
85
+ request.quotes_for(delivery)
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,49 @@
1
+ require 'digest/sha1'
2
+
3
+ module Spree
4
+ # TemandoQuote object is a representation of the selected Quote provided by
5
+ # the Temando API.
6
+ class TemandoQuote < ActiveRecord::Base
7
+ belongs_to :order
8
+ belongs_to :shipment
9
+ belongs_to :address
10
+ belongs_to :calculator
11
+
12
+ def outdated?
13
+ self.current_items_hash != self.cached_items_hash
14
+ end
15
+
16
+ def calculator_hash
17
+ Digest::SHA1.hexdigest(self.calculator.preferences.to_s)
18
+ end
19
+
20
+ def address_hash
21
+ Digest::SHA1.hexdigest(self.address.attributes.to_s)
22
+ end
23
+
24
+ def current_items_hash
25
+ data = (shipment || order).line_items.order(:variant_id).collect { |li| [ li.variant_id, li.quantity ].join(',') }.join(';')
26
+ Digest::SHA1.hexdigest(calculator_hash + address_hash + data)
27
+ end
28
+
29
+ def self.new_or_update_from_quote(calculator, object, quote, address)
30
+ q = object.temando_quotes.find_by_calculator_id(calculator.id) || object.temando_quotes.new
31
+ q.calculator = calculator
32
+
33
+ if object.is_a?(Spree::Order) then
34
+ q.order = object
35
+ else
36
+ q.shipment = object
37
+ end
38
+
39
+ [ :total_price, :tax, :currency, :minimum_eta, :maximum_eta, :name, :base_price, :guaranteed_eta, :carrier_id ].each do |field|
40
+ q.send("#{field}=".to_sym, quote.send(field))
41
+ end
42
+
43
+ q.address = address
44
+ q.cached_items_hash = q.current_items_hash
45
+
46
+ q
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,22 @@
1
+ module SpreeTemando
2
+ module AddressDecorator
3
+ def to_temando_location
4
+ location = Temando::Location.new
5
+ location.postcode = self.zipcode
6
+ location.suburb = self.city
7
+ location.contact = "#{self.firstname} #{self.lastname}"
8
+ location.state = self.state.name
9
+ location.street = [ self.address1, self.address2 ].compact.join(' , ')
10
+ location.phone1 = self.phone
11
+ location.phone2 = self.alternative_phone
12
+ location.company = self.company
13
+
14
+ raise 'Temando only ships to Australia' unless self.country.try(:name) == 'Australia'
15
+ location.country = 'AU'
16
+
17
+ location
18
+ end
19
+
20
+ end
21
+ end
22
+ Spree::Address.send(:include, SpreeTemando::AddressDecorator)
@@ -0,0 +1,10 @@
1
+ module SpreeTemando
2
+ module LineItemShippingDecorator
3
+ def to_temando_item
4
+ item = self.variant.to_temando_item
5
+ item.quantity = self.quantity
6
+ item
7
+ end
8
+ end
9
+ end
10
+ Spree::LineItem.send(:include, SpreeTemando::LineItemShippingDecorator)
@@ -0,0 +1,3 @@
1
+ Spree::Order.class_eval do
2
+ has_many :temando_quotes
3
+ end
@@ -0,0 +1,3 @@
1
+ Spree::Shipment.class_eval do
2
+ has_many :temando_quotes
3
+ end
@@ -0,0 +1,16 @@
1
+ module SpreeTemando
2
+ module VariantShippingDecorator
3
+ def to_temando_item
4
+ item = Temando::Item::GeneralGoods.new
5
+ # NOTE: All the distances in Temando are in metres
6
+ item.height = (self.height / 100.0)
7
+ item.length = (self.depth / 100.0)
8
+ item.width = (self.width / 100.0)
9
+ item.weight = self.weight
10
+ item.quantity = 1
11
+ item.description = self.name
12
+ item
13
+ end
14
+ end
15
+ end
16
+ Spree::Variant.send(:include, SpreeTemando::VariantShippingDecorator)
@@ -0,0 +1,2 @@
1
+ en:
2
+ temando: Temando
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ SpreeTemando::Engine.routes.draw do
2
+ end
@@ -0,0 +1,25 @@
1
+ class CreateSpreeTemandoQuotes < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :spree_temando_quotes do |t|
4
+ t.decimal :total_price, :scale => 2, :precision => 6
5
+ t.decimal :base_price, :scale => 2, :precision => 6
6
+ t.decimal :tax, :scale => 2, :precision => 6
7
+ t.string :currency
8
+ t.integer :minimum_eta
9
+ t.integer :maximum_eta
10
+
11
+ t.string :name
12
+
13
+ t.string :delivery_method
14
+ t.boolean :guaranteed_eta
15
+ t.string :carrier_id
16
+
17
+ t.timestamps
18
+ end
19
+
20
+ end
21
+
22
+ def self.down
23
+ drop_table :spree_temando_quotes
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ class AddOrdersTemandoQuoteId < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :spree_orders, :temando_quote_id, :integer
4
+ end
5
+
6
+ def self.down
7
+ remove_column :spree_orders, :temando_quote_id
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class AddTemandoQuoteAddressId < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :spree_temando_quotes, :address_id, :integer
4
+ end
5
+
6
+ def self.down
7
+ remove_column :spree_temando_quotes, :address_id
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class AddLineItemsTemandoQuoteId < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :spree_line_items, :temando_quote_id, :integer
4
+ end
5
+
6
+ def self.down
7
+ remove_column :spree_line_items, :temando_quote_id
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class AddShipmentTemandoQuoteId < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :spree_shipments, :temando_quote_id, :integer
4
+ end
5
+
6
+ def self.down
7
+ remove_column :spree_shipments, :temando_quote_id
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class AddCachedItemsHashToTemandoQuote < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :spree_temando_quotes, :cached_items_hash, :string
4
+ end
5
+
6
+ def self.down
7
+ remove_column :spree_temando_quotes, :cached_items_hash
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class RemoveTemandoQuoteDeliveryMethod < ActiveRecord::Migration
2
+ def self.up
3
+ remove_column :spree_temando_quotes, :delivery_method
4
+ end
5
+
6
+ def self.down
7
+ add_column :spree_temando_quotes, :delivery_method, :string
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ class FixTemandoQuoteAssociations < ActiveRecord::Migration
2
+ def self.up
3
+ Spree::TemandoQuote.delete_all
4
+
5
+ add_column :spree_temando_quotes, :calculator_id, :integer, :null => false
6
+ add_column :spree_temando_quotes, :order_id, :integer
7
+ add_column :spree_temando_quotes, :shipment_id, :integer
8
+
9
+ remove_column :spree_orders, :temando_quote_id
10
+ remove_column :spree_shipments, :temando_quote_id
11
+ end
12
+
13
+ def self.down
14
+ raise ActiveRecord::IrreversableMigration
15
+ end
16
+ end
@@ -0,0 +1,4 @@
1
+ require "spree_temando/engine"
2
+
3
+ module SpreeTemando
4
+ end
@@ -0,0 +1,21 @@
1
+ module SpreeTemando
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace SpreeTemando
4
+
5
+ def self.activate
6
+ Dir.glob(File.join(File.dirname(__FILE__), "../../app/overrides/*.rb")) do |c|
7
+ Rails.application.config.cache_classes ? require(c) : load(c)
8
+ end
9
+ Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator*.rb")) do |c|
10
+ Rails.configuration.cache_classes ? require(c) : load(c)
11
+ end
12
+ Temando::Api::Base.logger = Rails.logger
13
+ end
14
+
15
+ initializer "spree_temando.register.shipping_calculators" do |app|
16
+ app.config.spree.calculators.shipping_methods << Spree::Calculator::TemandoQuote
17
+ end
18
+
19
+ config.to_prepare &method(:activate).to_proc
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module SpreeTemando
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :spree_temando do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spree_temando
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Stirk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: spree_core
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.1
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: 1.1.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 3.2.3
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: 3.2.3
46
+ - !ruby/object:Gem::Dependency
47
+ name: temando
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.1.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.11.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: 2.11.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: faker
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Adds temando shipping support to Spree
95
+ email:
96
+ - jason@reinteractive.net
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - app/controllers/spree_temando/application_controller.rb
102
+ - app/helpers/spree_temando/application_helper.rb
103
+ - app/models/spree/calculator/temando_quote.rb
104
+ - app/models/spree/temando_quote.rb
105
+ - app/models/spree_temando/address_decorator.rb
106
+ - app/models/spree_temando/line_item_shipping_decorator.rb
107
+ - app/models/spree_temando/order_shipping_decorator.rb
108
+ - app/models/spree_temando/shipment_shipping_decorator.rb
109
+ - app/models/spree_temando/variant_shipping_decorator.rb
110
+ - config/locales/en.yml
111
+ - config/routes.rb
112
+ - db/migrate/20121011082400_create_spree_temando_quotes.rb
113
+ - db/migrate/20121011083400_add_orders_temando_quote_id.rb
114
+ - db/migrate/20121011084600_add_temando_quote_address_id.rb
115
+ - db/migrate/20121011084900_add_line_items_temando_quote_id.rb
116
+ - db/migrate/20121011092000_add_shipment_temando_quote_id.rb
117
+ - db/migrate/20121011104200_add_cached_items_hash_to_temando_quote.rb
118
+ - db/migrate/20121011115000_remove_temando_quote_delivery_method.rb
119
+ - db/migrate/20121017165000_fix_temando_quote_associations.rb
120
+ - lib/spree_temando/engine.rb
121
+ - lib/spree_temando/version.rb
122
+ - lib/spree_temando.rb
123
+ - lib/tasks/spree_temando_tasks.rake
124
+ - MIT-LICENSE
125
+ - Rakefile
126
+ - README.md
127
+ homepage: http://github.com/reInteractive/spree_temando
128
+ licenses: []
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ! '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ! '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 1.8.19
148
+ signing_key:
149
+ specification_version: 3
150
+ summary: Adds temando shipping support to Spree
151
+ test_files: []