Files
old-stazarucak/whatforlunch.com/app/models/recipe.rb
2017-09-24 10:12:44 +02:00

73 lines
1.7 KiB
Ruby

# encoding: utf-8
class Recipe < ActiveRecord::Base
before_create :setup_ordinal
def scrape!
scrape_document Nokogiri::HTML(open(self.original_url))
end
def scrape_document(doc)
r = self
r.name = doc.css('h1')[0].text.to_s.strip
r.duration = doc.xpath('//*[@id="recipe_preparation"]/div[1]/dl/dd[1]/text()').text.to_s.strip
r.process = doc.css(".recipe_step_description").map { |el| el.text.to_s.strip }.join("\n")
r.ingredients = doc.xpath('//*[@id="recipe_ingredients"]/ul/li').map { |x|
if x.css('span').length == 2
x.css('span')[0].text.strip + " | " + x.css('span')[1].text.strip
else
nil
end
}.compact.join("\n")
end
def search_url
name = URI::escape(self.name)
return "https://www.google.com/search?q=#{name}&source=lnms&tbm=isch&sa=X"
end
def ingredients_fit?
ing = self.ingredients.to_s.split("\n")
return false if ing.length < 1
return false if ing.length > 20
return false unless ing.select { |x| x.length > 22 }.empty?
true
end
def process_fits?
pro = self.process.to_s
return false if pro.length < 1
return false if pro.length > 15 * 44
true
end
def picture_ok?
!picture_url.to_s.include?("coolinarika") and picture_url.to_s.length > 0
end
def is_it_valid?
ingredients_fit? and process_fits? and name.to_s.length <= 14 and duration.to_s.length <= 10 and picture_ok?
end
def ordinal_date
Date.ordinal(2014,self.ordinal)
end
def ingredients_safe
unless self.ingredients.nil?
ingred = self.ingredients.gsub("\r\n", "/").gsub("|"," ").gsub("/", "\n")
return ingred
end
""
end
private
def setup_ordinal
self.ordinal = Recipe.all.length + 1
end
end