class Pebbles::CookpadSearch::Searcher

Public Class Methods

by_id(url) click to toggle source
# File lib/pebbles/cookpad_search/searcher.rb, line 25
        def by_id(url)
          doc = scrape(url)
          recipe = Parser.parse_a_recipe(doc)
          ingredient = recipe[:ingredient_names].zip(recipe[:ingredient_quantity]).to_h

          ingredient_text = ''
          ingredient.each do |name,quantity|
            ingredient_text += "#{name} : #{quantity}\n"
          end

          line = '==============================================================================================='
          _line = '-----------------------------------------------------------------------------------------------'

          steps = recipe[:step_number].zip(recipe[:step_text]).to_h
          step_text = ''
          steps.each do |num,text|
            step_text += "[#{num}]\n#{text}\n#{_line}\n"
          end

          text = <<-CONTENTS
#{line}
#{recipe[:title]}
#{line}
材料 #{recipe[:serving_for]}
#{ingredient_text}
#{_line}
#{step_text}
          CONTENTS

          puts text
        end

Private Class Methods

scrape(url) click to toggle source
# File lib/pebbles/cookpad_search/searcher.rb, line 59
def scrape(url)
  html = {}
  html[:doc] = open(url) do |f|
    html[:charset] = f.charset
    f.read
  end
  html
rescue OpenURI::HTTPError => ex
  puts "存在しないか、プレミアム会員限定レシピです。"
end