-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
188 lines (163 loc) · 5.99 KB
/
server.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
#!/usr/bin/env ruby
require 'sinatra'
require 'rubygems'
require 'rest_client'
require 'json'
require 'set'
require 'mongo'
require 'uri'
require 'pp'
require 'open-uri'
require 'date'
include Mongo
$stdout.sync = true
configure :production do
require 'newrelic_rpm'
end
configure do
db_details = URI.parse(ENV['MONGOHQ_URL'])
conn = MongoClient.new(db_details.host, db_details.port)
db_name = db_details.path.gsub(/^\//, '')
db = conn.db(db_name)
db.authenticate(db_details.user, db_details.password) unless (db_details.user.nil? || db_details.user.nil?)
set :mongo_db, db
set :appUrl, "http://map4access.herokuapp.com"
enable :logging
puts "dbconnection successful to #{ENV['MONGOHQ_URL']}"
end
get '/' do
redirect "/places"
end
get '/places' do
erb :places, :locals => {:page => "places", :page_title => "Find places nearby"}
end
get '/xhrnearbyplaces/:lat/:lng' do
content_type :json
getNearbyPlacesFromFoursquare(params[:lat], params[:lng], 10, 500).to_json
end
get '/xhrnearbyplacessearch/:lat/:lng/:query' do
content_type :json
getSearchResultsNearMeFromFoursquare(params[:lat], params[:lng], params[:query], 10, 5000).to_json
end
get '/xhrplacesnearnamedplace/:query/:near' do
content_type :json
getSearchResultsNearNamedPlace(URI.encode(params[:query]), params[:near], 20).to_json
end
get '/assess/:venueid' do
venue = getVenueFromFoursquare(params[:venueid])
venue_name = venue["response"]["venue"]["name"]
categories = venue["response"]["venue"]["categories"]
venue_type = "?"
if (categories!=nil && categories.length > 0)
venue_type = categories[0]["name"]
end
erb :assess, :locals => {:page => "assess", :page_title => "Assessing #{venue_name}, #{venue_type}", :venueid => params[:venueid], :venue => venue["response"]["venue"], :venue_name => venue_name, :venue_type => venue_type}
end
get '/reviews' do
redirect "/places"
end
get '/postreviews/:venueid' do
redirect "/assess/" + params[:venueid]
end
post '/assessment' do
@pretty_json = JSON.pretty_generate(params)
@assessment = params
erb :assessment, :locals => {:page => "assessment", :page_title => "Your assessment"}
end
get '/assess_summary' do
assessmentsCol = settings.mongo_db['assessments']
assessmentsByType = assessmentsCol.aggregate([{"$group" => {_id: "$venue_type", assessments: {"$sum" => 1}}}])
@byTypeHash= {}
@total = 0
assessmentsByType.each do | entry |
@byTypeHash[entry["_id"]] = entry["assessments"]
@total += entry["assessments"]
end
erb :assess_summary, :locals => {:page => "assess_summary", :page_title => "Assessment Summary"}
end
get '/assess_map' do
erb :assess_map, :locals => {:page => "assess_map", :page_title => "Assessment Map (trial)"}
end
post '/postassessment' do
assessmentsCol = settings.mongo_db['assessments']
params['assessmentTimestamp'] = Time.new
assessmentsCol.insert(params)
erb :postassessment, :locals => {:page => "postassessment", :page_title => "Thanks for your help!"}
end
def getVenueFromFoursquare(venueid)
foursquare_id = ENV['FOURSQUARE_KEY']
foursquare_secret = ENV['FOURSQUARE_SECRET']
url = "https://api.foursquare.com/v2/venues/#{venueid}?client_id=#{foursquare_id}&client_secret=#{foursquare_secret}&v=20140715"
response = RestClient.get url
venue = JSON.parse(response)
venue
end
def sendAlertMail(subject, body)
if (ENV['MAIL_ACTIVE'] == 'true')
message = Mail.new do
from ENV['ADMIN_EMAIL_FROM']
to ENV['ADMIN_EMAIL_TO']
subject subject
body body
delivery_method Mail::Postmark, :api_key => ENV['POSTMARK_API_KEY']
end
message.deliver
end
end
# Converts a street address to a GeoJSON object via the mapquest API
def getGeoJSON(address)
stationOutageTrackerCol = settings.mongo_db['geocodes']
cache_result = stationOutageTrackerCol.find_one({:_id => address})
if cache_result==nil # address doesn't exist in global variable
mapquestKey = ENV['MAPQUEST_API_KEY']
geocodeRequestUri = "http://open.mapquestapi.com/geocoding/v1/address?key=#{mapquestKey}&location=#{address}"
geoCodeResponse = RestClient.get geocodeRequestUri
jsonResults = JSON.parse(geoCodeResponse)
if jsonResults['info']['statuscode'] == 403 # Request failed
latLng = {"lng" => 0,"lat" => 0}
latLng["_id"] = address
stationOutageTrackerCol.insert(latLng)
elsif jsonResults['results'][0]['locations'].length > 0
latLng = jsonResults['results'][0]['locations'][0]['latLng']
$yelpAddressLatLng[address] = latLng
latLng["_id"] = address
stationOutageTrackerCol.insert(latLng)
else
latLng = {"lng" => 0,"lat" => 0}
latLng["_id"] = address
stationOutageTrackerCol.insert(latLng)
end
else # address exists in global variable
latLng = cache_result
end
return latLng
end
def getVenueFromFoursquare(venueid)
url = "https://api.foursquare.com/v2/venues/#{venueid}?#{getFsKeySecretVersionString()}"
response = RestClient.get url
venue = JSON.parse(response)
venue
end
def getNearbyPlacesFromFoursquare(lat, lng, limit, radius)
url = "https://api.foursquare.com/v2/venues/search?ll=#{lat},#{lng}&limit=#{limit}&radius=#{radius}&#{getFsKeySecretVersionString()}"
response = RestClient.get url
venue = JSON.parse(response)
venue
end
def getSearchResultsNearMeFromFoursquare(lat, lng, query, limit, radius)
url = "https://api.foursquare.com/v2/venues/search?ll=#{lat},#{lng}&query=#{query}&limit=#{limit}&radius=#{radius}&#{getFsKeySecretVersionString()}"
response = RestClient.get url
venue = JSON.parse(response)
venue
end
def getSearchResultsNearNamedPlace(query, near, limit)
url = "https://api.foursquare.com/v2/venues/search?query=#{query}&near=#{near}&limit=#{limit}&#{getFsKeySecretVersionString()}"
response = RestClient.get url
venue = JSON.parse(response)
venue
end
def getFsKeySecretVersionString()
foursquare_key = ENV['FOURSQUARE_KEY']
foursquare_secret = ENV['FOURSQUARE_SECRET']
"client_id=#{foursquare_key}&client_secret=#{foursquare_secret}&v=20141015"
end