Sha256: 88b71fb2ae3385e92805004797757295859d069c7b10df9b703a4e7862faa014

Contents?: true

Size: 1.31 KB

Versions: 7

Compression:

Stored size: 1.31 KB

Contents

require 'sanitize'

module Html2rss
  module AttributePostProcessors
    ## Returns a formatted String according to the string pattern.
    #
    # If +self+ is given as a method, the extracted value will be used.
    #
    # 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: '%s (%s)'
    #         methods:
    #           - self
    #           - price
    #
    # Would return:
    #    'Product (23,42€)'
    class Template
      def initialize(value, env)
        @value = value
        @options = env[:options]
        @item = env[:item]
      end

      ##
      # - uses {http://ruby-doc.org/core-2.6.3/String.html#method-i-25 String#%}
      # @return [String]
      def get
        string % methods
      end

      private

      def string
        @options['string']
      end

      def methods
        @methods ||= @options['methods'].map do |method|
          method == 'self' ? @value.to_s : @item.public_send(method.to_sym).to_s
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
html2rss-0.7.0 lib/html2rss/attribute_post_processors/template.rb
html2rss-0.6.0 lib/html2rss/attribute_post_processors/template.rb
html2rss-0.5.2 lib/html2rss/attribute_post_processors/template.rb
html2rss-0.5.1 lib/html2rss/attribute_post_processors/template.rb
html2rss-0.5.0 lib/html2rss/attribute_post_processors/template.rb
html2rss-0.4.1 lib/html2rss/attribute_post_processors/template.rb
html2rss-0.4.0 lib/html2rss/attribute_post_processors/template.rb