嘗試專注於在將它移動到gem/plugin之前先讓它工作。 另外,忘記接口/抽象類 - 只寫代碼做的事情。 你的模型應該知道的唯一事情是如果這是遠程配方,以及什麼是網址。 你可以把所有的刮碼放在app/scrapers中。下面是一個例子實施綱要:
class RecipePage
def new(url)
@url = url
@parser = get_parser
end
def get_attributes
raise "trying to scrape unknown site" unless @parser
@parser.recipe_attributes(get_html)
end
private
def get_html
#this uses your favorite http library to get html from the @url
end
def get_parser(url)
#this matches url to your class, ie returns domain camelized, or nil if you are not handling particular site yet
return EpicurusComParser
end
end
class EpicurusComParser
def self.recipe_attributes(html)
# this does the hard job of querying html and moving
# all the code to get title, text, image of recipe and return hash
{
:title => "recipe title",
:text => "recipe text",
:image => "recipe_image_url",
}
end
end
在模型
然後
class Recipe
after_create :scrape_recipe, :if => :recipe_url
private
def scrape_recipe
# do that in background - ie in DelayedJob
recipe_page = RecipePage.new(self.recipe_url)
self.update_attributes(recipe_page.get_attributes.merge(:scraped => true))
end
end
然後你就可以創造更多的解析器,即CookComParser等