lib/fandango.rb in fandango-0.2.1 vs lib/fandango.rb in fandango-1.0.0

- old
+ new

@@ -1,39 +1,50 @@ require "fandango/version" -require 'feedzirra' +require 'open-uri' +require 'nokogiri' + require 'fandango/parser' module Fandango class << self def movies_near(postal_code) raise ArgumentError, "postal code cannot be blank" if postal_code.nil? || postal_code == '' - feed = fetch_and_parse(postal_code) - feed.entries.map do |entry| - parser = Parser.new(entry) - hash = {} - hash[:theater] = parser.parse_theater - hash[:movies] = parser.parse_movies - hash - end + response = request(postal_code) + raise BadResponse.new(response) unless response.status.first == '200' + source = response.read + parse source end - private + def request(postal_code) + open(url_for_postal_code(postal_code)) + end - def fetch_and_parse(postal_code) - cleaned_postal_code = postal_code.to_s.gsub(' ', '') - feed = Feedzirra::Feed.fetch_and_parse("http://www.fandango.com/rss/moviesnearme_#{cleaned_postal_code}.rss") - raise BadResponse.new(feed) unless feed.respond_to?(:entries) - feed + # Given RSS source string, parse using Nokogiri. + # Return hash of theaters and movies playing at each.. + def parse(source) + Parser.parse(source) end + private + + def url_for_postal_code(postal_code) + cleaned_postal_code = clean_postal_code(postal_code) + "http://www.fandango.com/rss/moviesnearme_#{cleaned_postal_code}.rss" + end + + # Remove spaces. + def clean_postal_code(postal_code) + postal_code.to_s.gsub(' ', '') + end + end class BadResponse < StandardError def initialize(response) - super "Bad response: #{response.inspect}" + super "Bad response:\n#{response.inspect}" end end end