Sha256: ec60b11891d14a090fd66f085ec70b50f4e239a8399bf887b91a0110171e816d

Contents?: true

Size: 952 Bytes

Versions: 5

Compression:

Stored size: 952 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)
      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

5 entries across 5 versions & 1 rubygems

Version Path
kabutops-0.1.1 lib/kabutops/recipe_item.rb
kabutops-0.1.0 lib/kabutops/recipe_item.rb
kabutops-0.0.15 lib/kabutops/recipe_item.rb
kabutops-0.0.14 lib/kabutops/recipe_item.rb
kabutops-0.0.13 lib/kabutops/recipe_item.rb