Class: Html2rss::Selectors::PostProcessors::Gsub

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

Overview

Imagine this HTML:

<h1>Foo bar and boo<h1>

YAML usage example:

selectors:
 title:
   selector: h1
   post_process:
     name: gsub
     pattern: boo
     replacement: baz

Would return:

'Foo bar and baz'

pattern can be a Regexp or a String. If it is a String, it will remove one pair of surrounding slashes ('/') to keep backwards compatibility and then parse it to build a Regexp.

replacement can be a String or a Hash.

See the doc on String#gsub for more info.

Defined Under Namespace

Classes: Options

Constant Summary collapse

OPTION_TYPES =

Required config field types (validator introspection via Options).

{ pattern: String, replacement: String }.freeze
DESCRIPTION =

JSON Schema description exported via schema_doc.

'Replace matches of `pattern` in the extracted string with `replacement` ' \
'(Ruby String#gsub; pattern may be a regexp-like string).'
EXAMPLES =

Example post-process objects for JSON Schema examples.

[
  { 'name' => 'gsub', 'pattern' => 'boo', 'replacement' => 'baz' }
].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

Constructor Details

#initialize(value, context) ⇒ Gsub

Returns a new instance of Gsub.

Parameters:



62
63
64
65
66
67
68
69
# File 'lib/html2rss/selectors/post_processors/gsub.rb', line 62

def initialize(value, context)
  super

  options = context[:options]

  @replacement = options[:replacement]
  @pattern = options[:pattern]
end

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



48
# File 'lib/html2rss/selectors/post_processors/gsub.rb', line 48

def self.schema_doc = SchemaDoc.for_post_processor(name: :gsub, 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



53
54
55
56
57
# File 'lib/html2rss/selectors/post_processors/gsub.rb', line 53

def self.validate_args!(value, context)
  assert_type value, String, :value, context:
  expect_options(%i[replacement pattern], context)
  assert_type context.dig(:options, :replacement), [String, Hash], :replacement, context:
end

Instance Method Details

#getString

Returns:

  • (String)


73
74
75
# File 'lib/html2rss/selectors/post_processors/gsub.rb', line 73

def get
  value.to_s.gsub(pattern, replacement)
end