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€)'
Defined Under Namespace
Classes: Options
Constant Summary collapse
- OPTION_TYPES =
Required config field types (validator introspection via
Options). { string: String }.freeze
- DESCRIPTION =
JSON Schema description exported via
schema_doc. rubocop:disable Style/FormatStringToken -- documents Kernel#format%{key}placeholders 'Format a string with Kernel#format-style placeholders (`%{key}` / `%<key>s`). ' \ '`%{self}` is the current selector value; other keys resolve sibling selectors.'
- EXAMPLES =
Example post-process objects for JSON Schema
examples. [ { 'name' => 'template', 'string' => '`%{self}` (`%{price}`)' } ].freeze
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
-
.schema_doc ⇒ Hash{Symbol => Object}
JSON Schema fragment for this post-processor.
- .validate_args!(value, context) ⇒ void
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.
74 75 76 77 78 79 80 |
# File 'lib/html2rss/selectors/post_processors/template.rb', line 74 def initialize(value, context) super @options = context[:options] || {} @string = @options[:string].to_s @getter = ->(key) { item_value(key) } end |
Class Method Details
.schema_doc ⇒ Hash{Symbol => Object}
Returns JSON Schema fragment for this post-processor.
55 |
# File 'lib/html2rss/selectors/post_processors/template.rb', line 55 def self.schema_doc = SchemaDoc.for_post_processor(name: :template, klass: self) |
.validate_args!(value, context) ⇒ void
This method returns an undefined value.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/html2rss/selectors/post_processors/template.rb', line 60 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? return if context.item_scope raise MissingOption, 'The post-processor context is missing `item_scope`.', [], cause: nil end |
Instance Method Details
#get ⇒ String
84 85 86 |
# File 'lib/html2rss/selectors/post_processors/template.rb', line 84 def get Html2rss::Config::DynamicParams.call(@string, {}, getter: @getter, replace_missing_with: '') end |