Class: GoogleMovies47::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/google_movies47/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language = 'en') ⇒ Parser

Returns a new instance of Parser.



10
11
12
13
14
15
16
# File 'lib/google_movies47/parser.rb', line 10

def initialize(language = 'en')
  @language = language
  @language_parser = GoogleMovies47::LanguageParser.new(language)
  @genre_parser = GoogleMovies47::GenreParser.new(language)
  @theaters = Hash.new
  @movies = Hash.new
end

Instance Attribute Details

#moviesObject

Returns the value of attribute movies.



8
9
10
# File 'lib/google_movies47/parser.rb', line 8

def movies
  @movies
end

#theatersObject

Returns the value of attribute theaters.



8
9
10
# File 'lib/google_movies47/parser.rb', line 8

def theaters
  @theaters
end

Instance Method Details

#parse_movie_info(info_line) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/google_movies47/parser.rb', line 52

def parse_movie_info(info_line)
  duration = ChronicDuration.parse(info_line)
  genre = @genre_parser.parse(info_line)
  language = @language_parser.parse(info_line)
  
  { :duration => duration, :genre => genre, :language => language }
end

#parse_show_times(doc) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/google_movies47/parser.rb', line 18

def parse_show_times(doc)
  theater_elements = doc.xpath(
  "//div[@class='movie_results']/div[@class='theater' and .//h2/a]"
  )
  y = 0
  theater_elements.each do |t|
    theater_name = t.search(".//h2[@class='name']/a/text()").text
    theater_info = t.search(".//div[@class='info']/text()").text
    showtimes = []
    movie_elements = t.search(".//div[@class='showtimes']//div[@class='movie']")
    
    movie_elements.each do |m|
      movie_name = m.search(".//div[@class='name']/a/text()").text.strip
      movie_info_line = m.search(".//span[@class='info']/text()").text
      movie_info = parse_movie_info(movie_info_line)
      
      @movies[movie_name] = { :name => movie_name, :info => movie_info } if @movies[movie_name].nil?
      
      movie_times = m.search(".//div[@class='times']/span/text()")
      times = []
      movie_times.each do |mt|
        time = mt.text.strip
        times << time
      end
      
      showtimes << { :name => movie_name, :language => movie_info[:language], :times => times }
    end
    
    @theaters[y] = { :name => theater_name, :info => theater_info, :movies => showtimes }
    y = y + 1
  end
  
end