Module: RecipeScrapers::Parsers::Nutrients

Defined in:
lib/recipe_scrapers/parsers/nutrients.rb

Overview

The bundled parser of nutrient values. It reads the first number and the word after it, and drops text in parentheses. Mass and energy units are written as symbols, so "grams" and "г" become "g" and "calories" becomes "kcal".

Examples:

RecipeScrapers::Parsers::Nutrients.call("12.5 grams").to_h # => { name: nil, unit: "g", amount: 12.5 }

Class Method Summary collapse

Class Method Details

.call(text) ⇒ Models::Nutrient?

Returns without a name, which Models::Recipe#parsed_nutrients fills in. Nil when the value has no number.

Parameters:

  • text (String) —

    one nutrient value

Returns:



30
31
32
33
34
35
# File 'lib/recipe_scrapers/parsers/nutrients.rb', line 30

def call(text, **)
  match = text.to_s.gsub(PARENTHESES, " ").match(MEASURE)
  return nil if match.nil?

  Models::Nutrient.new(amount: Quantity.total(match[:amount]), unit: unit(match[:unit]))
end