Created
November 4, 2009 06:03
-
-
Save beccasaurus/225841 to your computer and use it in GitHub Desktop.
Sinatra-based "member-only" Twitter site
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
%w( rubygems sinatra haml sass rack/oauth json ).each {|lib| require lib } | |
use Rack::Session::Cookie | |
use Rack::OAuth, :site => 'http://twitter.com', | |
:key => 'bKtzVmvJoCYjyK1mUYaUQ', | |
:secret => 'CmOM0APjvmT8z86DA0cXPQWXGZGfLKTpmdTUGzxqnU', | |
:redirect => '/' | |
configure do | |
dev_db = "sqlite3://#{ File.expand_path(File.dirname(__FILE__) + '/db/development.sqlite3') }" | |
DataMapper.setup :default, ENV['DATABASE_URL'] || dev_db | |
DataMapper.auto_upgrade! | |
end | |
helpers do | |
include Rack::OAuth::Methods | |
end | |
get '/' do | |
if get_access_token | |
@info = JSON.parse get_access_token.get('/account/verify_credentials.json').body | |
haml :home | |
else | |
haml :login | |
end | |
end | |
get '/styles.css' do | |
content_type 'text/css' | |
sass :stylesheet | |
end | |
get '/tweets' do | |
@tweets = JSON.parse get_access_token.get('/statuses/user_timeline.json').body | |
haml :tweets | |
end | |
post '/tweet' do | |
response = get_access_token.post('/statuses/update.json', :status => params[:tweet]) | |
if response.code == '200' | |
session[:message] = "Tweeted: #{ params[:tweet].inspect }" | |
redirect '/' | |
else | |
"Response wasn't OK? Raw response: #{ response.to_yaml }" | |
end | |
end | |
get '/logout' do | |
session.clear | |
redirect '/' | |
end | |
__END__ | |
@@ layout | |
!!! | |
!!! XML | |
%html | |
%head | |
%title Member-Only Twitter Site | |
%link{ :rel => 'stylesheet', :type => 'text/css', :href => '/styles.css' } | |
%body | |
%h1 | |
%a{ :href => '/' } Member-Only Twitter Site | |
= yield | |
@@ home | |
- if msg = session.delete(:message) | |
%h3= msg | |
%h2== Welcome #{ @info['screen_name'] } | |
%p Thank you for logging in via Twitter! | |
%p | |
We would look at your stats and maybe some of your tweets | |
to determine if we want to accept your to our member-only site or not. | |
%fieldset | |
%legend Twitter Stats | |
%dl | |
%dt Followers | |
%dd= @info['followers_count'] | |
%dt Friends | |
%dd= @info['friends_count'] | |
%dt Total Tweets | |
%dd= @info['statuses_count'] | |
%dt Protected Profile? | |
%dd= @info['protected'].inspect | |
%dt Account Created | |
%dd= @info['created_at'] | |
%fieldset | |
%legend Read / Write Access | |
%p | |
To demonstrate that we can read from your twitter account | |
%a{ :href => '/tweets' } click here | |
to view some of your recent tweets. | |
%p | |
To demonstrate that we can write to your twitter account | |
%form{ :action => '/tweet', :method => 'post' } | |
%textarea{ :name => 'tweet', :rows => 5, :cols => 40 } | |
%input{ :type => 'submit', :value => 'Click here to Tweet this text!', :style => 'display: block;' } | |
@@ login | |
%p This is a member-only site. | |
%p | |
To login or to try to join, you must | |
%a{ :href => oauth_login_path } login via Twitter | |
@@ tweets | |
%p Some of your recent tweets: | |
%ul | |
- for tweet in @tweets | |
%li= tweet['text'] | |
@@ stylesheet | |
html, body | |
:padding 0 | |
:margin 0 | |
body | |
:padding 40px | |
:font 14px/1.5em arial, helvetica, sans-serif | |
a | |
:text-decoration none | |
:color #2C44B8 | |
fieldset | |
:width 45% | |
:float left | |
dl | |
dt | |
:font-weight bold |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment