Class: RecipeScrapers::Sources::Microdata

Inherits:
Object
  • Object
show all
Defined in:
lib/recipe_scrapers/sources/microdata.rb

Constant Summary collapse

VALUE_ATTRIBUTES =
{
  "meta" => "content",
  "audio" => "src", "embed" => "src", "iframe" => "src", "img" => "src",
  "source" => "src", "track" => "src", "video" => "src",
  "a" => "href", "area" => "href", "link" => "href",
  "object" => "data",
  "data" => "value", "meter" => "value",
  "time" => "datetime"
}.freeze
INLINE =
%w[
  a abbr b bdi bdo big cite code data del dfn em font i img ins kbd label link mark meta
  q s samp small span strong sub sup svg time tt u var wbr
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Microdata

Returns a new instance of Microdata.



22
23
24
25
# File 'lib/recipe_scrapers/sources/microdata.rb', line 22

def initialize(document)
  @nodes = roots(document).flat_map { |element| flatten(item_for(element)) }
  @by_id = @nodes.select { |node| node["@id"] }.to_h { |node| [node["@id"], node] }
end

Instance Method Details

#person(id) ⇒ Object



28
# File 'lib/recipe_scrapers/sources/microdata.rb', line 28

def person(id) = entity(id, "Person")

#rating(id) ⇒ Object



29
# File 'lib/recipe_scrapers/sources/microdata.rb', line 29

def rating(id) = entity(id, "AggregateRating")

#recipe ⇒ Object



27
# File 'lib/recipe_scrapers/sources/microdata.rb', line 27

def recipe = @recipe ||= node_of_type("Recipe")

#website_name ⇒ Object



31
32
33
34
# File 'lib/recipe_scrapers/sources/microdata.rb', line 31

def website_name
  site = node_of_type("WebSite")
  site && Text.normalize(site["name"])
end