class BestMovies::Movie

Attributes

desc[RW]
title[RW]
url[RW]

Public Class Methods

all() click to toggle source
# File lib/best_movie_year/movie.rb, line 17
def self.all
  @@all
end
create(movie_array) click to toggle source
# File lib/best_movie_year/movie.rb, line 13
def self.create(movie_array)
  movie_array.each { |movie_hash| @movie = self.new(movie_hash) }
end
new(title = nil, desc = nil, url = nil) click to toggle source
# File lib/best_movie_year/movie.rb, line 6
def initialize(title = nil, desc = nil, url = nil)
  @title = title
  @desc = desc
  @url = url
  @@all << self
end
reset_all() click to toggle source
# File lib/best_movie_year/movie.rb, line 21
def self.reset_all
  @@all.clear
end
scrape_desc(url) click to toggle source
# File lib/best_movie_year/movie.rb, line 25
def self.scrape_desc(url)
  titles = {}
  doc = Nokogiri::HTML(open(url))
  titles[:desc] = doc.css("div#movieSynopsis.movie_synopsis.clamp.clamp-6").text.strip

  titles
end
scrape_movies(main_url) click to toggle source
# File lib/best_movie_year/movie.rb, line 33
def self.scrape_movies(main_url)
  movie_list = []
  doc = Nokogiri::HTML(open("#{main_url}"))
  movies = doc.css("table.table a.unstyled.articleLink")
  movies.each { |movie|
    movie_title = movie.text.split("            ").first(11).drop(1)
    movie_url = "https://www.rottentomatoes.com" + movie.attribute("href").value
    movie_desc = self.scrape_desc("#{movie_url}")
    movie_list << { title: movie_title[0], url: movie_url, desc: movie_desc }
  }

  movie_list
end