Sha256: 4c25783c8caf2e6b238875a5e643ba91dc7415ecf48f6943c3c6e821cb8dce91

Contents?: true

Size: 988 Bytes

Versions: 26

Compression:

Stored size: 988 Bytes

Contents

require 'mechanize'


module Scrapers

  module AllRecipes

    def self.scrape(url)

      results = Hash.new

      Scrapers.agent.get(url).tap do |page|
        results[:url] = page.uri.to_s
        results[:title] = page.title.strip
        results[:ingredients] = scrape_ingredients(page)
        results[:directions] = scrape_directions(page)
        results[:photo] = scrape_photo(page)
      end

      results

    end

    def self.scrape_ingredients(page)
      page.
        search("ul.ingredient-wrap").
        search(".//li").
        map do |i|
        i.text.gsub(/[[:space:]]+/,' ').sub(/^/,'*')
      end
    end

    def self.scrape_directions(page)
      page.
        search("div.directLeft").first.
        search("li").
        map do |i|
        i.text.gsub(/[[:space:]]+/,' ').sub(/^/,'# ')
      end
    end

    def self.scrape_photo(page)
      photo = page.search("img#imgPhoto").first
      Hash[photo.attributes.map{|k,v| [k,v.value]}]
    end

  end

end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
scrapers-0.4.2 lib/scrapers/allrecipes.rb
scrapers-0.4.1 lib/scrapers/allrecipes.rb
scrapers-0.4.0 lib/scrapers/allrecipes.rb
scrapers-0.3.0 lib/scrapers/allrecipes.rb
scrapers-0.2.1 lib/scrapers/allrecipes.rb
scrapers-0.2.0 lib/scrapers/allrecipes.rb