module TheatresEventCinemas
Parse Event Cinemas SessionTimes HTML into structured screening data.
Constants
- TIME_PATTERN
Public Instance Methods
Source
# File lib/theatres_event_cinemas.rb, line 35 def build_movie(title_text) rating = nil title = title_text.to_s.strip if (match = title.match(/\s+(G|PG|M|MA15\+|R18\+|CTC)\z/i)) rating = match[1].upcase title = title.sub(/\s+(G|PG|M|MA15\+|R18\+|CTC)\z/i, "").strip end { title: title, rating: rating, times: [] } end
Source
# File lib/theatres_event_cinemas.rb, line 72 def dedupe_times(times) seen = {} times.map { |time| normalize_time(time) }.each_with_object([]) do |time, list| next if time.empty? || seen[time] seen[time] = true list << time end end
Source
# File lib/theatres_event_cinemas.rb, line 55 def extract_times_for_movie(_doc, heading, movie) node = heading.next_element while node break if node.name.match?(/\Ah[1-6]\z/) text = node.text.to_s text.scan(TIME_PATTERN).each do |time| movie[:times] << normalize_time(time) end node = node.next_element end end
Source
# File lib/theatres_event_cinemas.rb, line 68 def normalize_time(time) time.to_s.strip.gsub(/\s+/, " ") end
Source
# File lib/theatres_event_cinemas.rb, line 11 def parse_sessions(html, cinema:, chain: "Event Cinemas") doc = Nokogiri::HTML(html.to_s) movies = [] doc.css("h2, h3").each do |heading| title_text = heading.text.to_s.strip next if title_text.empty? next if skip_heading?(title_text) movie = build_movie(title_text) extract_times_for_movie(doc, heading, movie) next if movie[:times].empty? movie[:times] = dedupe_times(movie[:times]) movies << movie end { cinema: cinema.to_s, chain: chain, movies: movies } end
Source
# File lib/theatres_event_cinemas.rb, line 46 def skip_heading?(title_text) lowered = title_text.downcase lowered.include?("filter") || lowered.include?("legend") || lowered.include?("sessions") || lowered.include?("promotions") || lowered.include?("events") end