Class: Html2rss::Selectors::PostProcessors::Template
- 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
Class Method Summary collapse
Instance Method Summary collapse
- #get ⇒ String
-
#initialize(value, context) ⇒ Template
constructor
A new instance of Template.
Methods inherited from Base
Constructor Details
#initialize(value, context) ⇒ Template
Returns a new instance of Template.
47 48 49 50 51 52 53 54 |
# File 'lib/html2rss/selectors/post_processors/template.rb', line 47 def initialize(value, context) super = context[:options] || {} @scraper = context[:scraper] @item = context[:item] @string = [:string].to_s end |
Class Method Details
.validate_args!(value, context) ⇒ Object
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
#get ⇒ 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 |