1

I am attempting to scrape data from a website. I am running rake from index.html.erb

 <% Article.run_rake("fetch_games") %>

I have it defined in a .rb file. Here is article.rb

require 'rake'
require 'rubygems'
#load './lib/tasks/article_task.rake'

class Article < ActiveRecord::Base

 def self.run_rake(fetch_games)
  Rails.root + "lib/tasks/article_task.rake"
  Rake::Task["fetch_games"].invoke
 end
end

And here is the rakefile itself: article_task.rake

desc "Fetch Games"
task :fetch_games => :environment do 

 require 'nokogiri'
 require 'open-uri'

 url = "http://espn.go.com/nba/schedule"
 data = Nokogiri::HTML(open(url))
 games = data.css('.responsive-table-wrap')

 games.each do |game|

 #check for a listing
 if !game.at_css("caption").nil?
    #Date
    puts game.at_css("caption").text
 else
    puts "No Listing"   
 end

 #check for the team name
 if !game.at_css(".team-name").nil?
    #Team name
    puts game.at_css(".team-name").text
 else
    puts "No Games Scheduled"
 end
    #empty
    puts ""
 end
end

When I run this from the terminal it pulls what I need. But when I try to run it through rails server it gives me this error:

Runtime Error

What am I doing wrong? New to ruby/rails btw

Lilspree
  • 135
  • 2
  • 14

4 Answers4

3

We need to load_tasks before you run that. You comment out that line -

 load './lib/tasks/article_task.rake'

Do this -

  require 'rake'
  require 'rubygems'
  load './lib/tasks/article_task.rake'

  class Article < ActiveRecord::Base

    def self.run_rake(fetch_games)
      Rake::Task["fetch_games"].invoke
    end
  end
Amit Suroliya
  • 1,515
  • 1
  • 11
  • 21
  • Tried this first. The new error is `Don't know how to load 'environment'` – Lilspree May 13 '15 at 19:21
  • @Lilspree If environment is not available. Remove `environment` in task file for force task >>>>> task :fetch_games do – Amit Suroliya May 13 '15 at 20:10
  • I did this and I even tried loading the environment even though it should already be loaded. I don't get any errors when I run the above answer. I believe its running but just not displaying. Could it be because I am trying to display it from a `.html.erb` file? – Lilspree May 14 '15 at 00:05
  • Do `<%= Article.run_rake("fetch_games") %>` instead of `<% Article.run_rake("fetch_games") %>` for display in view file. – Amit Suroliya May 14 '15 at 05:10
1

I think this thread can help: Run rake task in controller

Apparently, you have to call something like that before you call your task:

NameOfYourApp::Application.load_tasks
Community
  • 1
  • 1
d34n5
  • 1,308
  • 10
  • 18
  • 1
    That loads every task, I just want to load a specific one with `load './lib/tasks/article_task.rake'` – Lilspree May 14 '15 at 00:10
1

Rather than go through the contortions needed to load up an invoke a Rake task, you could move the code into a separate unit and load it in the model (and the Rake task, if you need it).

module GameFetcher
  def fetch
    ...
  end
end

class Article < ActiveRecord::Base
  extend GameFetcher
  ...
end

Article.fetch

This also makes it easier to write a unit test for the fetch logic.

zetetic
  • 47,184
  • 10
  • 111
  • 119
1

I went in another direction from the rake file. I defined it in articles controller and called it in index.html.erb. It now displays in the rails server.

articles_controller.rb

require 'nokogiri'
require 'open-uri'

def index
    url = "http://espn.go.com/nba/schedule"
    data = Nokogiri::HTML(open(url))
    @games = data.css('.responsive-table-wrap')
end

index.html.erb

<% @games.each do |game| %>
        <% if !game.at_css("caption").nil? %>
          <%= game.at_css("caption").text %>
        <% else %>
          <%= 'No Games Sheduled' %>
          <% end %>
        <% end %>
Lilspree
  • 135
  • 2
  • 14