Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails rake task fails just in production: "NoMethodError: private method `open' called for URI:Module"

I am coding a rake task which function is getting info from other webpage. To do that I use open-uri and nokogiri. I have tested in development and it does the job, but then I deploy to the production server and fails.

This is the code:

require 'open-uri'
require 'nokogiri'

desc 'recover eventos llerena'
task recover_eventos_llerena: :environment  do

  enlaces = []

  # Getting index and all links
  url = open(URI.parse("https://llerena.org/eventos/lista"))
  page = Nokogiri::HTML(url)

  page.css("a.fusion-read-more").each do |line|
    enlaces.push(line.attr('href'))
  end

  enlaces = enlaces.uniq

  #Inspecting everyone of them
  enlaces.each do |link|
    url = open(URI.parse(link))
    page = Nokogiri::HTML(url)

    title = page.css("h1").text
    if Evento.find_by_titulo(title) == nil

      description = page.css(".tribe-events-single-event-description.tribe-events-content.entry-content.description p").text
      date = page.css(".tribe-events-abbr").attr('title')
      image = page.css(".size-full").attr('src')

      Evento.create!(
        titulo: title,
        remote_imgevento_url: image,
        info: description,
        fecha: Date.parse(date)
      )

    end
  end

end

When checking cron_error_log I get:

rake aborted!
NoMethodError: private method `open' called for URI:Module
/home/deploy/guialocal/releases/20200204193434/lib/tasks/example.rake:23:in `block in <top (required)>'
/home/deploy/guialocal/shared/bundle/ruby/2.4.0/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/cli/exec.rb:74:in `load'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/cli/exec.rb:74:in `kernel_load'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/cli/exec.rb:28:in `run'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/cli.rb:463:in `exec'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/cli.rb:27:in `dispatch'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/cli.rb:18:in `start'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/exe/bundle:30:in `block in <top (required)>'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/friendly_errors.rb:124:in `with_friendly_errors'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/exe/bundle:22:in `<top (required)>'
/home/deploy/.rvm/gems/ruby-2.4.4/bin/bundle:23:in `load'
/home/deploy/.rvm/gems/ruby-2.4.4/bin/bundle:23:in `<main>'
Tasks: TOP => recover_eventos_llerena
(See full trace by running task with --trace)

And finally I have tried to run console in production getting:

2.4.0 :001 > Rake::Task['recover_eventos_llerena'].execute
NameError: uninitialized constant Rake::Task
    from (irb):1

In rails 2.7.0 there is a warning for future deprecation and I have change also the proposed new way (without results):

url = URI.open("https://llerena.org/eventos/lista")

Why is this happening?

like image 850
Juanse Cora Avatar asked Dec 23 '22 19:12

Juanse Cora


1 Answers

You should try to simply add

require 'open-uri'

at the start of your script

like image 128
unplugandplay Avatar answered Jan 14 '23 03:01

unplugandplay