lib/html2rss/item_extractors/attribute.rb in html2rss-0.9.0 vs lib/html2rss/item_extractors/attribute.rb in html2rss-0.10.0

- old
+ new

@@ -1,11 +1,13 @@ +# frozen_string_literal: true + module Html2rss module ItemExtractors ## # Returns the value of the attribute. # - # Imagine this +time+ HTML element with a +datetime+ attribute: + # Imagine this +time+ HTML tag with a +datetime+ attribute: # # <time datetime="2019-07-01">...</time> # # YAML usage example: # @@ -16,22 +18,33 @@ # attribute: datetime # # Would return: # '2019-07-01' # - # In case you're extracting a date or a time, do not forget to parse it - # during post processing with - # {AttributePostProcessors::ParseTime}[rdoc-ref:Html2rss::AttributePostProcessors::ParseTime]. + # In case you're extracting a date or a time, consider parsing it + # during post processing with {AttributePostProcessors::ParseTime}. class Attribute + # The available options for the attribute extractor. + Options = Struct.new('AttributeOptions', :selector, :attribute, keyword_init: true) + + ## + # Initializes the Attribute extractor. + # + # @param xml [Nokogiri::XML::Element] + # @param options [Options] def initialize(xml, options) @options = options - @element = ItemExtractors.element(xml, options) + @element = ItemExtractors.element(xml, options.selector) end ## - # @return [String] + # Retrieves and returns the attribute's value as a string. + # + # @return [String] The value of the attribute. def get - @element.attr(@options[:attribute]).to_s + @element.attr(@options.attribute).to_s.freeze + rescue NoMethodError => error + raise "Failed to extract attribute: #{error.message}" end end end end