-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
149 lines (124 loc) · 3.38 KB
/
app.rb
File metadata and controls
149 lines (124 loc) · 3.38 KB
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
$stdout.sync = true
require 'sinatra'
require 'httparty'
require "sinatra/reloader" if development?
set :host, settings.development? ? "localhost:5000" : "www.pubs.io"
set :categories, {
"gastro_pub" => "4bf58dd8d48988d155941735",
"irish_pub" => "52e81612bcbc57f1066b7a06",
"pub" => "4bf58dd8d48988d11b941735"
}
set :default_api_params, defaults = {
client_id: ENV['FS_ID'],
client_secret: ENV['FS_SECRET'],
v: 20140806,
m: "foursquare"
}
enable :sessions
set :session_secret, "4bf58dd8d48988d11b94173552e81612bcbc57f1066b7a064bf58dd8d48988d155941735"
helpers do
def api method, endpoint, params = {}
params.update(settings.default_api_params)
params = method == :get ? {query: params} : {body: params}
HTTParty.send(method,"https://api.foursquare.com/v2#{endpoint}", params)
end
def signed_in?
!session[:token].nil?
end
def authenticate!
unless signed_in?
session[:post_signin_action] = JSON.dump({endpoint:request.path, params: params})
halt 401
end
end
def ensure_user_id
if signed_in? && session[:user_id].nil?
response = api(:get, "/users/self", {oauth_token: session[:token]})
session[:user_id] = response["response"]["user"]["id"]
end
end
end
get "/" do
ensure_user_id
erb :index
end
get "/signin" do
redirect "https://foursquare.com/oauth2/authenticate?client_id=#{ENV['FS_ID']}&response_type=code&redirect_uri=http://#{settings.host}/authenticate"
end
get "/authenticate" do
response = HTTParty.get("https://foursquare.com/oauth2/access_token", {query: {
grant_type: "authorization_code",
redirect_uri: "http://#{settings.host}/authenticate",
code: params[:code]
}.update(settings.default_api_params)})
session[:token] = JSON.parse(response.body)["access_token"]
ensure_user_id
if session[:post_signin_action]
action = JSON.parse(session[:post_signin_action])
response = api(:post, action['endpoint'], {
oauth_token: session[:token]
}.update(action['params']))
session[:post_signin_action] = nil
end
redirect "/"
end
get "/venues/:method" do
content_type :json
response = api(:get, request.path, {
ll: params[:ll],
intent: params[:intent],
radius: params[:radius],
categoryId: settings.categories[params[:category]],
})
response.body
end
post "/checkins/add" do
content_type :json
authenticate!
response = api(:post, request.path, {
venueId: params[:venueId],
oauth_token: session[:token],
})
response.body
end
post "/tips/add" do
content_type :json
authenticate!
response = api(:post, request.path, {
venueId: params[:venueId],
oauth_token: session[:token],
text: params[:text]
})
response.body
end
post "/tips/:id/delete" do
content_type :json
authenticate!
response = api(:post, "/tips/#{params[:id]}/delete", {
oauth_token: session[:token]
})
response.body
end
get "/venues/:id/:action" do
content_type :json
response = api(:get,request.path)
response.body
end
post "/venues/:id/like" do
content_type :json
authenticate!
response = api(:post,request.path,{
set: params[:set],
oauth_token: session[:token]
})
response.body
end
post "/venues/:id/dislike" do
content_type :json
authenticate!
response = api(:post,request.path,{
set: params[:set],
oauth_token: session[:token]
})
response.body
end