Class: FindDeals::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



5
6
7
8
9
10
11
12
# File 'lib/find_deals/cli.rb', line 5

def initialize 
    # @deals = FindDeals::SavedDeals.all
    @user_name = ""
    @input = ""
    @city_input = ""
    @category_input = ""
    @selected_deal
end

Instance Attribute Details

#category_inputObject

Returns the value of attribute category_input.



3
4
5
# File 'lib/find_deals/cli.rb', line 3

def category_input
  @category_input
end

#city_inputObject

Returns the value of attribute city_input.



3
4
5
# File 'lib/find_deals/cli.rb', line 3

def city_input
  @city_input
end

#inputObject

Returns the value of attribute input.



3
4
5
# File 'lib/find_deals/cli.rb', line 3

def input
  @input
end

#selected_dealObject

Returns the value of attribute selected_deal.



3
4
5
# File 'lib/find_deals/cli.rb', line 3

def selected_deal
  @selected_deal
end

#user_nameObject

Returns the value of attribute user_name.



3
4
5
# File 'lib/find_deals/cli.rb', line 3

def user_name
  @user_name
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
# File 'lib/find_deals/cli.rb', line 14

def call
    puts "--------------------------------------------------------------------"
    puts ""
    puts "Welcome to this amazing promo finder!"
    puts ""
    start
end

#delete_recordObject



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/find_deals/cli.rb', line 307

def delete_record 
    user = get_user
    show_saved_deals
    puts "--------------------------------------------------------------------"
    puts ""
    puts "Type in the number of the deal you would like to delete"
    puts ""
    puts "--------------------------------------------------------------------" 
    @input = gets.strip.downcase

    while @input.to_i == 0 || @input.to_i >= SavedDeals.select {|deal| deal.user_id == user.id}.length
        invalid_input
        puts "--------------------------------------------------------------------"
        puts ""
        puts "Type in the number of the deal you would like to delete"
        puts ""
        puts "--------------------------------------------------------------------" 
        @input = gets.strip.downcase
        
        if @input == 'quit'
            goodbye
        end
    end 

    SavedDeals.all.delete_from_db(user, @input.to_i)
    prompt_to_show_saved_deals
    next_steps

end

#filter_by_categoryObject



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/find_deals/cli.rb', line 249

def filter_by_category
    puts ""
    puts "--------------------------------------------------------------------"
    puts ""
    puts "YOUR SAVED DEALS IN THE #{@category_input == nil ? 'ALL DEALS' : @category_input.upcase.split('-').join(' ')} CATEGORY"
    puts ""
    puts "--------------------------------------------------------------------"
    #get id of selected_user
    user = Users.find_by(name: @user_name)
    deals_from_category = SavedDeals.select {|deal| deal.user_id == user.id && deal.category_id == @input.to_i}
    if deals_from_category.length == 0 
        puts "Sorry! No saved deals for this section. Please try another selection."
        next_steps
    end
    deals_from_category.each.with_index(1) do |deal, index|
        puts "#{index}." 
        puts deal.print
    end

end

#filter_by_cityObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/find_deals/cli.rb', line 229

def filter_by_city
    puts ""
    puts "--------------------------------------------------------------------"
    puts ""
    puts "YOUR SAVED DEALS IN #{city_input.upcase.split('-').join(' ')}"
    puts ""
    puts "--------------------------------------------------------------------"
    #get id of selected_user
    user = Users.find_by(name: @user_name)
    deals_from_city = SavedDeals.select {|deal| deal.user_id == user.id && deal.city_id == @input.to_i}
    if deals_from_city.length == 0 
        puts "Sorry! No saved deals for this section. Please try another selection."
        next_steps
    end
    deals_from_city.each.with_index(1) do |deal, index|
        puts "#{index}." 
        puts deal.print
    end
end

#get_category_inputObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/find_deals/cli.rb', line 95

def get_category_input
    @input = gets.strip
    @category_input = select_category_from_input
    if @input == 'quit'
        goodbye
    end  
    
    if @input.to_i == 0 || @input.to_i > Categories.all.length
        invalid_input
        get_category_input
    end
    @category_input = Categories.find(input).name
    site_scraper = FindDeals::Scraper.new(@city_input, @category_input)
    if FindDeals::Deal.all.length == 0
        puts "--------------------------------------------------------------------"
        puts ""
        puts "Sorry! No deals for this selection today. Please try another selection."
        puts ""
        puts "--------------------------------------------------------------------"
    end
end

#get_city_inputObject



55
56
57
58
# File 'lib/find_deals/cli.rb', line 55

def get_city_input
    @input = gets.strip
    @input != 'quit' ? @city_input = select_city_from_input : goodbye
end

#get_userObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/find_deals/cli.rb', line 177

def get_user
    puts "--------------------------------------------------------------------"
    puts ""
    puts "We need to be able to associate this deal with you."
    puts "Please enter a username"
    @input = gets.strip
    if @input == 'quit'
        goodbye
    else 
        puts "--------------------------------------------------------------------"
        @user_name = input
        Users.find_or_create_by(name: @user_name)
    end
end

#goodbyeObject



345
346
347
348
349
350
351
352
# File 'lib/find_deals/cli.rb', line 345

def goodbye
    puts "--------------------------------------------------------------------"
    puts ""
    puts "Hope to see you again soon for more deals!"
    puts ""
    puts "--------------------------------------------------------------------"
    exit
end

#invalid_inputObject



337
338
339
340
341
342
343
# File 'lib/find_deals/cli.rb', line 337

def invalid_input
    puts "--------------------------------------------------------------------"
    puts ""
    puts "Invalid input. Please try again"
    puts ""
    puts "--------------------------------------------------------------------"
end

