Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

78 lines (68 sloc) 2.089 kB
require 'rubygems'
require 'sinatra'
require 'erb'
require 'yaml'
require 'twitter_oauth'
require 'lib/sinatra/gigonaut_helpers'
require 'data/init'
configure do
set :sessions, true
@@config = YAML.load_file("config/config.yml") rescue nil || {}
end
before do
next if request.path_info =~ /ping$/
@user = session[:user]
@client = TwitterOAuth::Client.new(
:consumer_key => ENV['CONSUMER_KEY'] || @@config['consumer_key'],
:consumer_secret => ENV['CONSUMER_SECRET'] || @@config['consumer_secret'],
:token => session[:access_token],
:secret => session[:secret_token]
)
@rate_limit_status = @client.rate_limit_status
end
get '/' do
erb :index
end
get '/account' do
erb :account
end
# store the request tokens and send to Twitter
get '/connect' do
request_token = @client.request_token(
:oauth_callback => ENV['CALLBACK_URL'] || @@config['callback_url']
)
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.
begin
@access_token = @client.authorize(
session[:request_token],
session[:request_token_secret],
:oauth_verifier => params[:oauth_verifier]
)
rescue OAuth::Unauthorized
end
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 '/account'
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
Jump to Line
Something went wrong with that request. Please try again.