在我的rails应用程序中,我有一个rake任务从另一个网页上删除数据。我想将方法功能从rake任务移到ruby类或模块中。要做的是,我在lib / tasks中有rake任务,然后是lib中的scraper.rb。在rake任务中,我需要'lib / scraper',但这会引发错误。
这是我的佣金任务:
require "lib/scraper"
namespace :some_namespace do
desc "A description"
task :scrape_info => :environment do
scraper = Scraper.new
scraper.scrape_info
end
end
红宝石刮刀类:
require 'mechanize'
class Scraper
def scrape_info
mechanize = Mechanize.new
# Scrape players from fox sports
url = "someurl"
# do some other stuff
end
end
答案 0 :(得分:1)
您的代码应如下所示。
<强> lib/scraper.rb
强>
require 'mechanize'
module Scraper
class Scraper
def scrape_info
end
end
end
<强> lib/tasks/some_namespace.rake
强>
namespace :some_namespace do
task some_task :environment do
include Scraper
end
end