#next_stepsObject



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/find_deals/cli.rb', line 270

def next_steps
    puts "--------------------------------------------------------------------"
    puts ""
    puts "What would you like to do now"
    puts "- To view more deals, type 'MORE'"
    puts "- To filter your saved deals by city, type in 'CITY'"
    puts "- To filter your saved deals by category, type in 'CATEGORY'"
    puts "- To delete a saved deal type 'DELETE'"
    puts "- To quit, type in quit"
    @input = gets.strip.downcase

    if @input == "more"
        puts ""
        puts "--------------------------------------------------------------------"
        # start program again
        start #
    elsif @input == 'city'
        puts ""
        puts "--------------------------------------------------------------------"
        prompt_user_city
        filter_by_city
        next_steps
    elsif @input == 'category'
        prompt_user_category
        filter_by_category
        next_steps
    elsif @input == "delete"
        delete_record
        next_steps
    elsif @input == "quit"
        goodbye
    else  
        invalid_input
        next_steps
    end
end


117
118
119
120
121
122
123
124
125
126
# File 'lib/find_deals/cli.rb', line 117

def print_deals
    puts "--------------------------------------------------------------------"
    puts ""
    puts "DEALS FOR THIS INPUT"
    puts ""
    FindDeals::Deal.all.each.with_index(1) do |deal, index|
        puts "#{index}." 
        puts deal.print
    end
end


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/find_deals/cli.rb', line 128

def print_more_info
    if FindDeals::Deal.all.length != 0
        puts "--------------------------------------------------------------------"
        puts ""
        puts "Please enter the number of the deal you'd like to see more about. Type in quit to exit"
        puts ""
        @input = gets.strip.to_i
        while @input == 0 || @input > FindDeals::Deal.all.size
            invalid_input
            @input = gets.strip.to_i
        end
        FindDeals::Deal.all[@input - 1].print_about_details
        @selected_deal = FindDeals::Deal.all[@input - 1]
    else
        puts "--------------------------------------------------------------------"
        puts ""
        puts "Sorry! No deals for this selection today. Please try another selection."
        puts ""
        puts "--------------------------------------------------------------------"
    end
    
    if @input == 'quit'
        goodbye
    end
end

#prompt_to_save_dealObject



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

def prompt_to_save_deal
    puts ""
    puts "Would you like to save this deal? Y or N"
    @input = gets.strip.downcase
    puts ""
    puts "--------------------------------------------------------------------"
    if @input == "y"
        user = get_user
        puts "Saving deal..."
        puts ""
        puts "--------------------------------------------------------------------"
        
        @selected_deal.save(user.id) ##save to Saved Deals, with the user id as the User_id
    elsif @input == "n"
        next_steps
    elsif @input == 'quit'
        goodbye   
    else
        invalid_input
        prompt_to_save_deal
    end
end

#prompt_to_show_saved_dealsObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/find_deals/cli.rb', line 192

def prompt_to_show_saved_deals
    puts ""
    puts "Would you like to see your saved deals? Y or N"
    
    @input = gets.strip.downcase
    puts ""
    puts "--------------------------------------------------------------------"

    if @input == "y"
        show_saved_deals
    elsif @input == "n"
        next_steps
    elsif @input == 'quit'
        goodbye
    else 
        invalid_input
        prompt_to_show_saved_deals
    end

end

#prompt_user_categoryObject



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

def prompt_user_category
    puts "----------------------------------------------------------------"
    puts ""
    puts "Select a category (type in corresponding number)"
    puts "Type quit to exit the program"
    puts "    1. Anything\n    2. Dining\n    3. Wellness & Beauty\n    4. Activities\n    5. Shopping\n    6. Services\n    7. Wine\n    8. Personalised Gifts\n    DOC\n    puts \"\"\n    get_category_input\nend\n"

#prompt_user_cityObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/find_deals/cli.rb', line 37

def prompt_user_city
    puts "--------------------------------------------------------------------"
    puts ""
    puts "Select a city (type in corresponding number)"
    puts ""
    puts "        1. Sydney\n        2. Melbourne\n        3. Perth\n        4. Brisbane\n        5. Adelaide\n        6. Gold Coast\n        DOC\n    \n    puts \"\"\n    get_city_input\nend\n"

#select_category_from_inputObject



68
69
70
71
72
73
74
# File 'lib/find_deals/cli.rb', line 68

def select_category_from_input
    while @input.to_i == 0 || @input.to_i > Categories.all.size
        invalid_input
        prompt_user_category
    end
        Categories.find(@input).name ## Collects it from the database 
end

#select_city_from_inputObject



60
61
62
63
64
65
66
# File 'lib/find_deals/cli.rb', line 60

def select_city_from_input
    while @input.to_i == 0 || @input.to_i > Cities.all.size
        invalid_input
        prompt_user_city
    end
        Cities.find(@input).name # Collects it from the database 
end

#show_saved_dealsObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/find_deals/cli.rb', line 213

def show_saved_deals
    puts ""
    puts "--------------------------------------------------------------------"
    puts ""
    puts "YOUR SAVED DEALS"
    puts ""
    puts "--------------------------------------------------------------------"
    #get id of selected_user
    user = Users.find_by(name: @user_name)
    deals_from_user = SavedDeals.select {|deal| deal.user_id == user.id}
    deals_from_user.each.with_index(1) do |deal, index|
        puts "#{index}." 
        puts deal.print
    end
end

#startObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/find_deals/cli.rb', line 22

def start
        #-----LIST OF METHODS---#
        prompt_user_city
        prompt_user_category
        if FindDeals::Deal.all.length != 0 
            print_deals
            print_more_info
            prompt_to_save_deal
            prompt_to_show_saved_deals
            next_steps
        else 
            start
        end
end