Class: CommandLineInterface
- Inherits:
-
Object
- Object
- CommandLineInterface
- Defined in:
- lib/command_line_interface.rb
Constant Summary collapse
- CATEGORIES =
Category.get_category_list
Instance Method Summary collapse
- #format_categories(array, string = "") ⇒ Object
- #get_location ⇒ Object
- #get_preferences(input) ⇒ Object
- #get_users ⇒ Object
- #make_recommendation(restaurant) ⇒ Object
- #run ⇒ Object
- #suggested_categories(input, categories = CATEGORIES, characters = 0) ⇒ Object
- #yes?(prompt) ⇒ Boolean
Instance Method Details
#format_categories(array, string = "") ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/command_line_interface.rb', line 90 def format_categories(array, string = "") head = "\"" + array[0].split.map{|word| word.capitalize}.join(" ") + "\"" tail = array[1..-1] if tail.empty? string == ""? head : string + ", or " + head else string = string == ""? head : string + ", " + head format_categories(tail, string) end end |
#get_location ⇒ Object
17 18 19 20 |
# File 'lib/command_line_interface.rb', line 17 def get_location puts "Hi! Where do you want to have dinner tonight?" location = gets.strip end |
#get_preferences(input) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/command_line_interface.rb', line 52 def get_preferences(input) validated_categories = [] if input.strip == "" validated_categories else user_categories = input .split(/[,][ ]*/) .select {|string| !string.include? ","} .map {|string| string.strip} user_categories.each do |user_category| if CATEGORIES.map{|category| category.title.downcase}.include? user_category.downcase validated_categories << user_category.downcase else suggested_categories = suggested_categories(user_category).map{|category| category.title} formatted_categories = format_categories(suggested_categories) puts "I'm sorry, I didn't understand \"#{user_category}\". Did you mean #{formatted_categories}?" recover_category = gets .split(/[,][ ]*/) .select {|string| !string.include? ","} .first get_preferences(recover_category).each {|category| validated_categories << category} end end validated_categories.uniq end end |
#get_users ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/command_line_interface.rb', line 22 def get_users users = [] loop do puts "What's your name?" name = gets.strip puts "What are you in the mood for today?" likes = get_preferences(gets) puts "What don't you want to eat?" dislikes = get_preferences(gets) user = User.new(name, likes, dislikes) users << user break unless yes?("Is anyone else joining you?") end users end |
#make_recommendation(restaurant) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/command_line_interface.rb', line 38 def make_recommendation(restaurant) puts "How about?" puts "" puts restaurant.name puts "#{restaurant.} star rating, #{restaurant.review_count} reviews" puts restaurant.categories.map{|category| category.title}.join(", ") puts "" puts restaurant.display_address puts restaurant.display_phone puts "" puts "\"#{restaurant.snippet_text}\"" puts "" end |
#run ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/command_line_interface.rb', line 5 def run location = get_location users = get_users whats_for_dinner = WhatsForDinner.new(location, users) loop do restaurant = whats_for_dinner.ask make_recommendation(restaurant) break if yes?("Sound good?") end puts "Great! Thanks for using What's For Dinner?, powered by Yelp." end |
#suggested_categories(input, categories = CATEGORIES, characters = 0) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/command_line_interface.rb', line 79 def suggested_categories(input, categories = CATEGORIES, characters = 0) suggestions = categories .select {|category| category.title.downcase[0, characters] == input.downcase[0, characters]} .compact if suggestions.empty? || suggested_categories(input, suggestions, characters + 1).empty? suggestions else suggested_categories(input, suggestions, characters + 1) end end |
#yes?(prompt) ⇒ Boolean
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/command_line_interface.rb', line 101 def yes?(prompt) puts prompt case gets.strip.downcase when "yes" true when "no" false else puts "I'm sorry, I didn't understand you. Please say \"Yes\" or \"No\"." yes?(prompt) end end |