Sha256: aeafe5919f11d7e2122684c662b9a848ac6f6c37a324825de74c707a3b109ade

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require "fandango/version"

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 == ''
      response = request(postal_code)
      raise BadResponse.new(response) unless response.status.first == '200'
      source = response.read
      parse source
    end

    def request(postal_code)
      open(url_for_postal_code(postal_code))
    end

    # 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:\n#{response.inspect}"
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fandango-1.0.0 lib/fandango.rb