Class: Html2rss::Selectors::Extractors::Text

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

Overview

Return the text content of the attribute. This is the default extractor used, when no extractor is explicitly given.

Example HTML structure:

<p>Lorem <b>ipsum</b> dolor ...</p>

YAML usage example:

selectors:
 description:
   selector: p
   extractor: text

Would return:

'Lorem ipsum dolor ...'

Defined Under Namespace

Classes: Options

Constant Summary collapse

DESCRIPTION =

JSON Schema description exported via schema_doc.

'Return collapsed visible text of the selected element (default extractor).'
EXAMPLES =

Example extractor name values for JSON Schema examples.

[
  'text'
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml, options) ⇒ Text

Initializes the Text extractor.

Parameters:

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

Options Hash (options):

  • :selector (String)

    CSS selector used to find the element



44
45
46
# File 'lib/html2rss/selectors/extractors/text.rb', line 44

def initialize(xml, options)
  @element = Extractors.element(xml, options.selector)
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



36
# File 'lib/html2rss/selectors/extractors/text.rb', line 36

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

Instance Method Details

#getString

Retrieves and returns the text content of the element.

Returns:

  • (String)

    The text content.



52
53
54
# File 'lib/html2rss/selectors/extractors/text.rb', line 52

def get
  @element.text.to_s.strip.gsub(/\s+/, ' ')
end