Module: RecipeScrapers::Scraper::ParsedFields

Included in:
RecipeScrapers::Scraper
Defined in:
lib/recipe_scrapers/scraper/parsed_fields.rb

Overview

The fields that split text into value objects, with the parsers Configuration#parsers holds.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(scraper) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



11
12
13
# File 'lib/recipe_scrapers/scraper/parsed_fields.rb', line 11

def self.included(scraper)
  scraper.extend(ClassMethods)
end

Instance Method Details

#parsed_ingredients ⇒ Array<Models::Ingredient>?

Every line of Models::Recipe#ingredients, in the same order, split into an amount, a unit and a name.

Examples:

recipe.parsed_ingredients.first.to_h # => { amount: 1.5, unit: "cups", name: "vegetable oil" }


36
37
38
39
# File 'lib/recipe_scrapers/scraper/parsed_fields.rb', line 36

def parsed_ingredients
  spoken = language
  ingredients&.map { |line| parse(:ingredients, line, spoken) }
end

#parsed_nutrients ⇒ Array<Models::Nutrient>?

Every entry of Models::Recipe#nutrients, in the order the page lists them, split into a name, a unit and an amount. The name is the key the page uses. A value with no number is left out.

Examples:

recipe.parsed_nutrients.first.to_h # => { name: "calories", unit: "kcal", amount: 219.0 }


42
43
44
45
46
47
48
49
# File 'lib/recipe_scrapers/scraper/parsed_fields.rb', line 42

def parsed_nutrients
  spoken = language
  parsed = nutrients.to_h.filter_map do |name, text|
    nutrient = parse(:nutrients, text, spoken)
    nutrient && (nutrient.name ? nutrient : nutrient.with(name: name))
  end
  parsed.empty? ? nil : parsed
end