lib/picturehouse_uk/cinema.rb in picturehouse_uk-3.0.14 vs lib/picturehouse_uk/cinema.rb in picturehouse_uk-4.0.0

- old
+ new

@@ -1,61 +1,45 @@ module PicturehouseUk # The object representing a cinema on the Picturehouse UK website - class Cinema + class Cinema < Cinebase::Cinema # address css - ADDRESS_CSS = '.static-content #contact-us + p:first' + ADDRESS_CSS = '.static-content #contact-us + p:first'.freeze # cinema link css - CINEMA_LINKS_CSS = '.footer .col-sm-3 option + option' + CINEMA_LINKS_CSS = '.footer .col-sm-3 option + option'.freeze - # @return [String] the brand of the cinema - attr_reader :brand - # @return [String] the id of the cinema on the Picturehouse website - attr_reader :id - # @return [String] the name of the cinema - attr_reader :name - # @return [String] the slug of the cinema - attr_reader :slug - # @return [String] the url of the cinema on the Picturehouse website - attr_reader :url + # @!attribute [r] id + # @return [Integer] the numeric id of the cinema on the Cineworld website - # @param [Hash] options id, name and url of the cinemas - # @return [PicturehouseUk::Cinema] - def initialize(options) - @brand = 'Picturehouse' - @id = options[:id] - @name = options[:name] - @slug = @name.downcase.gsub(/[^0-9a-z ]/, '').gsub(/\s+/, '-') - @url = if options[:url][0] == '/' - "http://www.picturehouses.com#{options[:url]}" - else - options[:url] - end - end + # @!method initialize(options) + # Constructor + # @param [String] id the cinema id of the cinema in capitalized snake case + # @return [PicturehouseUk::Cinema] # Return basic cinema information for all cinemas # @return [Array<PicturehouseUk::Cinema>] # @example # PicturehouseUk::Cinema.all - # # => [<PicturehouseUK::Cinema ...>, <PicturehouseUK::Cinema ...>, ...] + # #=> [<PicturehouseUk::Cinema>, <PicturehouseUk::Cinema>, ...] def self.all - cinema_links.map { |link| new_from_link(link) } + cinema_hash.keys.map { |id| new(id) } 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 ...> - def self.find(id) - all.find { |cinema| cinema.id == id } + # @api private + def self.cinema_hash + @cinema_hash ||= ListParser.new(cinema_links).to_hash end + # @!method address + # Address of the cinema + # @return [Hash] of different address parts + # @see #adr + # Address of the cinema - # @return [Hash] of different address parts + # @return [Hash] contains :street_address, :extended_address, + # :locality, :postal_code, :country # @example - # cinema = PicturehouseUk::Cinema.find('Dukes_At_Komedia') + # cinema = PicturehouseUk::Cinema.new('Dukes_At_Komedia') # cinema.adr # #=> { # street_address: '44-47 Gardner Street', # extended_address: 'North Laine', # locality: 'Brighton', @@ -65,124 +49,149 @@ # } # @note Uses method naming as at http://microformats.org/wiki/adr def adr PicturehouseUk::Internal::Parser::Address.new(address_node.to_s).address end - alias_method :address, :adr - # The second address line of of the cinema - # @return [String, nil] + # Brand of the cinema + # @return [String] which will always be 'Picturehouse' # @example - # cinema = PicturehouseUk::Cinema.find('Dukes_At_Komedia') - # cinema.extended_address - # #=> 'North Laine' - # @note Uses method naming as at http://microformats.org/wiki/adr - def extended_address - address[:extended_address] + # cinema = PicturehouseUk::Cinema.new('Dukes_At_Komedia') + # cinema.brand + # #=> 'Picturehouse' + def brand + 'Picturehouse'.freeze end - # Films with showings scheduled at this cinema - # @return [Array<PicturehouseUk::Film>] - # @example - # cinema = PicturehouseUk::Cinema.find('Dukes_At_Komedia') - # cinema.films - # # => [<PicturehouseUk::Film ...>, <PicturehouseUk::Film ...>, ...] - def films - PicturehouseUk::Film.at(@id) - end + # @!method country_name + # Country of the cinema + # @return [String] which will always be 'United Kingdom' + # @example + # cinema = PicturehouseUk::Cinema.new('Dukes_At_Komedia') + # cinema.country_name + # #=> 'United Kingdom' + # @!method extended_address + # The second address line of the cinema + # @return [String] + # @example + # cinema = PicturehouseUk::Cinema.new('Dukes_At_Komedia') + # cinema.extended_address + # #=> 'North Laine' + # The name of the cinema (might include brand) # @return [String] # @example - # cinema = PicturehouseUk::Cinema.find('Dukes_At_Komedia') + # cinema = PicturehouseUk::Cinema.new('Dukes_At_Komedia') # cinema.full_name # #=> "Duke's At Komedia" def full_name name end - # The locality (town) of the cinema - # @return [String] - # @example - # 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[:locality] - end + # @!method locality + # The locality (town) of the cinema + # @return [String] + # @example + # cinema = PicturehouseUk::Cinema.new('Dukes_At_Komedia') + # cinema.locality + # #=> 'Brighton' - # Post code of the cinema + # The name 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[:postal_code] + # cinema = PicturehouseUk::Cinema.new('Dukes_At_Komedia') + # cinema.name + # #=> "Duke's At Komedia" + def name + self.class.cinema_hash.fetch(id, {})[:name] end - # The region (county) of the cinema - # @return [String] - # @example - # cinema = PicturehouseUk::Cinema.find('Dukes_At_Komedia') - # cinema.region - # #=> 'East Sussex' - # @note Uses the standard method naming as at http://microformats.org/wiki/adr - def region - address[:region] - end + # @!method postal_code + # Post code of the cinema + # @return [String] + # @example + # cinema = PicturehouseUk::Cinema.new('Dukes_At_Komedia') + # cinema.postal_code + # #=> 'BN1 1UN' - # All planned screenings - # @return [Array<PicturehouseUk::Screening>] - # @example - # cinema = PicturehouseUk::Cinema.find('Dukes_At_Komedia') - # cinema.screenings - # # => [<PicturehouseUk::Screening ...>, <PicturehouseUk::Screening ...>] - def screenings - PicturehouseUk::Screening.at(@id) - end + # @!method region + # The region (county) of the cinema if provided + # @return [String] + # @example + # cinema = PicturehouseUk::Cinema.new('Dukes_At_Komedia') + # cinema.region + # #=> 'East Sussex' - # 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[:street_address] + # @!method slug + # The URL-able slug of the cinema + # @return [String] + # @example + # cinema = PicturehouseUk::Cinema.new('Dukes_At_Komedia') + # cinema.slug + # #=> 'dukes-at-komedia' + + # @!method street_address + # The street address of the cinema + # @return [String] + # @example + # cinema = PicturehouseUk::Cinema.new('Dukes_At_Komedia') + # cinema.street_address + # #=> '44-47 Gardner Street' + + # The url of the cinema on the Picturehouse website + # @return [String] + def url + "http://www.picturehouses.com#/cinema/#{id}" end private def self.cinema_links home_doc.css(CINEMA_LINKS_CSS) end + private_class_method :cinema_links def self.home_doc - @home_doc ||= Nokogiri::HTML(website.home) + @home_doc ||= + Nokogiri::HTML(PicturehouseUk::Internal::Website.new.home) end + private_class_method :home_doc - def self.website - @website ||= PicturehouseUk::Internal::Website.new - end - - def self.new_from_link(link) - url = link.get_attribute('data-href') - name = link.children.first.to_s.split(' — ')[1] - - new(id: url.match(%r{/cinema/(.+)$})[1], - name: name, - url: url) - end - def address_node @address_node ||= info_doc.css(ADDRESS_CSS) end def info_doc - @info_doc ||= Nokogiri::HTML(self.class.website.info(id)) + @info_doc ||= + Nokogiri::HTML(PicturehouseUk::Internal::Website.new.info(id)) + end + + # @api private + # Utility class to parse the links spat out from the options + class ListParser + def initialize(nodes) + @nodes = nodes + end + + def to_hash + @nodes.each_with_object({}) do |node, result| + result[id(node)] = { name: name(node), url: url(node) } + end + end + + private + + def id(node) + url(node).match(%r{/cinema/(.+)$})[1] + end + + def name(node) + node.children.first.to_s.split(' — ')[1] + end + + def url(node) + node.get_attribute('data-href') + end end end end