Class: FreightKit::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/freight_kit/package.rb

Constant Summary collapse

VALID_FREIGHT_CLASSES =
[55, 60, 65, 70, 77.5, 85, 92.5, 100, 110, 125, 150, 175, 200, 250, 300, 400].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total_grams_or_ounces, dimensions, packaging_type, options = {}) ⇒ Package

Package.new(100, [10, 20, 30], ‘pallet’, :units => :metric) Package.new(Measured::Weight.new(100, :g), ‘box’, [10, 20, 30].map {|m| Length.new(m, :centimetres)}) Package.new(100.grams, [10, 20, 30].map(&:centimetres))

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/freight_kit/package.rb', line 34

def initialize(total_grams_or_ounces, dimensions, packaging_type, options = {})
  options = @@default_options.update(options) if @@default_options
  options.symbolize_keys!
  @options = options

  raise ArgumentError, 'Package#new: packaging_type is required' unless packaging_type
  raise ArgumentError, 'Package#new: quantity is required' unless options[:quantity]

  @dimensions = if dimensions.is_a?(Hash)
                  [
                    dimensions[:height].presence,
                    dimensions[:width].presence,
                    dimensions[:length].presence,
                  ]
                else
                  dimensions
                end

  @description = options[:description]
  @hazmat = options[:hazmat] == true
  @nmfc = options[:nmfc].presence

  imperial = (options[:units] == :imperial)

  weight_imperial = dimensions_imperial = imperial if options.include?(:units)

  weight_imperial = (options[:weight_units] == :imperial) if options.include?(:weight_units)

  dimensions_imperial = (options[:dim_units] == :imperial) if options.include?(:dim_units)

  @weight_unit_system = weight_imperial ? :imperial : :metric
  @dimensions_unit_system = dimensions_imperial ? :imperial : :metric

  @quantity = options[:quantity] || 1

  @total_weight = attribute_from_metric_or_imperial(
    total_grams_or_ounces,
    Measured::Weight,
    @weight_unit_system,
    :grams,
    :ounces,
  )

  @each_weight = attribute_from_metric_or_imperial(
    total_grams_or_ounces / @quantity.to_f,
    Measured::Weight,
    @weight_unit_system,
    :grams,
    :ounces,
  )

  if @dimensions.blank?
    zero_length = Measured::Length.new(0, (dimensions_imperial ? :inches : :centimetres))
    @dimensions = [zero_length] * 3
  else
    # Overriding ReactiveShipping's protected process_dimensions which sorts
    # them making it confusing for ReactiveFreight carrier API's that expect
    # the H x W x L order. Since H x W x L is nonstandard in the freight
    # industry ReactiveFreight introduces explicit functions for each
    @dimensions = @dimensions.map do |l|
      attribute_from_metric_or_imperial(l, Measured::Length, @dimensions_unit_system, :centimetres, :inches)
    end
    2.downto(@dimensions.length) do |_n|
      @dimensions.unshift(@dimensions[0])
    end
  end

  @value = Package.cents_from(options[:value])
  @currency = options[:currency] || (options[:value].currency if options[:value].respond_to?(:currency))
  @cylinder = options[:cylinder] || options[:tube] ? true : false
  @gift = options[:gift] ? true : false
  @oversized = options[:oversized] ? true : false
  @unpackaged = options[:unpackaged] ? true : false
  @packaging = Packaging.new(packaging_type)
end

Instance Attribute Details

#currencyObject (readonly)

Returns the value of attribute currency.



28
29
30
# File 'lib/freight_kit/package.rb', line 28

def currency
  @currency
end

#declared_freight_classObject



137
138
139
# File 'lib/freight_kit/package.rb', line 137

def declared_freight_class
  @declared_freight_class || @options[:declared_freight_class]
end

#descriptionObject

Returns the value of attribute description.



27
28
29
# File 'lib/freight_kit/package.rb', line 27

def description
  @description
end

#hazmatObject

Returns the value of attribute hazmat.



27
28
29
# File 'lib/freight_kit/package.rb', line 27

def hazmat
  @hazmat
end

#nmfcObject

Returns the value of attribute nmfc.



27
28
29
# File 'lib/freight_kit/package.rb', line 27

def nmfc
  @nmfc
end

#optionsObject (readonly)

Returns the value of attribute options.



28
29
30
# File 'lib/freight_kit/package.rb', line 28

def options
  @options
end

#packagingObject (readonly)

Returns the value of attribute packaging.



28
29
30
# File 'lib/freight_kit/package.rb', line 28

def packaging
  @packaging
end

#quantityObject

Returns the value of attribute quantity.



27
28
29
# File 'lib/freight_kit/package.rb', line 27

def quantity
  @quantity
end

#valueObject (readonly)

Returns the value of attribute value.



28
29
30
# File 'lib/freight_kit/package.rb', line 28

def value
  @value
end

Class Method Details

