Class: Html2rss::AttributePostProcessors::Substring

Inherits:
Base
  • Object
show all
Defined in:
lib/html2rss/attribute_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::AttributePostProcessors::Base

Class Method Details

.validate_args!(value, context) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/html2rss/attribute_post_processors/substring.rb', line 32

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.



46
47
48
# File 'lib/html2rss/attribute_post_processors/substring.rb', line 46

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).



54
55
56
57
58
59
60
61
62
63
# File 'lib/html2rss/attribute_post_processors/substring.rb', line 54

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