Class: Html2rss::AttributePostProcessors::Template

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

Overview

Returns a formatted String according to the string pattern.

If self is used, the selectors extracted value will be used. It uses [Kernel#format](ruby-doc.org/core/Kernel.html#method-i-format)

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:



45
46
47
48
49
50
51
# File 'lib/html2rss/attribute_post_processors/template.rb', line 45

def initialize(value, context)
  super

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

Class Method Details

.validate_args!(value, context) ⇒ Object

Raises:



35
36
37
38
39
40
# File 'lib/html2rss/attribute_post_processors/template.rb', line 35

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)


55
56
57
# File 'lib/html2rss/attribute_post_processors/template.rb', line 55

def get
  @options[:methods] ? format_string_with_methods : format_string_with_dynamic_params
end