Class: BudgetBytesCli::CLI

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

Instance Method Summary collapse

Instance Method Details

#callObject



3
4
5
6
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
46
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
# File 'lib/budget_bytes_cli/cli.rb', line 3

def call
    puts "Welcome to Budget Bytes CLI!"
    BudgetBytesCli::Scraper.create_categories
    category_selector = BudgetBytesCli::ArrayPrompter.new("Selecting recipe category.")
    category_selector.array_to_select = BudgetBytesCli::Category.all.map {|i| i.name}
    
    recipe_selector = BudgetBytesCli::ArrayPrompter.new("Selecting recipe")
    
    cat_combination_selector = BudgetBytesCli::ArrayPrompter.new("Selecting category to combine.")
    
    current_selector = category_selector
    selection = category_selector.get_input
    
    #define variables outside of while loop scope
    selected_category = nil
    filtered_categories = []
    recipe_array = []
    
    while selection != 'Q'
        if current_selector == category_selector
            selected_category = BudgetBytesCli::Category.all[selection.to_i - 1]
            whether_to_combine = yes_no_input("Combine with another category?\nIn other words, display only recipes in both the current category and another you select?\n")
            if whether_to_combine == 'Y'
                current_selector = cat_combination_selector
                filtered_categories = BudgetBytesCli::Category.all.select do |c|
                    c != selected_category
                end
                cat_combination_selector.array_to_select = filtered_categories.map {|i| i.name}
            else
                current_selector = recipe_selector
                recipe_array = selected_category.recipes
                recipe_selector.array_to_select = selected_category.recipes.map {|i| i.name}
            end
        elsif current_selector == cat_combination_selector
            combination_category = filtered_categories[selection.to_i - 1]
            recipe_array = selected_category.combine_recipes(combination_category)
            if recipe_array.empty?
                valid_input_empty_array = false
                while !valid_input_empty_array
                    puts "No recipes are in the two categories to combine."
                    puts "Enter 'B' to select a different category to combine,"
                    puts "'C' to start fresh with a different recipe category,"
                    puts "or 'I' to ignore the recipe combination and use the category you chose."
                    empty_array_input = gets.strip.upcase
                    if empty_array_input == 'B'
                        current_selector = cat_combination_selector
                        #not necessary, but makes explicit that we're running this again
                        valid_input_empty_array = true
                    elsif empty_array_input == 'C'
                        valid_input_empty_array = true
                        current_selector = category_selector
                    elsif empty_array_input == 'I'
                        valid_input_empty_array = true
                        current_selector = recipe_selector
                        recipe_array = selected_category.recipes
                        recipe_selector.array_to_select = selected_category.recipes.map {|i| i.name}
                    end
                end
            else
                current_selector = recipe_selector
                recipe_selector.array_to_select = recipe_array.map {|i| i.name}
            end
        else
            selected_recipe = recipe_array[selection.to_i - 1]
            self.display_recipe(selected_recipe)
            current_selector = category_selector
        end
        selection = current_selector.get_input
    end
end

#display_recipe(recipe_chosen) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/budget_bytes_cli/cli.rb', line 74

def display_recipe(recipe_chosen)
    if recipe_chosen.ingredients == "" || recipe_chosen.instructions == ""
        puts "Error!  Could not scrape recipe from page selected!"
    else
        puts recipe_chosen.name
        puts "\nIngredients\n"
        puts recipe_chosen.ingredients
        puts ""
        page_width = IO.console.winsize[1]
        puts reformat_wrapped(recipe_chosen.instructions, page_width || 80)
        puts ""
        if yes_no_input("Do you want to open this recipe in your browser?") == 'Y'
            Launchy.open(recipe_chosen.url)
        end
    end
end

#reformat_wrapped(s, width = 78) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/budget_bytes_cli/cli.rb', line 103

def reformat_wrapped(s, width= 78)
  lines = []
  line = ""
  s.split(/\s+/).each do |word|
    if line.size + word.size >= width
      lines << line
      line = word
    elsif line.empty?
     line = word
    else
     line << " " << word
   end
   end
   lines << line if line
  return lines.join "\n"
end

#yes_no_input(prompt) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/budget_bytes_cli/cli.rb', line 91

def yes_no_input (prompt)
    puts prompt + " Please answer 'y' or 'n'"
    input = gets.strip.upcase
    while !['Y', 'N'].include?(input)
        puts "Invalid input."
        puts prompt + " Please answer 'y' or 'n'" 
        input = gets.strip.upcase
    end
    input
end