Module: PackwizardParser::GramConverter

Defined in:
lib/packwizard_parser/gram_converter.rb

Constant Summary collapse

FACTORS =

Conversion factors for each unit to grams

{
  'g' => 1.0,
  'kg' => 1000.0,
  'oz' => 28.35,
  'lb' => 453.59
}.freeze

Class Method Summary collapse

Class Method Details

.convert(value:, unit:) ⇒ Float

Convert the value to grams

Parameters:

  • value (Float)

    The value to convert

  • unit (String)

    The unit extracted from the JSON

Returns:

  • (Float)

    The converted value in grams



19
20
21
22
# File 'lib/packwizard_parser/gram_converter.rb', line 19

def self.convert(value:, unit:)
  factor = FACTORS.fetch(unit.to_s.downcase, 1.0)
  (value * factor)
end