Class: Menu
Overview
menu, access data and extract the data then display the data in a readable format
Constant Summary
Constants included from RecipeHelper
RecipeHelper::HEADERS, RecipeHelper::PROMPT, RecipeHelper::RECIPES_PATH
Instance Method Summary collapse
-
#choices ⇒ Object
apps main logic router.
-
#choose_recipe(arr) ⇒ Object
user is prompted to chose a recipe based on index -1 formula to display recipe.
-
#delete_saved_recipe(arr) ⇒ Object
user is prompted to delete a recipe or not, this was the simplest implementation of this so it occurs before they make a selection to view a recipe.
-
#display_menu ⇒ Object
menu where user choses what to do, is refreshed through choices every time the user completes a loop.
-
#display_recipe(num, list) ⇒ Object
user choses a recipe based on the index value +1 stored in i which is displayed beside the recipes on the table.
-
#filtered_results ⇒ Object
takes the search term and stores the result in an array so that it can be displayed in a table format.
-
#initialize ⇒ Menu
constructor
A new instance of Menu.
-
#letter_check(str) ⇒ Object
checking user input##.
-
#save_random_recipe ⇒ Object
user is prompted to save the new recipe.
-
#save_recipe ⇒ Object
this takes the return of the view recipe choice and acts accordingly.
-
#search_new_recipe ⇒ Object
uses api to display random recipe.
-
#search_targeted_recipe ⇒ Object
filters saved recipes based on a search term, returns a message if there is no recipe with that term.
-
#terminal_table_lander(list) ⇒ Object
saved recipes route##.
-
#terminal_table_new_recipe(api_recipe) ⇒ Object
stores the recipe in app memory before prompting the user to view.
-
#view_recipe ⇒ Object
where the user can choose to view the new recipe or search again after viewwing the user can decide if they want to save it at that point to the recipes.JSON file.
-
#view_recipe_choice ⇒ Object
router to either view or search again, user may exitt at this point as well.
Constructor Details
#initialize ⇒ Menu
Returns a new instance of Menu.
10 11 12 13 14 |
# File 'lib/menu.rb', line 10 def initialize @recipebook = JSON.parse(File.read(RECIPES_PATH)) @new_recipe = Api.new end |
Instance Method Details
#choices ⇒ Object
apps main logic router
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/menu.rb', line 191 def choices loop do case when '1' terminal_table_lander(@new_recipe.read_recipes) when '2' search_new_recipe when '3' @new_recipe.get_user_recipe when '4' search_targeted_recipe when '5' exit end end end |
#choose_recipe(arr) ⇒ Object
user is prompted to chose a recipe based on index -1 formula to display recipe
68 69 70 71 72 73 |
# File 'lib/menu.rb', line 68 def choose_recipe(arr) puts 'please select a recipe by number to display' print 'number? ' number = gets.chomp.to_i display_recipe(number,arr) end |
#delete_saved_recipe(arr) ⇒ Object
user is prompted to delete a recipe or not, this was the simplest implementation of this so it occurs before they make a selection to view a recipe
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/menu.rb', line 55 def delete_saved_recipe(arr) puts 'please enter the recipe number' print ' >' num = gets.chomp.to_i if num - 1 > arr.length puts "no recipe found" choices end arr.delete_at(num-1) File.write(RECIPES_PATH, JSON.pretty_generate(arr)) choices end |
#display_menu ⇒ Object
menu where user choses what to do, is refreshed through choices every time the user completes a loop
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/menu.rb', line 16 def faraday_return = Faraday.get('https://api.spoonacular.com/food/jokes/random?apiKey=945916246cc3460dbfe56c71616e4d96') query_counter = faraday_return.headers["x-api-quota-used"] query_use = faraday_return.headers["x-api-quota-request"] joke =JSON.parse(faraday_return.body)['text'] puts " ".colorize( :background => :white) PROMPT.select("#{joke} your counter is at #{query_counter} out of 150 for the day, this search used #{query_use}, just remember each recipe takes about 1 point and refreshing this menu takes 1 point, you get 150 a day so you should be fine.") do || .choice({name:'View saved recipes', value:'1'}) .choice({name:'Find a random recipe! Live a little! Cremebrulee for dinner?', value:'2'}) .choice({name:'Write your own recipe', value:'3'}) .choice({name:"Can't decide but have food in the house? filter your saved recipes based on what you've got!", value:'4'}) .choice({name:'Exit', value:'5'}) end end |
#display_recipe(num, list) ⇒ Object
user choses a recipe based on the index value +1 stored in i which is displayed beside the recipes on the table
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/menu.rb', line 75 def display_recipe(num, list) terminal_key = list[num-1] rows = [] rows << [terminal_key['name']] table = Terminal::Table.new headings: header = ['name'], rows: rows puts table puts 'Ingredients'.colorize(:color => :white, :background => :red) puts terminal_key['ingredients'].map{|item|item} puts " ".colorize( :background => :red) puts "Description".colorize(:color => :white, :background => :red) puts terminal_key['description'] puts " ".colorize( :background => :red) puts 'Recipe'.colorize(:color => :white, :background => :red) puts terminal_key['recipe'] puts " ".colorize( :background => :red) puts terminal_key['url'] end |
#filtered_results ⇒ Object
takes the search term and stores the result in an array so that it can be displayed in a table format
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/menu.rb', line 111 def filtered_results search_para = gets.chomp result = [] unless letter_check(search_para) puts 'it seems like you entered a number or a symbol, dont do that..' search_targeted_recipe end @new_recipe.read_recipes.each do|recipe| if recipe['ingredients'].join(' ').downcase!.include? search_para result << recipe end end if result.empty? puts "Sorry you don't have any recipes with those items, please try again." choices end result end |
#letter_check(str) ⇒ Object
checking user input##
basically an if check that returns true if there is only letters used in filtered results
136 137 138 |
# File 'lib/menu.rb', line 136 def letter_check(str) str[/[a-z]+/] == str end |
#save_random_recipe ⇒ Object
user is prompted to save the new recipe
169 170 171 172 173 174 175 176 |
# File 'lib/menu.rb', line 169 def save_random_recipe if PROMPT.yes?('Do you want to save this recipe?') @new_recipe.write_recipes else choices end end |
#save_recipe ⇒ Object
this takes the return of the view recipe choice and acts accordingly
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/menu.rb', line 178 def save_recipe case view_recipe_choice when '1' @new_recipe.write_recipes when '2' search_new_recipe when '3' exit end end |
#search_new_recipe ⇒ Object
uses api to display random recipe
100 101 102 103 |
# File 'lib/menu.rb', line 100 def search_new_recipe @new_recipe.search_random_recipe terminal_table_new_recipe(@new_recipe.results) end |
#search_targeted_recipe ⇒ Object
filters saved recipes based on a search term, returns a message if there is no recipe with that term
106 107 108 109 |
# File 'lib/menu.rb', line 106 def search_targeted_recipe puts 'what do you feel like? This search can take things like say "fish pasta eggs or even bagels!" just dont go entering numbers or anything and you\'re golden' terminal_table_lander(filtered_results) end |
#terminal_table_lander(list) ⇒ Object
saved recipes route##
displays recipes in table format
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/menu.rb', line 37 def terminal_table_lander(list) header = ['number' , 'name', 'serves'] i = 1 rows = [] list.each do |recipe| rows << [i] + [ recipe["name"]] + [recipe["serves"] ] i += 1 end table = Terminal::Table.new headings: header, rows: rows, :style => {:width => 80} puts table if PROMPT.yes?('do you want to delete a recipe?') delete_saved_recipe(list) else choose_recipe(list) end end |
#terminal_table_new_recipe(api_recipe) ⇒ Object
stores the recipe in app memory before prompting the user to view
140 141 142 143 144 145 146 147 |
# File 'lib/menu.rb', line 140 def terminal_table_new_recipe(api_recipe) header = ['name', 'serves'] rows = [[api_recipe[:name]] + [api_recipe[:serves]]] table = Terminal::Table.new headings: header, rows: rows puts table view_recipe_choice end |
#view_recipe ⇒ Object
where the user can choose to view the new recipe or search again after viewwing the user can decide if they want to save it at that point to the recipes.JSON file
149 150 151 152 153 154 155 |
# File 'lib/menu.rb', line 149 def view_recipe PROMPT.select('want to see the recipe?') do || .choice({name:'yes', value:'1'}) .choice({name:'no, search again', value:'2'}) .choice({name:'no, quit', value:'3'}) end end |
#view_recipe_choice ⇒ Object
router to either view or search again, user may exitt at this point as well
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/menu.rb', line 157 def view_recipe_choice case view_recipe when '1' @new_recipe.show_last_recipe(@new_recipe.read_recipes) save_random_recipe when '2' search_new_recipe when '3' exit end end |