Class: CompareSupermarkets::CLI

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

Instance Method Summary collapse

Instance Method Details

#anything_elseObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/compare_supermarkets/cli.rb', line 217

def anything_else
    puts "Can we help you compare anything else today? (Y/N)"
    answer = gets.strip.downcase
    if answer == "n"
        puts ""
        puts "Okay, thank you! Have a great day!"
        puts ""
        exit
    elsif answer == "y"
        start
    else
        invalid_input
        anything_else
    end
end

#callObject



2
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
# File 'lib/compare_supermarkets/cli.rb', line 2

def call
    puts ""
    puts "Welcome to Compare_Supermarkets"
    puts ""
    puts "Type q at any time to exit."
    puts ""
    puts "Would you like to compare prices between Coles and Woolworths (Y/N)"
    input = gets.strip.downcase
    puts ""
    if input == "y"
        start
    elsif input == "n"
        puts ""
        puts "No worries, have a good one!"
        puts ""
        exit
    elsif input.empty?
        invalid_input
        call
    elsif input == "q"
        exit
    else
        invalid_input
        call
    end
end

#change_search_termObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/compare_supermarkets/cli.rb', line 76

def change_search_term
    puts "Would you like to change your search term? (Y/N)"
    input = gets.strip.downcase
    if input == "y"
        start
    elsif input == "n"
        puts ""
        puts "No worries"
        puts ""
        if CompareSupermarkets::Product.count > 0
            how_to_display
        else
            puts ""
            puts "Have a great day!"
            puts ""
            exit
        end
    elsif input.empty?
        invalid_input
        change_search_term
    elsif input == "q"
        exit
    else
        invalid_input
        change_search_term
    end
end

#choice(input) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/compare_supermarkets/cli.rb', line 122

def choice(input)
    if input == "1"
        choice = CompareSupermarkets::Product.all_top_10_sorted_by_price
        print_items(choice, "asc")
    elsif input == "2"
        choice = CompareSupermarkets::Product.all_top_10_sorted_by_price
        print_items(choice, "desc")
    elsif input == "3"
        how_to_sort ? direction = "asc" : direction = "desc"
        choice = CompareSupermarkets::Product.all_items_sorted_by_price
        print_items(choice, direction)
    elsif input == "4"
        how_to_sort ? direction = "asc" : direction = "desc"
        choice = CompareSupermarkets::Product.coles_sorted_by_price
        print_items(choice, direction)
    elsif input == "5"
        how_to_sort ? direction = "asc" : direction = "desc"
        choice = CompareSupermarkets::Product.woolworths_sorted_by_price
        print_items(choice, direction)
    elsif input == ""
        invalid_input
        how_to_display
    elsif input == "q"
        exit
    else
        invalid_input
        how_to_display
    end
end

#finishedObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/compare_supermarkets/cli.rb', line 152

def finished
    puts ""
    puts "Is this what you were looking for? (Y/N)"
    input = gets.strip.downcase
    if input == "y"
        puts ""
        puts "Great!"
        anything_else
    elsif input == "n"
        puts ""
        try_again
    elsif input == ""
        invalid_input
        finished
    elsif input == "q"
        exit
    else
        invalid_input
        finished
    end
end

#how_to_displayObject



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

def how_to_display
    puts "How would you like your items displayed? (1-5)"
    puts ""
    puts "1 - First 10 Cheapest items"
    puts ""
    puts "2 - First 10 Most expensive items"
    puts ""
    puts "3 - All items"
    puts ""
    puts "4 - Only Coles items"
    puts ""
    puts "5 - Only Woolworths items"
    puts ""
    input = gets.strip
    puts ""
    choice(input)
end

#how_to_sortObject



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/compare_supermarkets/cli.rb', line 240

