Search code examples
ruby-on-railsrubyrake

rails LoadError: cannot load such file -- lib/scraper


In my rails app, I have a rake task that scrapes data from another webpage. I want to move the method functionality out of the rake task into a ruby class or module. To do, I have the rake task in lib/tasks, then scraper.rb in lib. In the rake task, I have require 'lib/scraper' but this throws an error.

Here is my rake task:

require "lib/scraper"
namespace :some_namespace do
    desc "A description"
    task :scrape_info => :environment do
        scraper = Scraper.new
        scraper.scrape_info
    end
end

And the ruby scraper class:

require 'mechanize'
class Scraper
    def scrape_info
        mechanize = Mechanize.new

        # Scrape players from fox sports
        url = "someurl"

        # do some other stuff
    end
end

Solution

  • Your code should look as follows.

    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