Module: Html2rss::Selectors::Extractors

Defined in:
lib/html2rss/selectors/extractors.rb,
lib/html2rss/selectors/extractors/href.rb,
lib/html2rss/selectors/extractors/html.rb,
lib/html2rss/selectors/extractors/text.rb,
lib/html2rss/selectors/extractors/static.rb,
lib/html2rss/selectors/extractors/attribute.rb

Overview

Provides a namespace for item extractors.

Defined Under Namespace

Classes: Attribute, Href, Html, Static, Text

Constant Summary collapse

NAME_TO_CLASS =

Maps the extractor name to the class implementing the extractor.

The key is the name to use in the feed config.

{
  attribute: Attribute,
  href: Href,
  html: Html,
  static: Static,
  text: Text
}.freeze
ITEM_OPTION_CLASSES =

Maps the extractor class to its corresponding options class.

Hash.new do |hash, klass|
  hash[klass] = klass.const_get(:Options)
end
DEFAULT_EXTRACTOR =

Extractor used when none is explicitly configured.

:text

Class Method Summary collapse

Class Method Details

.element(xml, selector) ⇒ Nokogiri::XML::ElementSet

Retrieves an element from Nokogiri XML based on the selector.

Parameters:

  • xml (Nokogiri::XML::Document)
  • selector (String, nil)

Returns:

  • (Nokogiri::XML::ElementSet)

    selected XML elements



36
37
38
# File 'lib/html2rss/selectors/extractors.rb', line 36

def element(xml, selector)
  selector ? xml.css(selector) : xml
end

.get(attribute_options, xml) ⇒ Object

Returns instance of the specified item extractor class.

Parameters:

  • attribute_options (Hash{Symbol => Object})

    Should contain at least ‘:extractor` (the name) and required options for that extractor.

  • xml (Nokogiri::XML::Document)

Returns:

  • (Object)

    instance of the specified item extractor class



44
45
46
47
48
49
# File 'lib/html2rss/selectors/extractors.rb', line 44

def get(attribute_options, xml)
  extractor_class = NAME_TO_CLASS[attribute_options[:extractor]&.to_sym || DEFAULT_EXTRACTOR]
  options = ITEM_OPTION_CLASSES[extractor_class].new(attribute_options.slice(*extractor_class::Options.members))

  extractor_class.new(xml, options).get
end