Class: ReactiveShipping::RateEstimate

Inherits:
Object
  • Object
show all
Defined in:
lib/reactive_freight/rate_estimate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, destination, carrier, service_name, options = {}) ⇒ RateEstimate

Returns a new instance of RateEstimate.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

Instance Attribute Details

#carrierObject

Returns the value of attribute carrier.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def carrier
  @carrier
end

#charge_itemsObject

Returns the value of attribute charge_items.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def charge_items
  @charge_items
end

#compare_priceObject

Returns the value of attribute compare_price.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def compare_price
  @compare_price
end

#currencyObject

Returns the value of attribute currency.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def currency
  @currency
end

#delivery_categoryObject

Returns the value of attribute delivery_category.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def delivery_category
  @delivery_category
end

#delivery_dateObject

Returns the value of attribute delivery_date.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def delivery_date
  @delivery_date
end

#delivery_rangeObject

Returns the value of attribute delivery_range.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def delivery_range
  @delivery_range
end

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def description
  @description
end

#destinationObject

Returns the value of attribute destination.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def destination
  @destination
end

#estimate_referenceObject

Returns the value of attribute estimate_reference.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def estimate_reference
  @estimate_reference
end

#expires_atObject

Returns the value of attribute expires_at.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def expires_at
  @expires_at
end

#insurance_priceObject

Returns the value of attribute insurance_price.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def insurance_price
  @insurance_price
end

#messagesObject

Returns the value of attribute messages.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def messages
  @messages
end

#negotiated_rateObject

Returns the value of attribute negotiated_rate.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def negotiated_rate
  @negotiated_rate
end

#originObject

Returns the value of attribute origin.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def origin
  @origin
end

#package_ratesObject

Returns the value of attribute package_rates.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def package_rates
  @package_rates
end

#phone_requiredObject

Returns the value of attribute phone_required.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def phone_required
  @phone_required
end

#pickup_timeObject

Returns the value of attribute pickup_time.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def pickup_time
  @pickup_time
end

#service_codeObject

Returns the value of attribute service_code.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def service_code
  @service_code
end

#service_nameObject

Returns the value of attribute service_name.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def service_name
  @service_name
end

#shipment_optionsObject

Returns the value of attribute shipment_options.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def shipment_options
  @shipment_options
end

#shipping_dateObject

Returns the value of attribute shipping_date.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def shipping_date
  @shipping_date
end

#transit_daysObject

Returns the value of attribute transit_days.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def transit_days
  @transit_days
end

#with_excessive_length_feesObject

Returns the value of attribute with_excessive_length_fees.



5
6
7
# File 'lib/reactive_freight/rate_estimate.rb', line 5

def with_excessive_length_fees
  @with_excessive_length_fees
end

Instance Method Details

#add(package, rate = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
# 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_countObject



67
68
69
# File 'lib/reactive_freight/rate_estimate.rb', line 67

def package_count
  package_rates.length
end

#packagesObject



63
64
65
# File 'lib/reactive_freight/rate_estimate.rb', line 63

def packages
  package_rates.map { |p| p[:package] }
end

#total_priceObject Also known as: price



46
47
48
49
50
# 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