Class: EverythingButTheKitchenSink::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/recipe_cli/cli.rb

Instance Method Summary collapse

Instance Method Details

#callObject

call will be used to run function (will call methods in order of how they should execute)



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/recipe_cli/cli.rb', line 8

def call
  shouldContinue = true
  listEmpty = false
  welcome_message
  enter_ingredients
  while !listEmpty && shouldContinue
    listEmpty = display_recipe_list
    if !listEmpty
      recipe = user_recipe_choice
      display_recipe_time_and_link(recipe)
      shouldContinue = other_options
    end
  end
end

#display_recipe_listObject

will print out numbered recipe list



38
39
40
41
42
43
44
45
46
47
# File 'lib/recipe_cli/cli.rb', line 38

def display_recipe_list
  if EverythingButTheKitchenSink::Recipe.all.count == 0
    return true
  else
    EverythingButTheKitchenSink::Recipe.all.each_with_index do |recipe, index|
      puts (index + 1).to_s + "." + recipe.title
    end
    return false
  end
end

will print recipe info (time and link)



72
73
74
# File 'lib/recipe_cli/cli.rb', line 72

def display_recipe_time_and_link(recipe)
  puts "\nThis meal can be ready in " + recipe.time.to_s + " minutes.\nFor detailed instructions go to: " + recipe.url
end

#enter_ingredientsObject

will prompt/gets ingredients and send to API class



29
30
31
32
33
34
35
# File 'lib/recipe_cli/cli.rb', line 29

def enter_ingredients
  puts "\nPlease input your ingredients below. Separate each ingredient with a comma.\n"

  ingredients = gets.strip
  recipes = EverythingButTheKitchenSink::SpoonacularApi.get_recipes(ingredients)
  EverythingButTheKitchenSink::TransformData.get_id_and_title(recipes)
end

#other_optionsObject

prompt user for other options; will take in yes/no and either loop through or exit function and call leave method



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/recipe_cli/cli.rb', line 77

def other_options
  puts "\nWould you like to see another recipe?\nPlease enter 'Yes' or 'No':"

  input = ""
  while input
    input = gets.strip.upcase
    case input
    when "YES"
      return true
    when "NO"
      puts "\nEnjoy your meal!"
      return false
    else
      puts "\nPlease enter 'Yes' or 'No'."
    end
  end
end

#user_recipe_choiceObject

will prompt/gets recipe choice and will send to api. will call Recipe class object to add info



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/recipe_cli/cli.rb', line 50

def user_recipe_choice
  puts "\nWhich recipe would you like to see?\nPlease input the recipe number:\n"

  input = gets.strip
  if valid_recipe_choice(input)
    recipe_index = (input.to_i - 1)

    attributes = EverythingButTheKitchenSink::SpoonacularApi.recipe_info(recipe_index)
    recipe_object = EverythingButTheKitchenSink::Recipe.all[recipe_index]
    EverythingButTheKitchenSink::TransformData.get_time_and_url(attributes, recipe_object)
    recipe_object
  else
    user_recipe_choice
  end
end

#valid_recipe_choice(input) ⇒ Object

checks if recipe choice number is valid choice



67
68
69
# File 'lib/recipe_cli/cli.rb', line 67

def valid_recipe_choice(input)
  input.to_i.between?(1,10)
end

#welcome_messageObject

will print welcome message



24
25
26
# File 'lib/recipe_cli/cli.rb', line 24

def welcome_message
  puts "Welcome to Everything but the Kitchen Sink!"
end