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#[] 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'

Defined Under Namespace

Classes: Options

Constant Summary collapse

OPTION_TYPES =

Required config field types (validator introspection via Options).

{ start: Integer }.freeze
OPTIONAL_OPTION_TYPES =

Optional config field types (validated when the key is present and non-nil).

{ end: Integer }.freeze
DESCRIPTION =

JSON Schema description exported via schema_doc.

'Return a slice of the extracted string using Integer `start` and optional `end` ' \
'(Ruby String#[] range semantics; end may be omitted).'
EXAMPLES =

Example post-process objects for JSON Schema examples.

[
  { 'name' => 'substring', 'start' => 4, 'end' => 6 }
].freeze

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

.schema_docHash{Symbol => Object}

Returns JSON Schema fragment for this post-processor.

Returns:

  • (Hash{Symbol => Object})

    JSON Schema fragment for this post-processor



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

def self.schema_doc = SchemaDoc.for_post_processor(name: :substring, klass: self)

.validate_args!(value, context) ⇒ void

This method returns an undefined value.

Parameters:

  • value (String)

    extracted selector value

  • context (Selectors::Context)

    post-processor context



57
58
59
60
61
62
63
# File 'lib/html2rss/selectors/post_processors/substring.rb', line 57

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

  options = context[:options]
  assert_type(options[:start], Integer, :start, context:)
  assert_type(options[:end], Integer, :end, context:) if options.key?(:end)
end

Instance Method Details

#getString?

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

Returns:

  • (String, nil)

    The extracted substring.



69
70
71
# File 'lib/html2rss/selectors/post_processors/substring.rb', line 69

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

Raises:

  • (ArgumentError)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/html2rss/selectors/post_processors/substring.rb', line 77

def range
  options = context[:options]
  start = options[:start]

  return (start..) unless options.key?(:end)

  finish = options[:end]
  raise ArgumentError, 'The `start` value must be unequal to the `end` value.' if start == finish

  (start..finish)
end