Class: Html2rss::AutoSource::LinkHeuristics::HrefExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/auto_source/link_heuristics/href_extractor.rb

Overview

Extracts a normalized href from a Nokogiri anchor or raw href value.

Constant Summary collapse

HREF_BASE_PATTERN =

Regexp to capture everything before the first '#'

/\A([^#]*)/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(anchor_or_href) ⇒ HrefExtractor

Returns a new instance of HrefExtractor.

Parameters:

  • anchor_or_href (Nokogiri::XML::Element, String, #to_s)

    anchor element or href-like value



16
17
18
# File 'lib/html2rss/auto_source/link_heuristics/href_extractor.rb', line 16

def initialize(anchor_or_href)
  @anchor_or_href = anchor_or_href
end

Class Method Details

.call(anchor_or_href) ⇒ String?

Returns href without fragment, or nil when blank.

Parameters:

  • anchor_or_href (Nokogiri::XML::Element, String, #to_s)

    anchor element or href-like value

Returns:

  • (String, nil)

    href without fragment, or nil when blank



13
# File 'lib/html2rss/auto_source/link_heuristics/href_extractor.rb', line 13

def self.call(anchor_or_href) = new(anchor_or_href).call

Instance Method Details

#callString?

Returns href without fragment, or nil when blank.

Returns:

  • (String, nil)

    href without fragment, or nil when blank



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/html2rss/auto_source/link_heuristics/href_extractor.rb', line 21

def call
  href = case @anchor_or_href
         when Nokogiri::XML::Node
           @anchor_or_href['href']
         else
           @anchor_or_href
         end

  return unless href

  # Extract base part before # and strip whitespace
  base = href.to_s[HREF_BASE_PATTERN, 1].strip
  base unless base.empty?
end