lib/picturehouse_uk/cinema.rb in picturehouse_uk-1.0.2 vs lib/picturehouse_uk/cinema.rb in picturehouse_uk-2.0.0

- old
+ new

@@ -1,9 +1,12 @@ module PicturehouseUk - # The object representing a cinema on the Picturehouse UK website class Cinema + # address css + ADDRESS_CSS = '.box6 .txt6' + # cinema link css + CINEMA_LINKS_CSS = '#cinemalisthome .cinemas a' # @return [String] the brand of the cinema attr_reader :brand # @return [String] the id of the cinema on the Picturehouse website attr_reader :id @@ -12,83 +15,80 @@ # @return [String] the slug of the cinema attr_reader :slug # @return [String] the url of the cinema on the Picturehouse website attr_reader :url - # @param [String] id cinema id from the site - # @param [String] name cinema name - # @param [String] url url on Picturehouse website + # @param [Hash] options id, name and url of the cinemas # @return [PicturehouseUk::Cinema] - def initialize(id, name, url) + def initialize(options) @brand = 'Picturehouse' - @id = id - @name = name - @slug = name.downcase.gsub(/[^0-9a-z ]/,'').gsub(/\s+/, '-') - @url = (url[0] == '/') ? "http://www.picturehouses.co.uk#{url}" : url + @id = options[:id] + @name = options[:name] + @slug = @name.downcase.gsub(/[^0-9a-z ]/, '').gsub(/\s+/, '-') + @url = if options[:url][0] == '/' + "http://www.picturehouses.co.uk#{options[:url]}" + else + options[:url] + end end # Return basic cinema information for all cinemas # @return [Array<PicturehouseUk::Cinema>] # @example # PicturehouseUk::Cinema.all - # # => [<PicturehouseUK::Cinema brand="Picturehouse" name="Duke's At Komedia" slug="dukes-at-komedia" id="Dukes_At_Komedia" url="...">, #=> <PicturehouseUK::Cinema brand="Picturehouse" name="Duke o York's" slug="duke-of-yorks" id="Duke_Of_Yorks" url="...">, ...] + # # => [<PicturehouseUK::Cinema ...>, <PicturehouseUK::Cinema ...>, ...] def self.all - cinema_links.map do |link| - new_from_link link - end + cinema_links.map { |link| new_from_link(link) } end # Find a single cinema # @param [String] id the cinema id as used on the picturehouses.co.uk website # @return [PicturehouseUk::Cinema, nil] # @example # PicturehouseUk::Cinema.find('Dukes_At_Komedia') - # # => <PicturehouseUK::Cinema brand="Picturehouse" name="Duke's At Komedia" slug="dukes-at-komedia" id="Dukes_At_Komedia" url="..."> + # # => <PicturehouseUK::Cinema ...> def self.find(id) - all.select { |cinema| cinema.id == id }[0] + all.find { |cinema| cinema.id == id } end # Address of the cinema # @return [Hash] of different address parts # @example # cinema = PicturehouseUk::Cinema.find('Dukes_At_Komedia') # cinema.adr - # #=> { street_address: '44-47 Gardner Street', extended_address: 'North Laine', locality: 'Brighton', postal_code: 'BN1 1UN', country_name: 'United Kingdom' } - # @note Uses the standard method naming as at http://microformats.org/wiki/adr + # #=> { + # street_address: '44-47 Gardner Street', + # extended_address: 'North Laine', + # locality: 'Brighton', + # postal_code: 'BN1 1UN', + # country_name: 'United Kingdom' + # } + # @note Uses method naming as at http://microformats.org/wiki/adr def adr - { - street_address: street_address, - extended_address: extended_address, - locality: locality, - postal_code: postal_code, - country: 'United Kingdom' - } + PicturehouseUk::Internal::AddressParser.new(address_node.to_s).address end alias_method :address, :adr # The second address line of of the cinema # @return [String, nil] # @example # cinema = PicturehouseUk::Cinema.find('Dukes_At_Komedia') # cinema.extended_address # #=> 'North Laine' - # @note Uses the standard method naming as at http://microformats.org/wiki/adr + # @note Uses method naming as at http://microformats.org/wiki/adr def extended_address - address_strings.length > 3 ? address_strings[1] : nil + address[:extended_address] end # Films with showings scheduled at this cinema # @return [Array<PicturehouseUk::Film>] # @example # cinema = PicturehouseUk::Cinema.find('Dukes_At_Komedia') # cinema.films - # # => [<PicturehouseUk::Film name="Iron Man 3">, <PicturehouseUk::Film name="Star Trek Into Darkness">] + # # => [<PicturehouseUk::Film ...>, <PicturehouseUk::Film ...>, ...] def films - film_nodes.map do |node| - parser = PicturehouseUk::Internal::FilmWithScreeningsParser.new node.to_s - PicturehouseUk::Film.new parser.film_name - end.uniq + PicturehouseUk::Film.at(@id) end # The name of the cinema (might include brand) # @return [String] # @example @@ -105,121 +105,72 @@ # cinema = PicturehouseUk::Cinema.find('Dukes_At_Komedia') # cinema.locality # #=> 'Brighton' # @note Uses the standard method naming as at http://microformats.org/wiki/adr def locality - address_strings[-2] + address[:locality] end # Post code of the cinema # @return [String] # @example # cinema = PicturehouseUk::Cinema.find('Dukes_At_Komedia') # cinema.postal_code # #=> 'BN1 1UN' # @note Uses the standard method naming as at http://microformats.org/wiki/adr def postal_code - address_strings[-1] + address[:postal_code] end # All planned screenings # @return [Array<PicturehouseUk::Screening>] # @example # cinema = PicturehouseUk::Cinema.find('Dukes_At_Komedia') # cinema.screenings - # # => [<PicturehouseUk::Screening film_name="Iron Man 3" cinema_name="Duke's At Komedia" when="..." variant="...">, <PicturehouseUk::Screening ...>] + # # => [<PicturehouseUk::Screening ...>, <PicturehouseUk::Screening ...>] def screenings - film_nodes.map do |node| - parser = PicturehouseUk::Internal::FilmWithScreeningsParser.new node.to_s - parser.showings.map do |screening_type, times| - times.map do |time| - variant = screening_type == '2d' ? nil : screening_type - PicturehouseUk::Screening.new parser.film_name, self.name, time, variant - end - end - end.flatten + PicturehouseUk::Screening.at(@id) end # The street adress of the cinema # @return a String # @example # cinema = PicturehouseUk::Cinema.find('Dukes_At_Komedia') # cinema.street_address # #=> '44-47 Gardner Street' # @note Uses the standard method naming as at http://microformats.org/wiki/adr def street_address - address_strings[0] + address[:street_address] end private def self.cinema_links - parsed_homepage.css('#cinemalisthome .cinemas a') + home_doc.css(CINEMA_LINKS_CSS) end - def self.homepage_response - @homepage_response ||= HTTParty.get('http://www.picturehouses.co.uk/') + def self.home_doc + @home_doc ||= Nokogiri::HTML(website.home) end - def self.new_from_link(link) - url = link.get_attribute('href') - id = url.match(/\/cinema\/(.+)\/$/)[1] - name = link.css('span:nth-child(2)').text - new id, name, url + def self.website + @website ||= PicturehouseUk::Internal::Website.new end - def self.parsed_homepage - Nokogiri::HTML(homepage_response) + def self.new_from_link(link) + url = link.get_attribute('href') + new( + id: url.match(%r{/cinema/(.+)/$})[1], + name: link.css('span:nth-child(2)').text, + url: url + ) end - def address_parts - if pure_address_parts.length > 0 && pure_address_parts[0].match(/\d+\Z/) - ["#{pure_address_parts[0]} #{pure_address_parts[1]}"] + pure_address_parts[2..-1] - else - pure_address_parts - end - end - - def address_strings - if address_parts && address_parts.length > 0 - address_parts[0..post_code_index] - else - # this is a horrendous hack for Hackney Picturehouse - address_node.css('p').to_s.split('Box Office')[0].split('<br> ')[1..-1] - end - end - def address_node - parsed_contact_us.css('.box6 .txt6') + @address_node ||= contact_us_doc.css(ADDRESS_CSS) end - def contact_us_response - @contact_us_response ||= HTTParty.get("#{@url}Hires_Info/Contact_Us/") - end - - def cinema_response - @cinema_response ||= HTTParty.get(@url) - end - - def film_nodes - parsed_cinema.css('.box8_content .largelist .item') - end - - def parsed_cinema - Nokogiri::HTML(cinema_response) - end - - def parsed_contact_us - Nokogiri::HTML(contact_us_response) - end - - def post_code_index - address_parts.index { |e| e.match /[A-Z]{1,2}\d{1,2}[A-Z]?\s\d{1,2}[A-Z]{1,2}/ } - end - - def pure_address_parts - @pure_address_parts = address_node.css('.cinemaListBox').map do |e| - e.children[0].to_s - end.select { |e| e != '' } + def contact_us_doc + @contact_us_doc ||= Nokogiri::HTML(self.class.website.contact_us(id)) end end end