class Destination

Attributes

city[RW]
city_description[RW]
country[RW]
list_destinations[RW]
rank[RW]

Public Class Methods

destination_information(input) click to toggle source
# File lib/top_25_travel_destinations/destination.rb, line 43
def self.destination_information(input)
  @get_dest_det ||= self.get_page.css('.tcDest_details') 
 
  dest = []
  get_destinations.css("##{input}").each do |x|
    dest << x.css('div.tcRank').text
    dest << x.css('div.tcCity').text
    dest << x.css('div.tcCountry').text
  end
  @get_dest_det.css("#tcDest#{input}_details").each do |d|
    dest << d.css('div.description p').text.gsub("Learn More >", "")
  end

  input = nil
  until input == "menu"
  puts "What would you like to know about #{dest[1]}?"
  puts "rank, country, description, or menu"
  input = gets.chomp.downcase
    if input == "rank"
      puts "----------------------"
      puts "The rank of #{dest[1]} is: #{dest[0]}"
      puts "----------------------"

    elsif input == "country"
      puts "----------------------"
      puts "#{dest[1]} is located in #{dest[2]}"
      puts "----------------------"
    elsif input == "description"
      puts "----------------------"
      puts dest[3]
      puts "----------------------"
    elsif input == "menu"
      list_destinations
    else
      puts "Incorrect input, please try again"  
    end
  end
end
find_destination_by_name(name) click to toggle source
# File lib/top_25_travel_destinations/destination.rb, line 12
def self.find_destination_by_name(name)
  @@all.detect{|d| d.name == name}
end
get_destinations() click to toggle source
# File lib/top_25_travel_destinations/destination.rb, line 28
def self.get_destinations
  @get_dest ||= self.get_page.css('.tcDest.clickable')
end
get_page() click to toggle source
# File lib/top_25_travel_destinations/destination.rb, line 17
def self.get_page
  # the first time I call this method, i set an instance
  # the second time i want to check if that instance variable exists
    # if yes, return it
  # else
    # create
  @doc ||= Nokogiri::HTML(open("http://www.tripadvisor.com/TravelersChoice-Destinations"))
  # Memoization - poor man caching
end
list_destinations() click to toggle source
# File lib/top_25_travel_destinations/destination.rb, line 33
def self.list_destinations 
  #lists all the destinations
 @counter = 1
  get_destinations.each do |x|
    @dest = x.css('div.tcCity').text
    puts "#{@counter}. #{@dest}"
    @counter += 1
  end
end