class MovieFetcher::Fetcher

Public Class Methods

new(endpoint = nil) click to toggle source
# File lib/movie_fetcher/fetcher.rb, line 6
def initialize(endpoint = nil)
  @endpoint = endpoint ||
    "https://www.google.pl/movies?hl=pl&near=Warszawa&dq=filmy+warszawa&sort=1&q=filmy&sa=X&ved=0CB4QxQMoAGoVChMIs-DMheDaxgIVQXdyCh3tcwCJ"
end

Public Instance Methods

fetch() click to toggle source
# File lib/movie_fetcher/fetcher.rb, line 11
def fetch
  get_google_html
  parse_html
  find_closest_movie
end

Private Instance Methods

find_closest_movie() click to toggle source
# File lib/movie_fetcher/fetcher.rb, line 47
def find_closest_movie
  now = Time.now.strftime('%H:%m')
  earliest_show_time = nil
  sort_showtimes.reject do |showtime|
    too_early =  showtime.time < now
    earliest_show_time ||= !too_early && showtime.time
    too_early || ( earliest_show_time && showtime.time > earliest_show_time )
  end.map &:to_s
end
get_google_html() click to toggle source
# File lib/movie_fetcher/fetcher.rb, line 19
def get_google_html
  @doc = open(@endpoint)
end
parse_html() click to toggle source
# File lib/movie_fetcher/fetcher.rb, line 23
def parse_html
  @showtimes = []
  doc = Nokogiri::HTML(@doc)
  movies = doc.css(".movie")
  movies.each do |movie|
    name = movie.css('[itemprop="name"] a').first.content
    movie.css('.theater').each do |theater|
      theater_name = theater.css('.name a').first.content
      theater.css('.times span').each do |start_time|
        time = start_time.content
        if time =~ /(\d\d:\d\d)/ && $1
          @showtimes << Showtime.new(name, theater_name, $1)
        end
      end
    end
  end
end
sort_showtimes() click to toggle source
# File lib/movie_fetcher/fetcher.rb, line 41
def sort_showtimes
  @showtimes.sort do |p, n|
    p.time <=> n.time
  end
end