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

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:

  • Product

    23,42€
  • 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:

    
    
    47
    48
    49
    50
    51
    52
    53
    54
    # File 'lib/html2rss/selectors/post_processors/template.rb', line 47
    
    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) ⇒ Object

    Raises:

    
    
    37
    38
    39
    40
    41
    42
    # File 'lib/html2rss/selectors/post_processors/template.rb', line 37
    
    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)
    
    
    58
    59
    60
    # File 'lib/html2rss/selectors/post_processors/template.rb', line 58
    
    def get
      Html2rss::Config::DynamicParams.call(@string, {}, getter: method(:item_value), replace_missing_with: '')
    end