Class: Html2rss::AttributePostProcessors::Substring

Inherits:
Object
  • 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 Method Summary collapse

Constructor Details

#initialize(value, env) ⇒ Substring

Returns a new instance of Substring.

Parameters:

  • value (String)

    The original string to extract a substring from.

  • env (Item::Context)

    Context object providing additional environment details.



35
36
37
38
# File 'lib/html2rss/attribute_post_processors/substring.rb', line 35

def initialize(value, env)
  @value = value
  @options = env[:options]
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.



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

def get
  start_index = @options[:start].to_i
  end_index = @options[:end]&.to_i || @value.length

  @value[start_index..end_index]
end