Class: Html2rss::Selectors::Extractors::Href

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/selectors/extractors/href.rb

Overview

Returns the value of the href attribute. It always returns absolute URLs. If the extracted href value is a relative URL, it prepends the channel’s URL.

Imagine this a HTML element with a href attribute:

<a href="/posts/latest-findings">...</a>

YAML usage example:

channel:
  url: http://blog-without-a-feed.example.com
  ...
selectors:
  link:
    selector: a
    extractor: href

Would return:

'http://blog-without-a-feed.example.com/posts/latest-findings'

Defined Under Namespace

Classes: Options

Instance Method Summary collapse

Constructor Details

#initialize(xml, options) ⇒ Href

Initializes the Href extractor.

Parameters:

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

Options Hash (options):

  • :selector (String)

    CSS selector used to find the link element

  • :channel (Hash{Symbol => Object})

    channel configuration, including :url



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

def initialize(xml, options)
  @options = options
  @element = Extractors.element(xml, options.selector)
  @href = @element.attr('href').to_s
end

Instance Method Details

#getString

Retrieves and returns the normalized absolute URL.

Returns:

  • (String)

    The absolute URL.



47
48
49
50
51
# File 'lib/html2rss/selectors/extractors/href.rb', line 47

def get
  return nil unless @href

  Url.from_relative(@href, @options.channel[:url])
end