-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
155 lines (134 loc) · 4.51 KB
/
main.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
class App < Sinatra::Base
set :sessions, true
get '/' do
erb :index
end
get '/login' do
@uuid = params[:uuid].to_s
user = User.where(:uuid => @uuid).first
if user.nil?
puts "creating user"
user = User.create(:uuid => @uuid, :score => 0)
end
content_type :json
{:user_id => user._id, :score => user.score}.to_json
end
get '/venues' do
@lat = params[:lat].to_f
@lng = params[:lng].to_f
@user = User.find(params[:user_id])
while @venue_groups.nil?
@venue_groups = JSON.parse(open("https://api.foursquare.com/v2/venues/search.json?ll=#{@lat},#{@lng}&client_id=#{api_key}&client_secret=#{api_secret}").read)["response"]["groups"]
end
@venue = nil
@venue_groups.each do |venue_group|
venue_set = venue_group["items"]
venue_set.each do |venue|
existing_venue = Venue.where(:name => venue["name"], :location => {'$near' => [venue["location"]["lat"].to_f, venue["location"]["lng"].to_f] }).first
if existing_venue.nil?
new_venue = Venue.create(:name => venue["name"], :location => [venue["location"]["lat"].to_f, venue["location"]["lng"].to_f], :address => venue["location"]["address"], :city => venue["location"]["city"], :cross_street => venue["location"]["crossStreet"])
new_venue.categories = []
venue["categories"].each do |category|
new_venue.categories += [category["name"]]
end
new_venue.save!
@venue = new_venue
else
@venue = existing_venue if @user.venues.length <= 0
puts @user.venues.inspect
puts existing_venue.inspect
@venue = existing_venue if [email protected]?(existing_venue) && @user.venues.length > 0
end
break if [email protected]?
end
end
erb :venues
end
post '/venues' do
@answer = params[:answer]
@venue = Venue.find(params[:venue_id])
@user = User.find(params[:user_id])
@user.venues << @venue
@user.score = 0 if @user.score.nil?
if @answer.to_s.downcase == @venue.name.to_s.downcase
@user.score += 100
score_result = "CONGRATS, you are correct, You earned 100 points, Your total Score is #{@user.score}"
else
score_result = "Sorry, that is incorrect. The name of the place is #{@venue.name}. You total Score remains at #{@user.score}"
end
@user.save!
content_type :json
{:score_result => score_result}.to_json
end
# Authentication
get '/auth/foursquare' do
redirect(client.web_server.
authorize_url(:redirect_uri => redirect_uri, :scope => 'read-write'))
end
get '/auth/foursquare/callback' do
begin
response = client.
web_server.
get_access_token(params[:code], :redirect_uri => redirect_uri)
session[:access_token] = response.token
session[:refresh_token] = response.refresh_token
if session[:access_token]
redirect '/'
else
"Error retrieving access token."
end
rescue OAuth2::HTTPError => e
e.response.body
end
end
get '/auth/foursquare/refresh' do
if session[:refresh_token]
# This doesn't work. Patch oauth2?
response = client.
web_server.
get_access_token(nil, :refresh_token => session[:refresh_token], :type => 'refresh_token')
session[:access_token] = response.token
session[:refresh_token] = response.refresh_token
puts response.refresh_token
else
redirect '/auth/foursquare'
end
end
error OAuth2::HTTPError do
reset
end
error OAuth2::AccessDenied do
reset
end
protected
def client
api_key = ENV['API_KEY'] || api_key
api_secret = ENV['API_SECRET'] || api_secret
oauth_site = ENV['OAUTH_SITE'] || 'https://foursquare.com'
options = {
:site => ENV['SITE'] || 'https://api.foursquare.com',
:authorize_url => oauth_site.dup << '/api/oauth2/authenticate',
:access_token_url => oauth_site.dup << '/api/oauth2/access_token'
}
OAuth2::Client.new(api_key, api_secret, options)
end
def connection
OAuth2::AccessToken.new(client, session[:access_token], session[:refresh_token])
end
def headers
{'Accept' => 'application/json'}
end
def redirect_uri
uri = URI.parse(request.url)
uri.path = '/auth/foursquare/callback'
uri.query = nil
uri.to_s
end
# An error from Gowalla most likely means our token is bad. Delete it
# and re-authorize.
def reset
session.delete(:access_token)
session.delete(:refresh_token)
redirect('/auth/foursquare')
end
end