-
Notifications
You must be signed in to change notification settings - Fork 13
/
sinitter.rb
executable file
·98 lines (85 loc) · 2.31 KB
/
sinitter.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
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
require 'twitter_oauth'
configure do
set :sessions, true
@@config = YAML.load_file("config.yml") rescue nil || {}
end
before do
next if request.path_info =~ /ping$/
@user = session[:user]
@client = TwitterOAuth::Client.new(
:consumer_key => @@config['consumer_key'],
:consumer_secret => @@config['consumer_secret'],
:token => session[:access_token],
:secret => session[:secret_token]
)
@rate_limit_status = @client.rate_limit_status
end
get '/' do
redirect '/timeline' if @user
@tweets = @client.public_timeline
erb :home
end
get '/timeline' do
@tweets = @client.friends_timeline
erb :timeline
end
post '/update' do
@client.update(params[:update])
redirect '/timeline'
end
get '/messages' do
@sent = @client.sent_messages
@received = @client.messages
erb :messages
end
get '/search' do
query = params[:q] || ''
@search = @client.search(query, params[:page])
erb :search
end
# store the request tokens and send to Twitter
get '/connect' do
request_token = @client.request_token
session[:request_token] = request_token.token
session[:request_token_secret] = request_token.secret
redirect request_token.authorize_url.gsub('authorize', 'authenticate')
end
# auth URL is called by twitter after the user has accepted the application
# this is configured on the Twitter application settings page
get '/auth' do
# Exchange the request token for an access token.
@access_token = @client.authorize(
session[:request_token],
session[:request_token_secret]
)
if @client.authorized?
# Storing the access tokens so we don't have to go back to Twitter again
# in this session. In a larger app you would probably persist these details somewhere.
session[:access_token] = @access_token.token
session[:secret_token] = @access_token.secret
session[:user] = true
redirect '/timeline'
else
redirect '/'
end
end
get '/disconnect' do
session[:user] = nil
session[:request_token] = nil
session[:request_token_secret] = nil
session[:access_token] = nil
session[:secret_token] = nil
redirect '/'
end
# useful for site monitoring
get '/ping' do
'pong'
end
helpers do
def partial(name, options={})
erb("_#{name.to_s}".to_sym, options.merge(:layout => false))
end
end