-1

I have problem with my app in Rails 5. I create class scrape.rb which scraping HTML via Nokogiri gem and can save this data in another model, but when I create new object in rails console this return nil and don't scrap any value:

2.3.0 :018 > s = Scrape.new
 => #<Scrape:0x007fba68b79e98>
2.3.0 :019 > s.scrape_new_movie
 => nil
2.3.0 :020 >

Here is scrape.rb model

 class Scrape
  attr_accessor :title, :vote, :image_url, :description,

  def scrape_new_movie
    begin
      doc = Nokogiri::HTML(open("https://zalukaj.com/zalukaj-film/26280/barbie_w_wiecie_gier_barbie_video_game_hero_2017_.html").read, nil, 'utf-8')
      doc.css('script').remove
      self.title = doc.css('#pw_title.about_movie_title').text
      v = doc.css('#success_vote').text
      self.vote = v.slice(2...5)
      self.image_url = doc.css('.about_movie img').attr('src').text
      self.description = doc.css('#pw_description.e_s3k').text
      return true
      rescue Exception => e
      self.failure = "Something went wrong with the scrape"
    end
  end

  def save_movie
    movie = Movie.new(
      title: self.title,
      vote: self.vote,
      image_url: self.image_url,
      description: self.description
    )
    movie.save
  end
end
the Tin Man
  • 158,662
  • 42
  • 215
  • 303

3 Answers3

0

Replace the method:

  def scrape_new_movie
    begin
      doc = Nokogiri::HTML(open("https://zalukaj.com/zalukaj-film/26280/barbie_w_wiecie_gier_barbie_video_game_hero_2017_.html").read, nil, 'utf-8')
      doc.css('script').remove
      self.title = doc.css('#pw_title.about_movie_title').text
      v = doc.css('#success_vote').text
      self.vote = v.slice(2...5)
      self.image_url = doc.css('.about_movie img').attr('src').text
      self.description = doc.css('#pw_description.e_s3k').text
      return true
      rescue Exception => e
      self.failure = "Something went wrong with the scrape"
    end
  end

With

  def scrape_new_movie
    doc = Nokogiri::HTML(open("https://zalukaj.com/zalukaj-film/26280/barbie_w_wiecie_gier_barbie_video_game_hero_2017_.html").read, nil, 'utf-8')
    doc.css('script').remove
    self.title = doc.css('#pw_title.about_movie_title').text
    v = doc.css('#success_vote').text
    self.vote = v.slice(2...5)
    self.image_url = doc.css('.about_movie img').attr('src').text
    self.description = doc.css('#pw_description.e_s3k').text
    return true
  end

Then any errors that occur will bubble up and be displayed in a way that allows you to debug what the problem is.

This is a good example of why you should never do rescue Exception as that always makes it harder to debug a problem. See: Why is it a bad style to `rescue Exception => e` in Ruby?

Community
  • 1
  • 1
ReggieB
  • 8,100
  • 3
  • 38
  • 46
0

With how you set it up there is no need to call the literal name of the class. Just add self. to the methods and don't call new. There is also quite a bit of bugs in this script if you want to debug this I would also raise the exception message. You should also change self.title = to @title = or if you want to keep self.title you need to add class to inherit from self and put attr_accessor inside that class.

class Scrape
  class << self
    attr_accessor :title, :vote, :image_url, :description, failure
  end 

  def self.scrape_new_movie
    begin
      doc = Nokogiri::HTML(open("https://zalukaj.com/zalukaj-film/26280/barbie_w_wiecie_gier_barbie_video_game_hero_2017_.html").read, nil, 'utf-8')
      doc.css('script').remove
      self.title = doc.css('#pw_title.about_movie_title').text
      v = doc.css('#success_vote').text
      self.vote = v.slice(2...5)
      self.image_url = doc.css('.about_movie img').attr('src').text
      self.description = doc.css('#pw_description.e_s3k').text
      return true
      rescue Exception => e
        raise e 
      end
    end

    def self.save_movie
      movie = Movie.new(
        title: self.title,
        vote: self.vote,
        image_url: self.image_url,
        description: self.description
      )
      movie.save
    end
 end      
 Scrape.scrape_new_movie
David Gross
  • 1,863
  • 10
  • 14
0

The reason it is returning nil, is because you end your attr_accessor with a comma. The variable failure is also undefined, so I assume you want an attr_accessor for that as well.

You should change

  attr_accessor :title, :vote, :image_url, :description,

to

  attr_accessor :title, :vote, :image_url, :description, :failure
Lasse Sviland
  • 1,479
  • 11
  • 23