Class: JustTheRecipe::CLI
- Inherits:
-
Object
- Object
- JustTheRecipe::CLI
- Defined in:
- lib/just-the-recipe/cli.rb
Instance Method Summary collapse
- #add_recipe(recipe) ⇒ Object
- #call ⇒ Object
- #cookbook_menu ⇒ Object
- #create_cookbook ⇒ Object
- #goodbye ⇒ Object
- #main_menu ⇒ Object
- #new_recipe ⇒ Object
- #scrape_url ⇒ Object
- #search ⇒ Object
Instance Method Details
#add_recipe(recipe) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/just-the-recipe/cli.rb', line 111 def add_recipe(recipe) prompt = TTY::Prompt.new puts "Would you like to add this recipe to a cookbook? It won't be saved otherwise. (y/n)" input = gets.chomp if input == "y" cookbooks_create = JustTheRecipe::Cookbook.list_cookbooks << "Create a new cookbook." cookbook = prompt.select("Ok! Choose a cookbook to add your recipe to, or create a new one.", cookbooks_create) if cookbook == "Create a new cookbook." recipe.add_to_cookbook(create_cookbook) puts "We added this recipe to your new cookbook." else recipe.add_to_cookbook(cookbook) puts "We added this recipe to the cookbook called #{cookbook}" end elsif input == "n" puts "No problem. This recipe wasn't added to a cookbook." else puts "Sorry, you'll have to answer with either 'y' or 'n'." end end |
#call ⇒ Object
3 4 5 6 7 |
# File 'lib/just-the-recipe/cli.rb', line 3 def call JustTheRecipe::Cookbook.create_from_files puts "Welcome to Just the Recipe! What would you like to do?" end |
#cookbook_menu ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/just-the-recipe/cli.rb', line 59 def prompt = TTY::Prompt.new cookbooks_create = JustTheRecipe::Cookbook.list_cookbooks << "Create a new cookbook." << "Delete a cookbook." << "Main Menu" input = prompt.select("Choose a cookbook to view or create a new one.", cookbooks_create) JustTheRecipe::Cookbook.list_cookbooks if JustTheRecipe::Cookbook.find_by_name(input) puts "Here's what's in the cookbook called #{input}:" puts JustTheRecipe::Cookbook.find_by_name(input).return_cookbook elsif input == "Create a new cookbook." create_cookbook elsif input == "Delete a cookbook." cookbooks_delete = JustTheRecipe::Cookbook.list_cookbooks << "Exit" name = prompt.select("Which cookbook would you like to delete? WARNING: THIS CANNOT BE UNDONE!", cookbooks_delete) JustTheRecipe::Cookbook.find_by_name(name).delete_cookbook puts "Ok, we deleted that cookbook." elsif input == "Main Menu" end end |
#create_cookbook ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/just-the-recipe/cli.rb', line 103 def create_cookbook puts "Ok! What would you like to name your new cookbook?" cookbook = gets.chomp JustTheRecipe::Cookbook.new(cookbook) puts "Great! We created a new cookbook for you." cookbook end |
#goodbye ⇒ Object
137 138 139 |
# File 'lib/just-the-recipe/cli.rb', line 137 def goodbye puts "\n\nThanks for stopping by! If you created any cookbooks, they'll be saved as text files so that you can continue using them in the future. See you soon!" end |
#main_menu ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/just-the-recipe/cli.rb', line 9 def prompt = TTY::Prompt.new puts "\n" = prompt.select("Choose an option") do || .choice name: "Search for a new recipe.", value: 1 .choice name: "Get a recipe from a URL.", value: 2 .choice name: "View your cookbooks.", value: 3 .choice name: "Create a new recipe manually.", value: 4 .choice name: "Exit", value: "exit" end case when 1 search when 2 scrape_url when 3 when 4 new_recipe when "exit" goodbye end end |
#new_recipe ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/just-the-recipe/cli.rb', line 84 def new_recipe puts "Ok, what would you like to call your new recipe?" name = gets.chomp puts "Add a short description of your recipe." description = gets.chomp puts "Add the recipe's ingredients, seperated by a comma. (Ex: 1/2 cup of flour, 2 tbsp sugar, 1 cup water)" ingredients = gets.chomp.split(", ") puts "Add the recipe's instructions, seperated by a comma. (Ex: Mix the ingredients, cook the recipe, enjoy!)" steps = gets.chomp.split(", ") puts "\nGreat! Here's your new recipe:" manual_recipe = JustTheRecipe::Recipe.new(name, description, ingredients, steps) manual_recipe.display_recipe add_recipe(manual_recipe) end |
#scrape_url ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/just-the-recipe/cli.rb', line 46 def scrape_url puts "Enter your url:" url = gets.chomp if JustTheRecipe::Scraper.new(url).valid_url? recipe = JustTheRecipe::Scraper.new(url).get_recipe_by_schema recipe.display_recipe add_recipe(recipe) else puts "\nSorry, it doesn't look like we can get the recipe from that URL. Try something else." end end |
#search ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/just-the-recipe/cli.rb', line 34 def search puts "Enter your search term:" search_term = gets.chomp recipe = JustTheRecipe::Searcher.new(search_term).api_get if recipe.class == JustTheRecipe::Recipe recipe.display_recipe add_recipe(recipe) else end end |