-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
388 lines (332 loc) · 13.6 KB
/
app.rb
1
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
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
73
74
75
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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
267
268
269
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
306
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
require 'bundler/setup'
require 'open-uri'
Bundler.require(:default)
Dir[File.dirname(__FILE__) + '/lib/*.rb'].each { |file| require file }
get '/' do
@user = nil
@br1avg = ave_rent(Apartment.where("bed = '1'")).to_i
@br1med = median(Apartment.where("bed = '1'")).to_i
@br1high = Apartment.where('bed = 1').order(:price)[-1].price
@br1low = Apartment.where('bed = 1').order(:price)[0].price
@br1sqr = ave_sqr(Apartment.where('bed = 1')).to_i
@avg_percent = (@br1avg * 100.0 / @br1high).floor
@med_percent = (@br1med * 100.0 / @br1high).floor
@low_percent = (@br1low * 100.0 / @br1high).floor
@br2avg = ave_rent(Apartment.where("bed = '2'")).to_i
@br2med = median(Apartment.where("bed = 2")).to_i
@br2high = Apartment.where('bed = 2').order(:price)[-1].price
@br2low = Apartment.where('bed = 2').order(:price)[0].price
@br3avg = ave_rent(Apartment.where("bed = '3'")).to_i
@br3med = median(Apartment.where("bed = '3'")).to_i
@br3high = Apartment.where('bed = 3').order(:price)[-1].price
@br3low = Apartment.where('bed = 3').order(:price)[0].price
@br4avg = ave_rent(Apartment.where("bed = '4'")).to_i
@br4med = median(Apartment.where("bed = '4'")).to_i
@br4high = Apartment.where('bed = 4').order(:price)[-1].price
@br4low = Apartment.where('bed = 4').order(:price)[0].price
#north
if Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'North Portland'").exists?
@best_deal_N = best_deal(Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'North Portland'").order(:price))
else
@best_deal_N = Apartment.where("bed = 1 and section = 'North Portland'").order(:price)[0]
end
#northeast
if Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Northeast Portland'").exists?
@best_deal_NE = best_deal(Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Northeast Portland'").order(:price))
else
@best_deal_NE = Apartment.where("bed = 1 and section = 'Northeast Portland'").order(:price)[0]
end
#northwest
if Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Northwest Portland'").exists?
@best_deal_NW = best_deal(Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Northwest Portland'").order(:price))
else
@best_deal_NW = Apartment.where("bed = 1 and section = 'Northwest Portland'").order(:price)[0]
end
#southwest
if Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Southwest Portland'").exists?
@best_deal_SW = best_deal(Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Southwest Portland'").order(:price))
else
@best_deal_SW = Apartment.where("bed = 1 and section = 'Southwest Portland'").order(:price)[0]
end
#southeast
if Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Southeast Portland'").exists?
@best_deal_SE = best_deal(Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Southeast Portland'").order(:price))
else
@best_deal_SE = Apartment.where("bed = 1 and section = 'Southeast Portland'").order(:price)[0]
end
erb(:index)
end
get '/index/:id' do
id = (params[:id]).to_i/793
@user = User.find(id)
@br1avg = ave_rent(Apartment.where("bed = '1'")).to_i
@br1med = median(Apartment.where("bed = '1'")).to_i
@br1high = Apartment.where('bed = 1').order(:price)[-1].price
@br1low = Apartment.where('bed = 1').order(:price)[0].price
@br1sqr = ave_sqr(Apartment.where('bed = 1')).to_i
@avg_percent = (@br1avg * 100.0 / @br1high).floor
@med_percent = (@br1med * 100.0 / @br1high).floor
@low_percent = (@br1low * 100.0 / @br1high).floor
@br2avg = ave_rent(Apartment.where("bed = '2'")).to_i
@br2med = median(Apartment.where("bed = 2")).to_i
@br2high = Apartment.where('bed = 2').order(:price)[-1].price
@br2low = Apartment.where('bed = 2').order(:price)[0].price
@br3avg = ave_rent(Apartment.where("bed = '3'")).to_i
@br3med = median(Apartment.where("bed = '3'")).to_i
@br3high = Apartment.where('bed = 3').order(:price)[-1].price
@br3low = Apartment.where('bed = 3').order(:price)[0].price
@br4avg = ave_rent(Apartment.where("bed = '4'")).to_i
@br4med = median(Apartment.where("bed = '4'")).to_i
@br4high = Apartment.where('bed = 4').order(:price)[-1].price
@br4low = Apartment.where('bed = 4').order(:price)[0].price
#north
if Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'North Portland'").exists?
@best_deal_N = best_deal(Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'North Portland'").order(:price))
else
@best_deal_N = Apartment.where("bed = 1 and section = 'North Portland'").order(:price)[0]
end
#northeast
if Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Northeast Portland'").exists?
@best_deal_NE = best_deal(Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Northeast Portland'").order(:price))
else
@best_deal_NE = Apartment.where("bed = 1 and section = 'Northeast Portland'").order(:price)[0]
end
#northwest
if Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Northwest Portland'").exists?
@best_deal_NW = best_deal(Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Northwest Portland'").order(:price))
else
@best_deal_NW = Apartment.where("bed = 1 and section = 'Northwest Portland'").order(:price)[0]
end
#southwest
if Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Southwest Portland'").exists?
@best_deal_SW = best_deal(Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Southwest Portland'").order(:price))
else
@best_deal_SW = Apartment.where("bed = 1 and section = 'Southwest Portland'").order(:price)[0]
end
#southeast
if Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Southeast Portland'").exists?
@best_deal_SE = best_deal(Apartment.where("price < #{@br1avg} and bed = '1' and sqft > #{@br1sqr} and section = 'Southeast Portland'").order(:price))
else
@best_deal_SE = Apartment.where("bed = 1 and section = 'Southeast Portland'").order(:price)[0]
end
erb(:index)
end
get '/signout' do
erb(:signout)
end
post '/signout' do
email = params['email']
password = params['password']
user = User.find_by_email(email)
if BCrypt::Password.new(user.password) == password
redirect("/user/#{user.id * 793}")
else
erb(:login_invalid)
end
end
get '/login' do
erb(:login)
end
post '/login' do
email = params['email']
password = params['password']
user = User.find_by_email(email)
if BCrypt::Password.new(user.password) == password
redirect("/user/#{user.id * 793}")
else
erb(:login_invalid)
end
end
get '/user/:id' do
id = (params[:id]).to_i/793
@all = []
@search = []
@user = User.find_by_id(id)
erb(:user)
end
post '/user/:id' do
id = (params[:id]).to_i/793
@user = User.find_by_id(id)
if params['bed']
bed_search = ""
params['bed'].each do |bed|
bed_search += "bed = #{bed} or "
end
bed_search = bed_search[0..-5]
end
if params['bath']
bath_search = ""
params['bath'].each do |bath|
bath_search += "bath = #{bath} or "
end
bath_search = bath_search[0..-5]
end
if params['section']
section_search = ""
params['section'].each do |section|
section_search += "section = '#{section}' or "
end
section_search = section_search[0..-5]
end
combined_search = ""
if bed_search
combined_search += " " + "(" + bed_search + ") and"
end
if bath_search
combined_search += " " + "(" + bath_search + ") and"
end
if section_search
combined_search += " " + "(" + section_search + ") and"
end
if params['sqft'] != ''
sqft_search = " and sqft > #{params['sqft']}"
end
combined_search = combined_search[1..-5]
if combined_search && params['sqft'] != ''
combined_search += sqft_search
elsif params['sqft'] != ''
combined_search = "sqft > #{params['sqft']}"
end
@search = Apartment.where(combined_search).order(params['sort'])
@all = Apartment.all
erb(:user)
end
get '/confirm' do
erb(:confirm)
end
get '/login_invalid' do
erb(:login_invalid)
end
post '/login_invalid' do
email = params['email']
password = params['password']
user = User.find_by_email(email)
if BCrypt::Password.new(user.password) == password
redirect("/user/#{user.id * 793}")
else
erb(:login_invalid)
end
end
get '/signup' do
erb(:signup)
end
post '/signup' do
username = params['username']
email = params['email']
password = params['password']
confirm_pass = params['confirm_pass']
if password == confirm_pass
password = BCrypt::Password.create(password)
else
redirect("/confirm")
end
user = User.create({:username => username, :email => email, :password => password})
redirect("/user/#{user.id * 793}")
end
post '/confirm' do
username = params['username']
email = params['email']
password = params['password']
confirm_pass = params['confirm_pass']
if password == confirm_pass
password = BCrypt::Password.create(password)
else
redirect("/confirm")
end
user = User.create({:username => username, :email => email, :password => password})
redirect("/user/#{user.id * 793}")
end
get '/update' do
#Southwest Portland 97221
# def self.search_craigs(url, quadrant)
apartments = Apartment.all
apartments.each do |x|
if x.name == ''
x.delete
end
end
apartments_north_portland1 = Apartment.search_craigs('https://portland.craigslist.org/search/apa?postedToday=1&search_distance=2&postal=97217&min_price=499&max_price=6001&min_bedrooms=1&min_bathrooms=1&minSqft=1&availabilityMode=0', "North Portland")
apartments_north_portland2 = Apartment.search_craigs('https://portland.craigslist.org/search/apa?postedToday=1&search_distance=2&postal=97203&min_price=499&max_price=6001&min_bedrooms=1&min_bathrooms=1&minSqft=1&availabilityMode=0', "North Portland")
apartments_northeast_portland1 = Apartment.search_craigs('https://portland.craigslist.org/search/apa?postedToday=1&search_distance=1&postal=97212&min_price=499&max_price=6001&min_bedrooms=1&min_bathrooms=1&minSqft=1&availabilityMode=0', "Northeast Portland")
apartments_northeast_portland2 = Apartment.search_craigs('https://portland.craigslist.org/search/apa?postedToday=1&search_distance=1.9&postal=97211&min_price=499&max_price=6001&min_bedrooms=1&min_bathrooms=1&minSqft=1&availabilityMode=0', "Northeast Portland")
apartments_northeast_portland3 = Apartment.search_craigs('https://portland.craigslist.org/search/apa?postedToday=1&search_distance=0.5&postal=97213&min_price=499&max_price=6001&min_bedrooms=1&min_bathrooms=1&minSqft=1&availabilityMode=0', "Northeast Portland")
apartments_northwest_portland = Apartment.search_craigs('https://portland.craigslist.org/search/apa?postedToday=1&search_distance=2&postal=97229&min_price=499&max_price=6001&min_bedrooms=1&min_bathrooms=1&minSqft=1&availabilityMode=0', "Northwest Portland")
apartments_southeast_portland = Apartment.search_craigs('https://portland.craigslist.org/search/apa?postedToday=1&search_distance=3.5&postal=97206&min_price=499&max_price=6001&min_bedrooms=1&min_bathrooms=1&minSqft=1&availabilityMode=0', "Southeast Portland")
apartments_southwest_portland = Apartment.search_craigs('https://portland.craigslist.org/search/apa?postedToday=1&search_distance=3.5&postal=97221&min_price=499&max_price=6001&min_bedrooms=1&min_bathrooms=1&minSqft=1&availabilityMode=0', "Southwest Portland")
all_quadrants = apartments_north_portland1 + apartments_north_portland2 + apartments_northeast_portland1 + apartments_northeast_portland2 + apartments_northeast_portland3 + apartments_northwest_portland + apartments_southeast_portland + apartments_southwest_portland
all_quadrants.each do |x|
if Apartment.exists?({:name => x[:name],:address => x[:address],:price => x[:price]}) == false
Apartment.create({:name => x[:name], :url => x[:url], :price => x[:price], :bed => x[:bed], :bath => x[:bath], :sqft => x[:sqft], :address => x[:address], :cat => x[:cat], :dog => x[:dog], :washer => x[:washer], :smoke => x[:smoke], :garage => x[:garage], :description => x[:description], :section => x[:section], :posted => x[:posted]})
end
end
redirect '/'
end
post ('/save/:id') do
@user = User.find(params['user_id'])
apartment = Apartment.find(params[:id])
@user.apartments.push(apartment)
redirect "/user/#{@user.id * 793}"
end
post ('/delete/:id') do
@user = User.find(params[:id])
@user.apartments.delete_all
redirect "/user/#{@user.id * 793}"
end
def ave_rent(array)
avg = 0
array.each do |listing|
avg += listing.price
end
(avg / array.length)
end
# puts x = ave_rent(Apartment.where("rooms = '1'"))
def median(array)
med = array[array.size/2]
(med.price)
end
def ave_sqr(array)
avg = 0
array.each do |listing|
avg += listing.sqft
end
(avg / array.length)
end
def median_sqr(array)
med = array[array.size/2]
(med.sq_ft)
end
def count_ammenity(listing)
ammenity_count = 0
if listing.cat
ammenity_count +=1
end
if listing.dog
ammenity_count +=1
end
if listing.washer
ammenity_count +=1
end
if listing.smoke
ammenity_count +=1
end
if listing.garage
ammenity_count +=1
end
return ammenity_count
end
# pass in Apartment.where("price < #{@br1avg}, bed = '1', sqft > #{@br1sqr}").order(:price)
def best_deal(array)
ammenity_count = 0
array.each do |listing|
ammenity_count = count_ammenity(listing)
if ammenity_count >= 3
return listing
end
end
array.each do |listing|
ammenity_count = count_ammenity(listing)
if ammenity_count >= 0
return listing
end
end
end