Class: FindRecipe::CLI
- Inherits:
-
Object
- Object
- FindRecipe::CLI
- Defined in:
- lib/find_recipe/cli.rb
Overview
This Class acts as the CLI Controller
Instance Method Summary collapse
- #call ⇒ Object
- #choose_searched_recipes ⇒ Object
- #exit_program ⇒ Object
- #search_options ⇒ Object
- #trending_recipes ⇒ Object
Instance Method Details
#call ⇒ Object
3 4 5 |
# File 'lib/find_recipe/cli.rb', line 3 def call end |
#choose_searched_recipes ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/find_recipe/cli.rb', line 90 def choose_searched_recipes puts "\n\n" puts "Search Results:".yellow puts "" FindRecipe::Recipe::SearchedRecipe.all.each.with_index(1) do |recipe, index| puts "#{index}.".green + " #{recipe.name}" end puts "" puts "Choose a recipe number or type 'restart'" input = gets.strip.downcase if input.to_i > 0 && input.to_i <= FindRecipe::Recipe::SearchedRecipe.all.size chosen_recipe = FindRecipe::Recipe::SearchedRecipe.all[input.to_i - 1] chosen_recipe.get_details elsif input == "restart" FindRecipe::Recipe::SearchedRecipe.reset else puts "Not sure what you mean..." choose_searched_recipes end puts "Open in browser? (y/n)" input = gets.strip.downcase if input == "y" chosen_recipe.open_in_browser end puts "Do you want to see the list again, restart, or exit?" puts "Enter list, restart, or exit" input = gets.strip.downcase if input == "list" choose_searched_recipes elsif input == "restart" FindRecipe::Recipe::SearchedRecipe.reset elsif input == "exit" exit_program end end |
#exit_program ⇒ Object
136 137 138 139 |
# File 'lib/find_recipe/cli.rb', line 136 def exit_program puts "See you next time!" exit end |
#search_options ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/find_recipe/cli.rb', line 7 def puts "" puts "Welcome!".red puts "How do you want to get started?" puts "" puts "1.".blue + " See trending recipes" puts "2.".blue + " Search for a recipe" puts "" puts "Enter 1 or 2, or exit" input = gets.strip.downcase if input == "1" # Scrapes recipes only once to save loading time if FindRecipe::Recipe::TrendingRecipe.all.size == 0 puts "Please wait a moment for the recipes to be loaded..." FindRecipe::Recipe::TrendingRecipe.create_recipes end trending_recipes elsif input == "2" # Scrape recipes only if a search hasn't been done yet or if user restarts if FindRecipe::Recipe::SearchedRecipe.all.size == 0 puts "What is the dish or ingredient you want to search for?" input = gets.strip.downcase puts "Please wait a moment for the recipes to be loaded..." # If search keyword has spaces, it's necessary to replace them with %20 so the URL works FindRecipe::Recipe::SearchedRecipe.create_recipes(input.gsub(" ", "%20")) end choose_searched_recipes elsif input == "exit" exit_program else puts "Not sure what you mean..." end end |
#trending_recipes ⇒ Object
47 48 49 50 51 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 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/find_recipe/cli.rb', line 47 def trending_recipes puts "\n\n" FindRecipe::Recipe::TrendingRecipe.all.each.with_index(1) do |recipe, index| puts "#{index}.".green + " #{recipe.name}" end puts "" puts "Choose a recipe number or type 'back'" input = gets.strip.downcase if input.to_i > 0 && input.to_i <= FindRecipe::Recipe::TrendingRecipe.all.size chosen_recipe = FindRecipe::Recipe::TrendingRecipe.all[input.to_i - 1] chosen_recipe.get_details elsif input == "back" else puts "Not sure what you mean..." puts "" trending_recipes end puts "Open in browser? (y/n)" input = gets.strip.downcase if input == "y" chosen_recipe.open_in_browser end puts "Do you want to see the list again, restart, or exit?" puts "Enter list, restart, or exit" input = gets.strip.downcase if input == "list" trending_recipes elsif input == "restart" elsif input == "exit" exit_program end end |