lib/fandango/movie.rb in fandango-1.0.0 vs lib/fandango/movie.rb in fandango-2.0.0

- old
+ new

@@ -1,30 +1,46 @@ module Fandango module Movie - class << self + module_function - # Return array of movie attributes. - def parse(description_node) - description_node.css('li').map do |li| - { - title: parse_title(li), - id: parse_id(li), - } - end + def parse(node) + a_tag = node.at_css('a.showtimes-movie-title') || node.at_css('a') + hash = { + title: parse_title(a_tag), + id: parse_id(a_tag), + } + + if a_tag.attr(:class)&.include?('showtimes-movie-title') + hash[:runtime] = parse_runtime(node) end - private + hash + end - def parse_title(li) - li.at_css('a').content - end + def parse_title(a_tag) + a_tag.content + end - # E.g. '141081' in fandango.com/the+adventures+of+tintin+3d_141081/movietimes - def parse_id(li) - li.at_css('a')['href'].match(%r{fandango\.com/.*_(?<id>\d+)/movietimes})[:id] - end + # E.g. '141081' in fandango.com/the+adventures+of+tintin+3d_141081/movietimes + def parse_id(a_tag) + a_tag['href'].match(%r{fandango\.com/.*_(?<id>\d+)/})[:id] + end + # <div class="showtimes-movie-rating-runtime"> + # <!-- Display rating --> + # R , + # <!-- Display runtime --> + # 1 hr 41 min + # </div> + def parse_runtime(node) + rating_runtime = node.at_css('.showtimes-movie-rating-runtime') + %r{(?<hour>\d+)\s+hr\s+(?<min>\d+)} =~ rating_runtime.content + begin + Integer(hour) * 60 + Integer(min) + rescue TypeError + #puts rating_runtime + end end end end