class Spree::Calculator::Shipping::PostalService
Attributes
line_items[RW]
Public Class Methods
description()
click to toggle source
# File lib/spree/calculator/shipping/postal_service.rb, line 17 def description Spree.t(:postal_service) end
register()
click to toggle source
Calls superclass method
# File lib/spree/calculator/shipping/postal_service.rb, line 21 def register super end
Public Instance Methods
available?(package)
click to toggle source
Determine if weight or size goes over bounds.
# File lib/spree/calculator/shipping/postal_service.rb, line 41 def available?(package) package.order.variants.each do |variant| return false if item_within_bounds?(variant.weight) # 18 return false if item_oversized?(variant) end true end
compute(package)
click to toggle source
As order_or_line_items we always get line items, as calculable we have Coupon, ShippingMethod or ShippingRate.
# File lib/spree/calculator/shipping/postal_service.rb, line 51 def compute(package) @line_items ||= package.order.line_items total_price, total_weight, shipping = compute_total_price, compute_total_weight, 0 return 0.0 if total_price > preferred_max_price while total_weight > weights.last # In several packages if need be. total_weight -= weights.last shipping += prices.last end [shipping, prices[compute_index(total_weight)], handling_fee(total_price)].compact.sum end
item_oversized?(variant)
click to toggle source
# File lib/spree/calculator/shipping/postal_service.rb, line 28 def item_oversized?(variant) sizes = [ variant.width ? variant.width : 0, variant.depth ? variant.depth : 0, variant.height ? variant.height : 0 ].sort.reverse return true if sizes[0] > preferred_max_item_length # Longest side. return true if sizes[1] > preferred_max_item_width # Second longest side. false end
Private Instance Methods
compute_index(total_weight)
click to toggle source
# File lib/spree/calculator/shipping/postal_service.rb, line 78 def compute_index(total_weight) index = weights.length - 2 while index >= 0 break if total_weight > weights[index] index -= 1 end index + 1 end
compute_total_price()
click to toggle source
# File lib/spree/calculator/shipping/postal_service.rb, line 74 def compute_total_price line_items.map { |item| item.price * item.quantity }.reduce(:+) end
compute_total_weight()
click to toggle source
# File lib/spree/calculator/shipping/postal_service.rb, line 67 def compute_total_weight line_items.map do |item| weight = item.variant.weight > 0 ? item.variant.weight : preferred_default_weight item.quantity * weight end.reduce(:+) end
handling_fee(total_price)
click to toggle source
# File lib/spree/calculator/shipping/postal_service.rb, line 91 def handling_fee(total_price) preferred_handling_max < total_price ? 0 : preferred_handling_fee end
item_within_bounds?(weight)
click to toggle source
# File lib/spree/calculator/shipping/postal_service.rb, line 87 def item_within_bounds?(weight) weight && weight > preferred_max_item_weight end
prices()
click to toggle source
# File lib/spree/calculator/shipping/postal_service.rb, line 95 def prices @prices ||= preferred_price_table.split.map(&:to_f) end
weights()
click to toggle source
# File lib/spree/calculator/shipping/postal_service.rb, line 99 def weights @weights ||= preferred_weight_table.split.map(&:to_f) end