Class: Html2rss::Selectors::PostProcessors::Substring

Inherits:
Base
  • Object
show all
Defined in:
lib/html2rss/selectors/post_processors/substring.rb

Overview

Returns a defined part of a String.

Both parameters must be an Integer and they can be negative. The end parameter can be omitted, in that case it will not cut the String at the end.

A Regexp or a MatchString is not supported.

See the [‘String#(ruby-doc.org/core/String.html#method-i-5B-5D) documentation for more information.

Imagine this HTML:

<h1>Foo bar and baz<h1>

YAML usage example:

selectors:
  title:
    selector: h1
    post_process:
      name: substring
      start: 4
      end: 6

Would return:

'bar'

Instance Attribute Summary

Attributes inherited from Base

#context, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

assert_type, expect_options, #initialize

Constructor Details

This class inherits a constructor from Html2rss::Selectors::PostProcessors::Base

Class Method Details

.validate_args!(value, context) ⇒ void

This method returns an undefined value.

Parameters:

  • value (String)

    extracted selector value

  • context (Selectors::Context)

    post-processor context



36
37
38
39
40
41
42
43
44
# File 'lib/html2rss/selectors/post_processors/substring.rb', line 36

def self.validate_args!(value, context)
  assert_type value, String, :value, context:

  options = context[:options]
  assert_type options[:start], Integer, :start, context:

  end_index = options[:end]
  assert_type(end_index, Integer, :end, context:) if end_index
end

Instance Method Details

#getString

Extracts the substring from the original string based on the provided start and end indices.

Returns:

  • (String)

    The extracted substring.



50
51
52
# File 'lib/html2rss/selectors/post_processors/substring.rb', line 50

def get
  value[range]
end

#rangeRange

Determines the range for the substring extraction based on the provided start and end indices.

Returns:

  • (Range)

    The range object representing the start and end/Infinity (integers).



58
59
60
61
62
63
64
65
66
67
# File 'lib/html2rss/selectors/post_processors/substring.rb', line 58

def range
  return (start_index..) unless end_index?

  if start_index == end_index
    raise ArgumentError,
          'The `start` value must be unequal to the `end` value.'
  end

  (start_index..end_index)
end