Class: Html2rss::AttributePostProcessors::Template

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

Constructor Details

#initialize(value, env) ⇒ Template

Returns a new instance of Template.



32
33
34
35
36
37
# File 'lib/html2rss/attribute_post_processors/template.rb', line 32

def initialize(value, env)
  @value = value
  @options = env[:options]
  @item = env[:item]
  @string = @options[:string]
end

Instance Method Details

#getString

Returns:

  • (String)


41
42
43
44
45
46
47
48
49
50
# File 'lib/html2rss/attribute_post_processors/template.rb', line 41

def get
  return format_string_with_methods if @options[:methods]

  names = string.scan(/%[<|{](\w*)[>|}]/)
  names.flatten!
  names.compact!
  names.map!(&:to_sym)

  format(string, names.map { |name| [name, item_value(name)] }.to_h)
end