-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.rb
60 lines (47 loc) · 1.29 KB
/
web.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
require 'sinatra'
require 'json'
require 'haml'
require 'httparty'
require 'pp'
require './lib/restaurantrequest.rb'
require './lib/environment.rb'
configure do
enable :sessions
set :server, :puma
set(:session_secret, '91d9be49394ffe1bbe94c89d9cd3945e')
end
get '/' do
haml :index
end
get '/randomrestaurant' do
# Set distance to 800 meters if distance is actually not a number. Assuming people walk 80 meters a minute
if request["distance"] == "NaN"
distance = 800
else
distance = request["distance"].to_i * 80
end
# Get restaurant request object from google
google = RestaurantRequest.new(ENV['GOOGLE_API_SERVER_KEY'])
restaurant = google.random_restaurant(
latitude: request["latitude"],
longitude: request["longitude"],
distance: distance
)
# Redirect to index if there were no results
unless restaurant
session[:flash] = "There are no results in your area"
redirect to('/')
end
haml :display_results, :locals => { :restaurant => restaurant }
end
get '/:placeid/details.json' do
content_type :json
google = RestaurantRequest.new ENV["GOOGLE_API_SERVER_KEY"]
details= google.return_details(params[:placeid])
details.to_json
end
helpers do
def formatted_address(restaurant)
restaurant["formatted_address"].gsub(" ", "+")
end
end