Class: Html2rss::AutoSource::Scraper::Microformats2

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/html2rss/auto_source/scraper/microformats2.rb

Overview

Scrapes h-entry objects based on Microformats2 specifications.

Constant Summary collapse

ENTRY_SELECTOR =

Selector for Microformats2 h-entry containers.

'.h-entry, [class*="h-entry"]'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parsed_body, url:, **_opts) ⇒ void

Parameters:

  • parsed_body (Nokogiri::HTML::Document)

    parsed HTML document

  • url (String, Html2rss::Url)

    base page URL

  • _opts (Hash)

    unused scraper-specific options

Options Hash (**_opts):

  • :_reserved (Object)

    reserved for future options



36
37
38
39
# File 'lib/html2rss/auto_source/scraper/microformats2.rb', line 36

def initialize(parsed_body, url:, **_opts)
  @parsed_body = parsed_body
  @url = Html2rss::Url.from_absolute(url)
end

Class Method Details

.articles?(parsed_body) ⇒ Boolean

Returns whether Microformats2 h-entry elements exist.

Parameters:

  • parsed_body (Nokogiri::HTML::Document, nil)

    parsed HTML document

Returns:

  • (Boolean)

    whether Microformats2 h-entry elements exist



24
25
26
27
28
# File 'lib/html2rss/auto_source/scraper/microformats2.rb', line 24

def articles?(parsed_body)
  return false unless parsed_body

  !parsed_body.at_css(ENTRY_SELECTOR).nil?
end

.options_keySymbol

Returns scraper config key.

Returns:

  • (Symbol)

    scraper config key



19
# File 'lib/html2rss/auto_source/scraper/microformats2.rb', line 19

def self.options_key = :microformats2

Instance Method Details

#each {|article| ... } ⇒ Enumerator, void

Yields normalized article hashes extracted from h-entry elements.

Yield Parameters:

  • article (Hash{Symbol => Object})

    normalized article hash

Returns:

  • (Enumerator, void)

    enumerator when no block is given



46
47
48
49
50
51
52
53
# File 'lib/html2rss/auto_source/scraper/microformats2.rb', line 46

def each
  return enum_for(:each) unless block_given?

  parsed_body.css(ENTRY_SELECTOR).each do |entry|
    article = build_article(entry)
    yield article if article
  end
end