Module: Html2rss::ItemExtractors
- Defined in:
- lib/html2rss/item_extractors.rb,
lib/html2rss/item_extractors/href.rb,
lib/html2rss/item_extractors/html.rb,
lib/html2rss/item_extractors/text.rb,
lib/html2rss/item_extractors/static.rb,
lib/html2rss/item_extractors/attribute.rb
Overview
Provides a namespace for item extractors.
Defined Under Namespace
Classes: Attribute, Href, Html, Static, Text, UnknownExtractorName
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 =
:text
Class Method Summary collapse
-
.build_options_instance(extractor_class, attribute_options) ⇒ Object
Builds the options instance for the extractor class.
-
.create_extractor_instance(extractor_class, xml, options_instance) ⇒ Object
Creates an instance of the extractor class.
-
.element(xml, selector) ⇒ Nokogiri::XML::ElementSet
Retrieves an element from Nokogiri XML based on the selector.
-
.find_extractor_class(extractor_name) ⇒ Class
Finds the extractor class based on the name.
-
.item_extractor_factory(attribute_options, xml) ⇒ Object
Creates an instance of the requested item extractor.
Class Method Details
.build_options_instance(extractor_class, attribute_options) ⇒ Object
Builds the options instance for the extractor class.
72 73 74 75 |
# File 'lib/html2rss/item_extractors.rb', line 72 def self.(extractor_class, ) = .slice(*extractor_class::Options.members) ITEM_OPTION_CLASSES[extractor_class].new() end |
.create_extractor_instance(extractor_class, xml, options_instance) ⇒ Object
Creates an instance of the extractor class.
84 85 86 |
# File 'lib/html2rss/item_extractors.rb', line 84 def self.create_extractor_instance(extractor_class, xml, ) extractor_class.new(xml, ) end |
.element(xml, selector) ⇒ Nokogiri::XML::ElementSet
Retrieves an element from Nokogiri XML based on the selector.
37 38 39 |
# File 'lib/html2rss/item_extractors.rb', line 37 def self.element(xml, selector) selector ? xml.css(selector) : xml end |
.find_extractor_class(extractor_name) ⇒ Class
Finds the extractor class based on the name.
61 62 63 64 |
# File 'lib/html2rss/item_extractors.rb', line 61 def self.find_extractor_class(extractor_name) NAME_TO_CLASS[extractor_name] || raise(UnknownExtractorName, "Unknown extractor name '#{extractor_name}' requested in NAME_TO_CLASS") end |
.item_extractor_factory(attribute_options, xml) ⇒ Object
Creates an instance of the requested item extractor.
48 49 50 51 52 53 |
# File 'lib/html2rss/item_extractors.rb', line 48 def self.item_extractor_factory(, xml) extractor_name = [:extractor]&.to_sym || DEFAULT_EXTRACTOR extractor_class = find_extractor_class(extractor_name) = (extractor_class, ) create_extractor_instance(extractor_class, xml, ) end |