class ReactiveShipping::RateEstimate

Attributes

carrier[RW]
charge_items[RW]
compare_price[RW]
currency[RW]
delivery_category[RW]
delivery_date[RW]
delivery_range[RW]
description[RW]
destination[RW]
estimate_reference[RW]
expires_at[RW]
insurance_price[RW]
messages[RW]
negotiated_rate[RW]
origin[RW]
package_rates[RW]
phone_required[RW]
pickup_time[RW]
service_code[RW]
service_name[RW]
shipment_options[RW]
shipping_date[RW]
transit_days[RW]
with_excessive_length_fees[RW]

Public Class Methods

new(origin, destination, carrier, service_name, options = {}) click to toggle source
# File lib/reactive_freight/rate_estimate.rb, line 13
def initialize(origin, destination, carrier, service_name, options = {})
  self.charge_items = options[:charge_items] || []
  self.compare_price = options[:compare_price]
  self.currency = options[:currency]
  self.delivery_category = options[:delivery_category]
  self.delivery_range = options[:delivery_range]
  self.description = options[:description]
  self.estimate_reference = options[:estimate_reference]
  self.expires_at = options[:expires_at]
  self.insurance_price = options[:insurance_price]
  self.messages = options[:messages] || []
  self.negotiated_rate = options[:negotiated_rate]
  self.origin = origin
  self.destination = destination
  self.carrier = carrier
  self.service_name = service_name
  self.package_rates = if options[:package_rates]
                         options[:package_rates].map { |p| p.update(rate: Package.cents_from(p[:rate])) }
                       else
                         Array(options[:packages]).map { |p| { package: p } }
                       end
  self.phone_required = options[:phone_required]
  self.pickup_time = options[:pickup_time]
  self.service_code = options[:service_code]
  self.shipment_options = options[:shipment_options] || []
  self.shipping_date = options[:shipping_date]
  self.transit_days = options[:transit_days]
  self.total_price = options[:total_price]
  self.with_excessive_length_fees = options.dig(:with_excessive_length_fees)

  self.delivery_date = @delivery_range.last
end

Public Instance Methods

add(package, rate = nil) click to toggle source
# File lib/reactive_freight/rate_estimate.rb, line 53
def add(package, rate = nil)
  cents = Package.cents_from(rate)
  if cents.nil? && total_price.nil?
    raise ArgumentError, 'New packages must have valid rate information since this RateEstimate has no total_price set.'
  end

  @package_rates << { package: package, rate: cents }
  self
end
package_count() click to toggle source
# File lib/reactive_freight/rate_estimate.rb, line 67
def package_count
  package_rates.length
end
packages() click to toggle source
# File lib/reactive_freight/rate_estimate.rb, line 63
def packages
  package_rates.map { |p| p[:package] }
end
price()
Alias for: total_price
total_price() click to toggle source
# File lib/reactive_freight/rate_estimate.rb, line 46
def total_price
  @total_price || @package_rates.sum { |pr| pr[:rate] }
rescue NoMethodError
  raise ArgumentError, 'RateEstimate must have a total_price set, or have a full set of valid package rates.'
end
Also aliased as: price

Protected Instance Methods

compare_price=(compare_price) click to toggle source
# File lib/reactive_freight/rate_estimate.rb, line 85
def compare_price=(compare_price)
  @compare_price = compare_price ? Package.cents_from(compare_price) : nil
end
currency=(currency) click to toggle source
# File lib/reactive_freight/rate_estimate.rb, line 89
def currency=(currency)
  @currency = ActiveUtils::CurrencyCode.standardize(currency)
end
delivery_range=(delivery_range) click to toggle source
# File lib/reactive_freight/rate_estimate.rb, line 73
def delivery_range=(delivery_range)
  @delivery_range = delivery_range ? delivery_range.map { |date| date_for(date) }.compact : []
end
insurance_price=(insurance_price) click to toggle source
# File lib/reactive_freight/rate_estimate.rb, line 101
def insurance_price=(insurance_price)
  @insurance_price = Package.cents_from(insurance_price)
end
negotiated_rate=(negotiated_rate) click to toggle source
# File lib/reactive_freight/rate_estimate.rb, line 81
def negotiated_rate=(negotiated_rate)
  @negotiated_rate = negotiated_rate ? Package.cents_from(negotiated_rate) : nil
end
phone_required=(phone_required) click to toggle source
# File lib/reactive_freight/rate_estimate.rb, line 93
def phone_required=(phone_required)
  @phone_required = !!phone_required
end
shipping_date=(shipping_date) click to toggle source
# File lib/reactive_freight/rate_estimate.rb, line 97
def shipping_date=(shipping_date)
  @shipping_date = date_for(shipping_date)
end
total_price=(total_price) click to toggle source
# File lib/reactive_freight/rate_estimate.rb, line 77
def total_price=(total_price)
  @total_price = Package.cents_from(total_price)
end

Private Instance Methods

date_for(date) click to toggle source
# File lib/reactive_freight/rate_estimate.rb, line 107
def date_for(date)
  date && Date.strptime(date.to_s, '%Y-%m-%d')
rescue ArgumentError
  nil
end