-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.rb
63 lines (52 loc) · 1.79 KB
/
app.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
require "rubygems"
require "sinatra"
require "oauth"
require "oauth/consumer"
require 'grackle'
require 'haml'
enable :sessions
before do
session[:oauth] ||= {}
consumer_key = ENV["consumer_key"]
consumer_secret = ENV["consumer_secret"]
@consumer ||= OAuth::Consumer.new(consumer_key, consumer_secret, :site => "http://twitter.com")
if !session[:oauth][:request_token].nil? && !session[:oauth][:request_token_secret].nil?
@request_token = OAuth::RequestToken.new(@consumer, session[:oauth][:request_token], session[:oauth][:request_token_secret])
end
if !session[:oauth][:access_token].nil? && !session[:oauth][:access_token_secret].nil?
@access_token = OAuth::AccessToken.new(@consumer, session[:oauth][:access_token], session[:oauth][:access_token_secret])
end
if @access_token
@client = Grackle::Client.new(:auth => {
:type => :oauth,
:consumer_key => consumer_key,
:consumer_secret => consumer_secret,
:token => @access_token.token,
:token_secret => @access_token.secret
})
end
end
get "/" do
if @access_token
@statuses = @client.statuses.friends_timeline? :count => 100
haml :index
else
'<a href="/request">Sign On</a>'
end
end
get "/request" do
@request_token = @consumer.get_request_token(:oauth_callback => "http://#{request.host}/auth")
session[:oauth][:request_token] = @request_token.token
session[:oauth][:request_token_secret] = @request_token.secret
redirect @request_token.authorize_url
end
get "/auth" do
@access_token = @request_token.get_access_token :oauth_verifier => params[:oauth_verifier]
session[:oauth][:access_token] = @access_token.token
session[:oauth][:access_token_secret] = @access_token.secret
redirect "/"
end
get "/logout" do
session[:oauth] = {}
redirect "/"
end