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

Constant Summary collapse

DESCRIPTION =

JSON Schema description exported via schema_doc.

'Return the absolute URL from the selected element\'s `href` attribute ' \
'(relative hrefs are resolved against the channel URL).'
EXAMPLES =

Example extractor name values for JSON Schema examples.

[
  'href'
].freeze

Class Method Summary collapse

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



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

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

Class Method Details

.schema_docHash{Symbol => Object}

Returns JSON Schema fragment for this extractor name.

Returns:

  • (Hash{Symbol => Object})

    JSON Schema fragment for this extractor name



40
# File 'lib/html2rss/selectors/extractors/href.rb', line 40

def self.schema_doc = SchemaDoc.for_extractor(name: :href, klass: self)

Instance Method Details

#getString

Retrieves and returns the normalized absolute URL.

Returns:

  • (String)

    The absolute URL.



59
60
61
62
63
# File 'lib/html2rss/selectors/extractors/href.rb', line 59

def get
  return nil unless @href

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