def how_to_sort
    puts ""
    puts "How would you like your results sorted?"
    puts ""
    puts "1 - Cheapest to most expensive"
    puts "2 - Most expensive to cheapest"
    puts ""
    input = gets.strip
    if input == "1"
        puts ""
        puts "Okay! Here we go!"
        puts ""
        return true
    elsif input == "2"
        puts ""
        puts "Okay! Here we go!"
        puts ""
        return false
    elsif input == ""
        puts ""
        puts "Please enter a command"
        how_to_sort
    else
        invalid_input
        how_to_sort
    end
end

#invalid_inputObject



233
234
235
236
237
238
# File 'lib/compare_supermarkets/cli.rb', line 233

def invalid_input
    puts ""
    puts "Oh, I don't understand that input"
    puts ""
    puts "Please try again"
end


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/compare_supermarkets/cli.rb', line 268

def print_items(choice, direction)
    direction == "asc" ? choice = choice : choice = choice.reverse
    choice.each do |item|
        p "Supermarket: #{item.supermarket.name}"
        p "Item name: #{item.name}"
        p "Item price: $#{item.dollar_value}.#{item.cent_value}"
        p "Item unit size: #{item.unit_size}"
        p item.price.empty? ? "Unit price: $#{item.dollar_value}.#{item.cent_value} / #{item.unit_size}" : "Unit price: $#{item.price}"
        p "#{item.url}"
        puts ""
    end
    puts ""
    puts ""
    finished
end

#search_supermarkets(input) ⇒ Object



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

def search_supermarkets(input)
    supermarkets = ["Coles", "Woolworths"]
    CompareSupermarkets::Scraper.search_supermarkets(supermarkets, input)
end

#sort_or_compareObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/compare_supermarkets/cli.rb', line 194

def sort_or_compare
    puts ""
    puts "Please pick from one of these options:"
    puts ""
    puts "1 - Change the which results you would like to see"
    puts ""
    puts "2 - Search for a new item"
    input = gets.strip
    if input == "1"
        how_to_display
    elsif input == "2"
        CompareSupermarkets::Product.clear_all
        CompareSupermarkets::Supermarket.clear_all
        start
    elsif input == ""
        invalid_input
        sort_or_compare
    else
        invalid_input
        sort_or_compare
    end
end

#startObject



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
# File 'lib/compare_supermarkets/cli.rb', line 29

def start
    puts ""
    puts "Great!"
    puts "Please enter the name of the item you wish to compare (example: red tractor oats or purple sweet potato)"
    input = gets.strip.downcase
    puts ""
    puts ""
    puts "Thank you, we will now search for this product"
    puts "This will open the browser, don't panic!"
    puts ""
    puts ""
    puts ""
    puts ""
    self.search_supermarkets(input)
    puts ""
    puts ""
    if CompareSupermarkets::Product.count > 20
        puts ""
        puts "Ooo, jeez, seems like we have a lot of results."
        puts "We have #{CompareSupermarkets::Product.count} results for you."
        puts "If you were a little more specific with your search"
        puts "term, we would have less to sort through..."
        puts ""
        puts "If you still want to see all of these results, select N"
        puts ""
        change_search_term
    elsif CompareSupermarkets::Product.count == 0
        puts ""
        puts "Ohhh man! Doesn't seem like any of these supermarkets"
        puts "carry that product."
        puts ""
        change_search_term
    else
        puts ""
        puts "Great! We have #{CompareSupermarkets::Product.count} items in stock for you."
        puts ""
        puts ""
        puts ""
        how_to_display
    end
end

#try_againObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/compare_supermarkets/cli.rb', line 174

def try_again
    puts "Oh damn, would you like to try something else? (Y/N)"
    input = gets.strip.downcase
    if input == "n"
        puts ""
        puts "I'm sorry we couldn't help you today. Have a wonderful day!"
        puts ""
        exit
    elsif input == "y"
        sort_or_compare
    elsif input == "q"
        exit
    elsif input == ""
        invalid_input
        try_again
    else
        invalid_input
    end
end