-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplaces.rb
executable file
·209 lines (146 loc) · 5.71 KB
/
places.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
require './weathers'
require 'json'
class Places
attr_reader :lat, :lon, :address, :phone_number, :name, :rating, :placeid, :price_level, :open_now, :periods, :radius, :weekday_text
def initialize(lat, lon, radius=1609)
@lat = lat
@lon = lon
@radius = radius
@rating = "No rating available"
@price_level = "Not available"
@has_error = false
parse_nearby_restaurants
end
def call_google_places_api(url)
json_response = HTTParty.get(url).body
return JSON.parse( json_response )
end
def parse_nearby_restaurants
restaurants = Hash.new()
#do a places search to get the name of restaurants and their place_id within a given radius
#search_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+ @lat + "," + @lon + "&radius=1000&types=food&key=AIzaSyB3xKb4v0cK805_F1ApSX0Os0KS-XzDoO4"
search_url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurants&location=" + @lat.to_s + "," + @lon.to_s + "&radius=" + @radius.to_s + "&types=restaurant&key=" + ENV["GOOGLE_PLACES"]
#search_url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurants&location=" + @lat.to_s + "," + @lon.to_s + "&radius=" + @radius.to_s + "&types=restaurant&key=AIzaSyBWIRHm8UOx8VLKT5x13thHOO-2O0HtJKs"
parsed_result = call_google_places_api(search_url)
#pp parsed_result
parsed_result['results'].each do |result|
restaurants[result['name']] = result['place_id']
end
place_id = restaurants[restaurants.keys.sample]
if place_id.nil?
@has_error = true
else
puts "IN NEW ELSE"
#do a details search to retrieve details about the selected location
details_url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=" + place_id + "&key=" + ENV["GOOGLE_PLACES"]
#details_url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=" + place_id + "AIzaSyBWIRHm8UOx8VLKT5x13thHOO-2O0HtJKs"
details_result = call_google_places_api(details_url)
#pp details_result
if details_result['result']['formatted_address'] != nil
@address = details_result['result']['formatted_address']
end
#THINK ABOUT REMOVING THIS:
if details_result['result']['place_id'] != nil
@placeid = details_result['result']['place_id']
end
if details_result['result']['formatted_phone_number'] != nil
@phone_number = details_result['result']['formatted_phone_number']
end
if details_result['result']['name'] != nil
@name = details_result['result']['name']
end
if details_result['result']['rating'] != nil
@rating = details_result['result']['rating']
end
if details_result['result'].include? "price_level" #&& details_result['result']['price_level'] != nil
@price_level = details_result['result']['price_level']
@price_level = "$" * @price_level
end
if details_result['result'].include? "opening_hours" #&& details_result['result']['opening_hours']['open_now'] != nil
if details_result['result']['opening_hours']['open_now'] == true
@open_now = "Open now"
else
@open_now = "Closed"
end
if details_result['result']['opening_hours']['periods'][Time.now.wday] != nil
@periods = details_result['result']['opening_hours']['periods'][Time.now.wday]
@periods = Time.parse(@periods['open']['time'].insert(2,":")).strftime("%I:%M%p")+ " - " + Time.parse(@periods['close']['time'].insert(2,":")).strftime("%I:%M%p")
end
@weekday_text = details_result['result']['opening_hours']['weekday_text']
#example: https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJIZtG1mlaQIgR6gd0v9uNliE&key=AIzaSyB3xKb4v0cK805_F1ApSX0Os0KS-XzDoO4
end
end
end
def has_error
return @has_error
end
end
get '/places/:error' do
@error = params[:error]
erb :restaurant
end
get '/places' do
lat = params[:lat]
lon = params[:lon]
<<<<<<< HEAD
@place = Places.new(lat, lon, )
#
# @weather = Weathers.new(lat,lon)
#
# erb :restaurant
# "Coords: " + lat + lon
# @place.to_json
=======
@place = Places.new(lat, lon)
>>>>>>> 63231e3eafb848562a5c87de67f110e788eebd79
puts "======================"
puts
h = {}
if @place.has_error
h['error'] = true;
else
@place.instance_variables.each { |var| h[var.to_s.delete("@").to_sym] = @place.instance_variable_get(var)}
#puts @place.instance_variables
#print h
puts
puts @place.open_now
end
puts "======================"
content_type :json
#{:name => @place.name, :rating => @place.rating, :phone => @place.phone_number, :address => @place.address}.to_json
#Hash[*@place.instance_variables.map{ |var| [var.to_s.delete("@").to_sym, @place.instance_variable_get(var)]}.flatten].to_json
h.to_json
end
post '/places' do
@json = JSON.parse(request.body.read)
puts "IN PLACES"
print @json
puts
query = @json["locationInput"]
radius = @json["radius"]
radius = radius.to_i * 1609
puts "radius:" + radius.to_s
puts "query: " + query
puts "before IF in post places"
if query.empty?
lat = params[:lat]
lon = params[:lon]
puts lat
puts lon
@place = Places.new(lat, lon, radius)
@weather = Weathers.new(lat,lon)
else
puts "INELSE " + query
settings = Maps.new(query)
settings.get_position
puts settings.lat
puts settings.lon
@place = Places.new(settings.lat, settings.lon, radius)
@weather = Weathers.new(settings.lat, settings.lon)
end
content_type :json
#move to own method
h = {}
@place.instance_variables.each { |var| h[var.to_s.delete("@").to_sym] = @place.instance_variable_get(var)}
h.to_json
end