Class: Html2rss::Selectors::PostProcessors::Template

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

Overview

Returns a formatted String according to the string pattern. It uses [Kernel#format](ruby-doc.org/core/Kernel.html#method-i-format)

It supports the format pattern ‘%<key>s` and `%key`, where `key` is the key of the selector. If `%self` is used, the selectors extracted value will be used.

Imagine this HTML:

<li>
  <h1>Product</h1>
  <span class="price">23,42€</span>
</li>

YAML usage example:

selectors:
  items:
    selector: 'li'
  price:
    selector: '.price'
  title:
    selector: h1
    post_process:
      name: template
      string: '%{self} (%{price})'

Would return:

'Product (23,42€)'

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) ⇒ Template

Returns a new instance of Template.

Parameters:



50
51
52
53
54
55
56
57
# File 'lib/html2rss/selectors/post_processors/template.rb', line 50

def initialize(value, context)
  super

  @options = context[:options] || {}
  @scraper = context[:scraper]
  @item = context[:item]
  @string = @options[:string].to_s
end

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

Raises:



40
41
42
43
44
45
# File 'lib/html2rss/selectors/post_processors/template.rb', line 40

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

  string = context[:options]&.dig(:string).to_s
  raise InvalidType, 'The `string` template is absent.' if string.empty?
end

Instance Method Details

#getString

Returns:

  • (String)


61
62
63
# File 'lib/html2rss/selectors/post_processors/template.rb', line 61

def get
  Html2rss::Config::DynamicParams.call(@string, {}, getter: method(:item_value), replace_missing_with: '')
end