Class: RecipeScrapers::Declaration
- Inherits:
-
Object
- Object
- RecipeScrapers::Declaration
- Defined in:
- lib/recipe_scrapers/declaration.rb
Overview
Tells the gem where the recipe fields are on a site whose markup is missing or wrong. A declaration is built by register. Inside its block, each recipe field is a method that takes a CSS selector:
title "h1.recipe-title"reads the text of the first match.ingredients rows: "ul.ingredients li"reads one line per match, for ingredients, instructions, keywords and equipment.ingredient_groups heading: "h3", item: "li"splits the ingredients under their headings.
A declared field wins over the schema.org value. Every field left out falls back to it.
Instance Attribute Summary collapse
-
#encoding_name ⇒ String?
readonly
The encoding the site sends without saying so, set with #encoding.
-
#host ⇒ String
readonly
The host the declaration was registered for.
Class Method Summary collapse
Instance Method Summary collapse
-
#encoding(name) ⇒ Object
Declares the encoding of pages that send no charset, or the wrong one.
-
#initialize(host) ⇒ Declaration
constructor
A new instance of Declaration.
-
#parser(field, *parsers) ⇒ Object
Sets the parsers of a field for this site, in the order they are tried.
-
#parsers_for(field) ⇒ Array<#call>?
private
The parsers this site sets for the field.
-
#rule(field) ⇒ Hash?
private
What the declaration says about the field:
{selector:},{rows:}, or{heading:, item:}for ingredient groups.
Constructor Details
#initialize(host) ⇒ Declaration
39 40 41 42 43 44 |
# File 'lib/recipe_scrapers/declaration.rb', line 39 def initialize(host) @host = host @rules = {} @parsers = {} @encoding_name = nil end |
Instance Attribute Details
#encoding_name ⇒ String? (readonly)
30 31 32 |
# File 'lib/recipe_scrapers/declaration.rb', line 30 def encoding_name @encoding_name end |
#host ⇒ String (readonly)
27 28 29 |
# File 'lib/recipe_scrapers/declaration.rb', line 27 def host @host end |
Class Method Details
.build(host) { ... } ⇒ Declaration
35 36 37 |
# File 'lib/recipe_scrapers/declaration.rb', line 35 def self.build(host, &block) new(host).tap { |declaration| declaration.instance_eval(&block) if block } end |
Instance Method Details
#encoding(name) ⇒ Object
Declares the encoding of pages that send no charset, or the wrong one.
58 59 60 |
# File 'lib/recipe_scrapers/declaration.rb', line 58 def encoding(name) @encoding_name = name end |
#parser(field, *parsers) ⇒ Object
Sets the parsers of a field for this site, in the order they are tried.
67 68 69 |
# File 'lib/recipe_scrapers/declaration.rb', line 67 def parser(field, *parsers) @parsers[Parsers::Chain.field!(field)] = parsers end |
#parsers_for(field) ⇒ Array<#call>?
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.
Returns the parsers this site sets for the field.
75 76 77 |
# File 'lib/recipe_scrapers/declaration.rb', line 75 def parsers_for(field) @parsers[field] end |
#rule(field) ⇒ Hash?
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.
Returns what the declaration says about the field: {selector:}, {rows:}, or
{heading:, item:} for ingredient groups.
51 52 53 |
# File 'lib/recipe_scrapers/declaration.rb', line 51 def rule(field) @rules[field] end |