class Html2rss::AttributePostProcessors::Template

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€)'

Attributes

string[R]

Public Class Methods

new(value, env) click to toggle source
# 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

Public Instance Methods

get() click to toggle source

@return [String]

# 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

Private Instance Methods

format_string_with_methods() click to toggle source
# File lib/html2rss/attribute_post_processors/template.rb, line 60
def format_string_with_methods
  string % methods
end
item_value(method_name) click to toggle source
# File lib/html2rss/attribute_post_processors/template.rb, line 64
def item_value(method_name)
  method_name.to_sym == :self ? @value.to_s : @item.public_send(method_name).to_s
end
methods() click to toggle source
# File lib/html2rss/attribute_post_processors/template.rb, line 56
def methods
  @methods ||= @options[:methods].map(&method(:item_value))
end