Skip to content
Browse files

made the demo look better, updated the documentation

  • Loading branch information...
1 parent dab88ea commit b93fbf2faafad9928cb43366c6f4eb2e3725e792 @moomerman moomerman committed Mar 21, 2009
Showing with 149 additions and 24 deletions.
  1. +68 −1 README.textile
  2. +14 −0 public/main.css
  3. +6 −3 sinitter.rb
  4. +41 −20 views/home.erb
  5. +20 −0 views/layout.erb
View
69 README.textile
@@ -23,4 +23,71 @@ sudo gem install moomerman-twitter_oauth</code></pre>
Run:
-<pre><code>./sinitter.rb</code></pre>
+<pre><code>./sinitter.rb</code></pre>
+
+The application should now be available at <code>http://localhost:4567/</code>
+
+h2. Step-by-step
+
+Firstly you need an instance of the Twitter OAuth API client with your credentials.
+
+<pre><code>@client = TwitterOAuth::Client.new(
+ :consumer_key => @@config['consumer_key'],
+ :consumer_secret => @@config['consumer_secret'],
+)</code></pre>
+
+This client handles all the communication and authentication with Twitter. The next stage is to prompt the user to click something on your site that initiates the authorization procedure - imagine this is a link to <code>/connect</code> on your site, this is what the code might look like
+
+<pre><code>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
+end
+</code></pre>
+
+We have just created a new request token and stored the details of the token in the session for when the user is returned to your site by Twitter. Then we have redirected to the authorize_url that will take the user to the Twitter site.
+
+When you configure your application details on Twitter you have to specify a callback URL. In this example let us assume it is <code>/auth</code>
+
+<pre><code>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 '/home'
+ else
+ redirect '/login'
+ end
+end
+</code></pre>
+
+Here we have retrieved the request token details from the session and asked Twitter to confirm the authorization, if this has all gone according to plan you will now have an access token for this user. The access token never expires (unless the user removes your aplication from their settings page) so you can use that in the future without having to do the authorization process again.
+
+To start making authorized requests we can now create an instance of the API client with this users access token
+
+<pre><code>@client = TwitterOAuth::Client.new(
+ :consumer_key => @@config['consumer_key'],
+ :consumer_secret => @@config['consumer_secret'],
+ :token => session[:access_token],
+ :secret => session[:secret_token]
+)
+</code></pre>
+
+Now we're ready to call the API on behalf of this user. Here is an example action that updates the users status on twitter
+
+<pre><code>post '/update' do
+ @client.update(params[:update])
+ redirect '/'
+end
+</code></pre>
+
+That's it!
View
14 public/main.css
@@ -0,0 +1,14 @@
+body {font-size:16px; font-family:verdana;}
+
+#header h1 {margin:0;}
+
+#content {
+ margin:0 auto;
+ width:850px;
+}
+
+#connect_button {
+ margin:20 auto;
+ width:250px;
+ font-size:20px;
+}
View
9 sinitter.rb
@@ -6,13 +6,16 @@
configure do
set :sessions, true
@@config = YAML.load_file("config.yml") rescue nil || {}
- TwitterOAuth::Client::CLIENT_KEY = @@config['consumer_key']
- TwitterOAuth::Client::CLIENT_SECRET = @@config['consumer_secret']
end
before do
@user = session[:user]
- @client = TwitterOAuth::Client.new(:token => session[:access_token], :secret => session[:secret_token])
+ @client = TwitterOAuth::Client.new(
+ :consumer_key => @@config['consumer_key'],
+ :consumer_secret => @@config['consumer_secret'],
+ :token => session[:access_token],
+ :secret => session[:secret_token]
+ )
end
get '/' do
View
61 views/home.erb
@@ -1,28 +1,49 @@
-<h1>Sinitter</h1>
-
<% unless @user %>
+ <div id="connect_button">
+ <a href="/connect">-> Login via Twitter</a>
+ </div>
+ <h2>About</h2>
<p>
- Sinitter is an application to demonstrate the integration of Twitter OAuth with sinatra.
- It also serves as a live test for the <a href="http://github.com/moomerman/twitter_oauth/tree/master">twitter_oauth</a> Ruby gem.
- The <a href="http://github.com/moomerman/sinitter/tree/master">source</a> for this application is also available on github.
+ Sinitter is an application to demonstrate the integration of Twitter OAuth with sinatra and
+ also serves as a live test for the <a href="http://github.com/moomerman/twitter_oauth/tree/master">twitter_oauth</a> Ruby gem.
</p>
- <p><a href="/connect">Connect with Twitter</a></p>
+ <p>The <a href="http://github.com/moomerman/sinitter/tree/master">source code</a> for this application is available on github.</p>
<% else %>
- <p><a href='/disconnect'>Disconnect</a> [<%= @client.authorized? %>]</p>
-
- <form action='/update' method="POST">
- <textarea name="update" rows="4" cols="50"></textarea>
- <input type="submit" value="Post to Twitter"/>
- </form>
+ <div style="float:right">
+ <p><a href='/disconnect'>Logout</a></p>
+ </div>
+ <br clear="all"/>
- <% @client.friends_timeline.each do |status| %>
- <p>
- <img src="<%= status['user']['profile_image_url'] %>"/>
- <a href="http://twitter.com/<%= status['user']['screen_name'] %>">@<%= status['user']['screen_name'] %></a>
- <%= status['text'] %>
- </p>
- <% end %>
+ <div style="margin:0 auto; width:700px">
+ <div style="padding-left:90px">
+ <form action='/update' method="POST">
+ <textarea name="update" rows="4" cols="50"></textarea>
+ <input type="submit" value="Post to Twitter"/>
+ </form>
+ </div>
+
+ <br/>
+
+ <% @client.friends_timeline.each do |status| %>
+ <p>
+ <div style="float:left; margin:5px">
+ <img src="<%= status['user']['profile_image_url'] %>" width="48" height="48"/>
+ </div>
+ <div style="">
+ <a href="http://twitter.com/<%= status['user']['screen_name'] %>">@<%= status['user']['screen_name'] %></a>
+ <%= status['text'] %>
+ </div>
+ <br clear="all"/>
+ </p>
+ <% end %>
+ </div>
<% end %>
<% rate = @client.rate_limit_status %>
-<small>API calls remaining: <%= rate['remaining_hits'] %>/<%= rate['hourly_limit'] %> per hour</small>
+
+<br/>
+<small>
+ API calls remaining: <%= rate['remaining_hits'] %>/<%= rate['hourly_limit'] %> per hour
+ <br/>
+ Created and maintained by Richard Taylor (<a href="http://twitter.com/moomerman">@moomerman</a>). Any comments/suggestions welcomed
+</small>
View
20 views/layout.erb
@@ -0,0 +1,20 @@
+<html>
+<head>
+ <title>Sinitter - Twitter OAuth Demo Application</title>
+ <link href="/main.css" media="screen" rel="Stylesheet" type="text/css" />
+</head>
+
+<body>
+
+<div id="content">
+ <div id="header">
+ <div>
+ <h1>Sinitter</h1>
+ </div>
+ <br clear="all"/>
+ </div>
+ <%= yield %>
+</div>
+
+</body>
+</html>

0 comments on commit b93fbf2

Please sign in to comment.
Something went wrong with that request. Please try again.