Sha256: 02d2b4354901cdc565dbf2d530976ea414227aa020e77359266a7874e4278bf6

Contents?: true

Size: 993 Bytes

Versions: 1

Compression:

Stored size: 993 Bytes

Contents

# -*- encoding : utf-8 -*-

module Kabutops

  class RecipeItem
    attr_reader :type, :value

    def initialize type, value, convert_to=nil
      @type = type
      @value = value
      @convert_to = convert_to
    end

    def process resource, page
      convert(get(resource, page))
    end

    protected

    def get resource, page
      case @type
      when :var
        resource[@value]
      when :recipe
        @value.process(resource, page)
      when :css
        page.css(@value).text.gsub(/\u00a0/, ' ').strip
      when :xpath
        page.xpath(@value).text.gsub(/\u00a0/, ' ').strip
      when :lambda, :proc
        @value.call(resource, page)
      when :const, :static
        @value
      else
        raise "unknown recipe item type '#{item.type}'"
      end
    end

    def convert v
      return nil if v.nil?

      case @convert_to
      when nil then v
      when :int then v[/\d+/].to_i
      when :float then v[/\d+(\.\d+)?/].to_f
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kabutops-0.1.2 lib/kabutops/recipe_item.rb