Class: Html2rss::Selectors::Extractors::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/selectors/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 PostProcessors::ParseTime.

Defined Under Namespace

Classes: Options

Instance Method Summary collapse

Constructor Details

#initialize(xml, options) ⇒ Attribute

Initializes the Attribute extractor.

Parameters:

  • xml (Nokogiri::XML::Element)
  • options (Options)

Options Hash (options):

  • :selector (String)

    CSS selector used to find the element

  • :attribute (String)

    attribute name to extract from the selected element



37
38
39
40
# File 'lib/html2rss/selectors/extractors/attribute.rb', line 37

def initialize(xml, options)
  @options = options
  @element = Extractors.element(xml, options.selector)
end

Instance Method Details

#getString

Retrieves and returns the attribute’s value as a string.

Returns:

  • (String)

    The value of the attribute.



46
47
48
# File 'lib/html2rss/selectors/extractors/attribute.rb', line 46

def get
  @element.attr(@options.attribute).to_s
end