Class: BestMovies::Movie

Inherits:
Object
  • Object
show all
Defined in:
lib/best_movie_year/movie.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = nil, desc = nil, url = nil) ⇒ Movie

Returns a new instance of Movie.



6
7
8
9
10
11
# 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

Instance Attribute Details

#descObject

Returns the value of attribute desc.



2
3
4
# File 'lib/best_movie_year/movie.rb', line 2

def desc
  @desc
end

#titleObject

Returns the value of attribute title.



2
3
4
# File 'lib/best_movie_year/movie.rb', line 2

def title
  @title
end

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/best_movie_year/movie.rb', line 2

def url
  @url
end

Class Method Details

.allObject



17
18
19
# File 'lib/best_movie_year/movie.rb', line 17

def self.all
  @@all
end

.create(movie_array) ⇒ Object



13
14
15
# 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

.reset_allObject



21
22
23
# File 'lib/best_movie_year/movie.rb', line 21

def self.reset_all
  @@all.clear
end

.scrape_desc(url) ⇒ Object



25
26
27
28
29
30
31
# 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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# 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