Alex Zakruzhetskyi Alex Zakruzhetskyi - 4 days ago 5
Ruby Question

NameError: uninitialized constant Class::Class in a rake-task

I'm trying to make a rake-task in a such way:

require 'open-uri'

namespace :news_parser do
desc 'Parsing news from 6 news sites'
task :parse_news do
load 'lib/news_parser.rb'
ProcherkParser.new.save_novelties
VikkaParser.new.save_novelties
InfomistParser.new.save_novelties
ZmiParser.new.save_novelties
VycherpnoParser.new.save_novelties
ProvceParser.new.save_novelties
end
end


In my
lib/news_parser.rb
I have classes and instance methods, which perfectly work in a rails console, by doing the following:

load 'lib/news_parser.rb'
ProcherkParser.new.save_novelties


It saves to my db all the information I need. But how can I do it in a rake-task? Any help would be appreciate. Thanks.

Answer

Does it work when you replace

task :parse_news do

with

task :parse_news => :environment do

?

It will load your Rails environment before your task, and your code should work just like in the rails console.

Also, you could DRY your code a bit :

require 'open-uri'

namespace :news_parser do
  desc 'Parsing news from 6 news sites'
  task :parse_news => :environment do
    load 'lib/news_parser.rb'
    [ProcherkParser, VikkaParser, InfomistParser, ZmiParser, VycherpnoParser, ProvceParser].each do |parser_klass|
      parser_klass.new.save_novelties
    end
  end
end
Comments