Class: Html2rss::Item
- Inherits:
-
Object
- Object
- Html2rss::Item
- Defined in:
- lib/html2rss/item.rb
Overview
Takes the selected Nokogiri::HTML and responds to accessor names defined in the feed config.
Instances can only be created via ‘.from_url` and each represents an internally used “RSS item”. Such an item provides dynamically defined attributes as methods.
Defined Under Namespace
Class Method Summary collapse
-
.from_url(url, config) ⇒ Array<Html2rss::Item>
Fetches items from a given URL using configuration settings.
Instance Method Summary collapse
-
#categories ⇒ Array<String>
Retrieves categories for the item based on configured category selectors.
-
#enclosure ⇒ Enclosure
Retrieves enclosure details for the item.
-
#enclosure? ⇒ true, false
Checks if the item has an enclosure based on configuration.
-
#extract(tag) ⇒ String
Selects and processes data according to the selector name.
-
#guid ⇒ String
SHA1 hashed GUID.
-
#initialize(xml, config) ⇒ Item
constructor
A new instance of Item.
-
#method_missing(method_name, *_args) ⇒ String
Dynamically extracts data based on the method name.
-
#respond_to_missing?(method_name, _include_private = false) ⇒ true, false
Checks if the object responds to a method dynamically based on the configuration.
-
#title_or_description ⇒ String?
Returns either the title or the description, preferring title if available.
-
#valid? ⇒ true, false
Checks if the item is valid accordin to RSS 2.0 spec, by ensuring it has at least a title or a description.
Constructor Details
#initialize(xml, config) ⇒ Item
Returns a new instance of Item.
22 23 24 25 |
# File 'lib/html2rss/item.rb', line 22 def initialize(xml, config) @xml = xml @config = config end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *_args) ⇒ String
Dynamically extracts data based on the method name.
45 46 47 48 49 |
# File 'lib/html2rss/item.rb', line 45 def method_missing(method_name, *_args) return super unless respond_to_missing?(method_name) extract(method_name) end |
Class Method Details
.from_url(url, config) ⇒ Array<Html2rss::Item>
Fetches items from a given URL using configuration settings.
131 132 133 134 135 136 137 138 |
# File 'lib/html2rss/item.rb', line 131 def self.from_url(url, config) body = Utils.request_body_from_url(url, convert_json_to_xml: config.json?, headers: config.headers) Nokogiri.HTML(body) .css(config.selector_string(Config::Selectors::ITEMS_SELECTOR_NAME)) .map { |xml| new(xml, config) } .select(&:valid?) end |
Instance Method Details
#categories ⇒ Array<String>
Retrieves categories for the item based on configured category selectors.
97 98 99 |
# File 'lib/html2rss/item.rb', line 97 def categories config.category_selector_names.map { |method_name| public_send(method_name) } end |
#enclosure ⇒ Enclosure
Retrieves enclosure details for the item.
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/html2rss/item.rb', line 113 def enclosure url = enclosure_url raise 'An item.enclosure requires an absolute URL' unless url&.absolute? Enclosure.new( type: Html2rss::Utils.guess_content_type_from_url(url), bits_length: 0, url: url.to_s ) end |
#enclosure? ⇒ true, false
Checks if the item has an enclosure based on configuration.
105 106 107 |
# File 'lib/html2rss/item.rb', line 105 def enclosure? config.selector?(:enclosure) end |
#extract(tag) ⇒ String
Selects and processes data according to the selector name.
56 57 58 59 60 61 62 63 |
# File 'lib/html2rss/item.rb', line 56 def extract(tag) = config.selector_attributes_with_channel(tag.to_sym) post_process( ItemExtractors.item_extractor_factory(, xml).get, .fetch(:post_process, false) ) end |
#guid ⇒ String
Returns SHA1 hashed GUID.
87 88 89 90 91 |
# File 'lib/html2rss/item.rb', line 87 def guid content = config.guid_selector_names.flat_map { |method_name| public_send(method_name) }.join Digest::SHA1.hexdigest(content) end |
#respond_to_missing?(method_name, _include_private = false) ⇒ true, false
Checks if the object responds to a method dynamically based on the configuration.
35 36 37 |
# File 'lib/html2rss/item.rb', line 35 def respond_to_missing?(method_name, _include_private = false) config.selector?(method_name) || super end |
#title_or_description ⇒ String?
Returns either the title or the description, preferring title if available.
78 79 80 81 82 |
# File 'lib/html2rss/item.rb', line 78 def title_or_description return title if config.selector?(:title) description if config.selector?(:description) end |
#valid? ⇒ true, false
Checks if the item is valid accordin to RSS 2.0 spec, by ensuring it has at least a title or a description.
70 71 72 |
# File 'lib/html2rss/item.rb', line 70 def valid? title_or_description.to_s != '' end |