.cents_from(money) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/freight_kit/package.rb', line 6

def cents_from(money)
  return if money.nil?

  if money.respond_to?(:cents)
    money.cents
  else
    case money
    when Float
      (money * 100).round
    when String
      money =~ /\./ ? (money.to_f * 100).round : money.to_i
    else
      money.to_i
    end
  end
end

Instance Method Details

#calculated_freight_classObject



133
134
135
# File 'lib/freight_kit/package.rb', line 133

def calculated_freight_class
  sanitized_freight_class(density_to_freight_class(density))
end

#centimetres(measurement = nil) ⇒ Object Also known as: cm



207
208
209
210
# File 'lib/freight_kit/package.rb', line 207

def centimetres(measurement = nil)
  @centimetres ||= @dimensions.map { |m| m.convert_to(:cm).value.to_f }
  measurement.nil? ? @centimetres : measure(measurement, @centimetres)
end

#cubic_ft(each_or_total) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/freight_kit/package.rb', line 110

def cubic_ft(each_or_total)
  q = case each_or_total
      when :each then 1
      when :total then @quantity
      else
        raise ArgumentError, 'each_or_total must be one of :each, :total'
      end

  return unless inches[..2].all?(&:present?)

  cubic_ft = (inches[0] * inches[1] * inches[2]).to_f / 1728
  cubic_ft *= q

  format('%0.2f', cubic_ft).to_f
end

#cylinder?Boolean Also known as: tube?

Returns:

  • (Boolean)


157
158
159
# File 'lib/freight_kit/package.rb', line 157

def cylinder?
  @cylinder
end

#densityObject



126
127
128
129
130
131
# File 'lib/freight_kit/package.rb', line 126

def density
  return unless inches[..2].all?(&:present?) && pounds(:each)

  density = pounds(:each).to_f / cubic_ft(:each)
  format('%0.2f', density).to_f
end

#dim_weightObject



213
214
215
216
217
# File 'lib/freight_kit/package.rb', line 213

def dim_weight
  return if inches(:length).blank? || inches(:width).blank? || inches(:height).blank? || pounds(:each).blank?

  @dim_weight ||= (inches(:length).ceil * inches(:width).ceil * inches(:height).ceil).to_f / 139
end

#each_weight(options = {}) ⇒ Object



219
220
221
# File 'lib/freight_kit/package.rb', line 219

def each_weight(options = {})
  weight(@each_weight, options)
end

#freight_classObject



141
142
143
# File 'lib/freight_kit/package.rb', line 141

def freight_class
  declared_freight_class.presence || calculated_freight_class
end

#gift?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/freight_kit/package.rb', line 171

def gift?
  @gift
end

#grams(options = {}) ⇒ Object Also known as: g



184
185
186
# File 'lib/freight_kit/package.rb', line 184

def grams(options = {})
  weight(options).convert_to(:g).value.to_f
end

#hazmat?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/freight_kit/package.rb', line 175

def hazmat?
  @hazmat
end

#height(unit) ⇒ Object



153
154
155
# File 'lib/freight_kit/package.rb', line 153

def height(unit)
  @dimensions[0].convert_to(unit).value.to_f
end

#inches(measurement = nil) ⇒ Object Also known as: in



201
202
203
204
# File 'lib/freight_kit/package.rb', line 201

def inches(measurement = nil)
  @inches ||= @dimensions.map { |m| m.convert_to(:in).value.to_f }
  measurement.nil? ? @inches : measure(measurement, @inches)
end

#kilograms(options = {}) ⇒ Object Also known as: kg, kgs



195
196
197
# File 'lib/freight_kit/package.rb', line 195

def kilograms(options = {})
  weight(options).convert_to(:kg).value.to_f
end

#length(unit) ⇒ Object



145
146
147
# File 'lib/freight_kit/package.rb', line 145

def length(unit)
  @dimensions[2].convert_to(unit).value.to_f
end

#ounces(options = {}) ⇒ Object Also known as: oz



179
180
181
# File 'lib/freight_kit/package.rb', line 179

def ounces(options = {})
  weight(options).convert_to(:oz).value.to_f
end

#oversized?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/freight_kit/package.rb', line 161

def oversized?
  @oversized
end

#pounds(args) ⇒ Object Also known as: lb, lbs



189
190
191
# File 'lib/freight_kit/package.rb', line 189

def pounds(args)
  weight(*args).convert_to(:lb).value.to_f
end

#total_weight(options = {}) ⇒ Object



223
224
225
# File 'lib/freight_kit/package.rb', line 223

def total_weight(options = {})
  weight(@total_weight, options)
end

#unpackaged?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/freight_kit/package.rb', line 165

def unpackaged?
  @unpackaged
end

#width(unit) ⇒ Object



149
150
151
# File 'lib/freight_kit/package.rb', line 149

def width(unit)
  @dimensions[1].convert_to(unit).value.to_f
end