-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.rb
103 lines (81 loc) · 2.42 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
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
require 'sinatra'
require 'pusher'
require 'lib/shoutbox'
require 'rack-flash'
require 'omniauth'
require 'pp'
enable :sessions
use Rack::Flash, :accessorize => [:notice, :error]
Pusher.app_id = Shoutbox.pusher_app_id
Pusher.key = Shoutbox.pusher_key
Pusher.secret = Shoutbox.pusher_secret
use OmniAuth::Builder do
provider :twitter, Shoutbox.twitter_consumer_key, Shoutbox.twitter_consumer_secret
end
OmniAuth.config.full_host = Shoutbox.full_host if Shoutbox.full_host
set :public_folder, File.dirname(__FILE__) + '/public'
# Lets start that some bitch
Shoutbox.initialize
get '/' do
redirect to('/index.html')
end
get '/signup' do
<<-HTML
<a href='/auth/twitter'>Sign in with Twitter</a>
HTML
end
get '/logout' do
logout
redirect to('/index.html?flash_notice=Successfully%20logged%20out.')
end
get '/auth/:name/callback' do
session[:user_name] = account_name_from_omniauth
redirect to('/shoutbox.html')
end
get '/auth/failure' do
File.read('public/failure.html')
end
get '/data' do
content_type :json
Shoutbox.get_current_status(current_user)
end
put '/status' do
content_type :json
@shoutbox_data = Shoutbox::ShoutboxData.from_json_string(request.body.read)
Shoutbox.update_status(current_user, @shoutbox_data)
"OK"
end
delete '/status' do
content_type :json
@shoutbox_data = Shoutbox::ShoutboxData.from_json_string(request.body.read)
Shoutbox.delete_status(current_user, @shoutbox_data)
"OK"
end
post '/pusher/auth' do
content_type :json
if current_user && "private-#{current_user}" == params[:channel_name]
Pusher[params[:channel_name]].authenticate(params[:socket_id]).to_json
end
end
private
def current_user
@current_user ||= session[:user_name] || account_name_from_auth_token || account_name_from_omniauth
throw(:halt, [401, "Unable to identify you\n"]) unless @current_user
response.headers['X-Shoutbox-Auth-Token'] = Shoutbox.auth_token_for( @current_user )
response.headers['X-Shoutbox-Account-Name'] = @current_user
end
def logout
@current_user = nil
session[:user_name] = nil
end
def account_name_from_omniauth
auth_hash = request.env['omniauth.auth']
auth_hash['user_info']['nickname'] if auth_hash and auth_hash['user_info']
end
def account_name_from_auth_token
if auth_token = request.env['HTTP_X_SHOUTBOX_AUTH_TOKEN']
return Shoutbox.get_account_name_from_auth_token( auth_token )
end
nil
end