forked from bilus/pythia
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pythia.rb
147 lines (117 loc) · 3.5 KB
/
pythia.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
require 'sinatra'
require 'koala'
enable :sessions
set :raise_errors, false
set :show_exceptions, false
set :protection, :except => :frame_options
# Scope defines what permissions that we are asking the user to grant.
# In this example, we are asking for the ability to publish stories
# about using the app, access to what the user likes, and to be able
# to use their pictures. You should rewrite this scope with whatever
# permissions your app needs.
# See https://developers.facebook.com/docs/reference/api/permissions/
# for a full list of permissions
FACEBOOK_SCOPE = 'user_likes,user_photos'
unless ENV["FACEBOOK_APP_ID"] && ENV["FACEBOOK_SECRET"]
abort("missing env vars: please set FACEBOOK_APP_ID and FACEBOOK_SECRET with your app credentials")
end
before do
# HTTPS redirect
if settings.environment == :production && request.scheme != 'https'
redirect "https://#{request.env['HTTP_HOST']}"
end
end
helpers do
def host
request.env['HTTP_HOST']
end
def scheme
request.scheme
end
def url_no_scheme(path = '')
"//#{host}#{path}"
end
def url(path = '')
"#{scheme}://#{host}#{path}"
end
def authenticator
@authenticator ||= Koala::Facebook::OAuth.new(ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_SECRET"], url("/auth/facebook/callback"))
end
# allow for javascript authentication
def access_token_from_cookie
authenticator.get_user_info_from_cookies(request.cookies)['access_token']
rescue => err
warn err.message
end
def access_token
session[:access_token] || access_token_from_cookie
end
end
# the facebook session expired! reset ours and restart the process
error(Koala::Facebook::APIError) do
session[:access_token] = nil
redirect "/auth/facebook"
end
get "/fb" do
puts params.inspect
# Get base API Connection
@graph = Koala::Facebook::API.new(access_token)
# Get public details of current application
@app = @graph.get_object(ENV["FACEBOOK_APP_ID"])
if access_token
@user = @graph.get_object("me")
@friends = @graph.get_connections('me', 'friends')
@photos = @graph.get_connections('me', 'photos')
@likes = @graph.get_connections('me', 'likes').first(4)
# for other data you can always run fql
@friends_using_app = @graph.fql_query("SELECT uid, name, is_app_user, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1")
end
if params["app_data"] == "ask"
# if params["fb_source"] == "notification"
# haml :vote
# else
erb :ask
else
haml :index
end
end
# used by Canvas apps - redirect the POST to be a regular GET
post "/" do
redirect "/fb?app_data=#{params["app_data"]}"
end
# used to close the browser window opened to post to wall/send to friends
get "/close" do
"<body onload='window.close();'/>"
end
# Doesn't actually sign out permanently, but good for testing
get "/preview/logged_out" do
session[:access_token] = nil
request.cookies.keys.each { |key, value| response.set_cookie(key, '') }
redirect '/'
end
# Allows for direct oauth authentication
get "/auth/facebook" do
session[:access_token] = nil
redirect authenticator.url_for_oauth_code(:permissions => FACEBOOK_SCOPE)
end
get '/auth/facebook/callback' do
session[:access_token] = authenticator.get_access_token(params[:code])
puts session[:access_token].inspect
redirect '/'
end
###############################
get '/' do
haml :index
end
get '/test' do
haml :test
end
get '/vote' do
haml :vote
end
get '/answers' do
haml :answers
end
get '/ask' do
erb :ask
end