Class: Html2rss::ItemExtractors::Attribute
- Inherits:
-
Object
- Object
- Html2rss::ItemExtractors::Attribute
- Defined in:
- lib/html2rss/item_extractors/attribute.rb
Overview
Returns the value of the attribute.
Imagine this time
HTML tag with a datetime
attribute:
<time datetime="2019-07-01">...</time>
YAML usage example:
selectors:
link:
selector: time
extractor: attribute
attribute: datetime
Would return:
'2019-07-01'
In case you’re extracting a date or a time, consider parsing it during post processing with AttributePostProcessors::ParseTime.
Defined Under Namespace
Classes: Options
Instance Method Summary collapse
-
#get ⇒ String
Retrieves and returns the attribute’s value as a string.
-
#initialize(xml, options) ⇒ Attribute
constructor
Initializes the Attribute extractor.
Constructor Details
#initialize(xml, options) ⇒ Attribute
Initializes the Attribute extractor.
34 35 36 37 |
# File 'lib/html2rss/item_extractors/attribute.rb', line 34 def initialize(xml, ) @options = @element = ItemExtractors.element(xml, .selector) end |
Instance Method Details
#get ⇒ String
Retrieves and returns the attribute’s value as a string.
43 44 45 46 47 |
# File 'lib/html2rss/item_extractors/attribute.rb', line 43 def get @element.attr(@options.attribute).to_s.freeze rescue NoMethodError => error raise "Failed to extract attribute: #{error.}" end |