Twitter on Rails

Michael Bleigh, RailsConf 2009

Wednesday, May 6, 2009

Wednesday, May 6, 2009

Wednesday, May 6, 2009

What’s the big f-ing deal?

Wednesday, May 6, 2009

MySpace

Wednesday, May 6, 2009

MySpace

Wednesday, May 6, 2009

Facebook

Wednesday, May 6, 2009

Facebook

Wednesday, May 6, 2009

How is different?

Wednesday, May 6, 2009

Audience.
Ben Garney via Flickr
Wednesday, May 6, 2009

Public.

three-B via Flickr
Wednesday, May 6, 2009

Search.

Vitó via Flickr
Wednesday, May 6, 2009

API.

benben via Flickr
Wednesday, May 6, 2009

In Code

Wednesday, May 6, 2009

Search API

Wednesday, May 6, 2009

require 'open-uri' require 'json' def search_twitter_for(query) query = CGI.escape(query) JSON.parse( open("http://search.twitter.com/search.json?q=#{query}").read )['results'] end

search_twitter_for('RailsConf').collect{|s| s['text']}

[ "Packing up for #railsconf. Swine flu here we come!", "Putting new strings on some guitars for the #RailsConf music jam”, "Vegas, baby! All the @contrast boys laying into their first drinks...”, ... ]

Wednesday, May 6, 2009

OAuth
Wednesday, May 6, 2009

TCM Hitchhiker via Flickr
Wednesday, May 6, 2009

There’s a gem for that.
ArtWerk via Flickr
Wednesday, May 6, 2009

The OAuth Shuffle
• • Request Token Authorization • • Access!
Consumer
Wednesday, May 6, 2009

Sign in with Twitter
• • Request Token Authorize (Just Once) • • Access!
Consumer
Wednesday, May 6, 2009

gem install oauth --version 0.3.2

require 'oauth' require 'json' # register your application at http://twitter.com/apps consumer = OAuth::Consumer.new( 'mykey','mysecret', :site => 'http://twitter.com/' ) request_token = consumer.get_request_token puts request_token.authorize_url # go and visit the authorize URL access_token = request_token.get_access_token me = JSON.parse(access_token.get('/account/verify_credentials.json').body) me['screen_name'] # => 'mbleigh'

Wednesday, May 6, 2009

Caveat OAuth
• •
The Advisory (no callbacks)

• Solution: One Dev App
Read/Write Inconsistency

• Solution: Delete and Recreate

Wednesday, May 6, 2009

Rails
Wednesday, May 6, 2009

TwitterAuth
github.com/mbleigh/twitter-auth

Wednesday, May 6, 2009

TwitterAuth makes Rails Twitter apps stupid simple.

Wednesday, May 6, 2009

Just enough magic.
Steffe via Flickr
Wednesday, May 6, 2009

Livecoding!

Wednesday, May 6, 2009

Twisteners
Who actually listens?

Wednesday, May 6, 2009

Application Goals
• Log in via Twitter • See a list of top mentioners • Tweet the top three “twisteners”
Wednesday, May 6, 2009

Step 1: Credentials

Wednesday, May 6, 2009

twitter.com/apps

Wednesday, May 6, 2009

Step 2: Template

Wednesday, May 6, 2009

Type the Magic Words
rails -m http://bit.ly/twitter-rails yourapp

Wednesday, May 6, 2009

TwitterAuth
• Engines-based login • logged_in? and current_user • user.twitter.get(‘/api/path’)
Wednesday, May 6, 2009

Twitter Template

• helpers for common twitter needs • instant gratification

Wednesday, May 6, 2009

Step 3: Aggregate

Wednesday, May 6, 2009

Step 4: Share

Wednesday, May 6, 2009

twisteners.heroku.com
github.com/mbleigh/twisteners

Wednesday, May 6, 2009

Follow me:

@mbleigh
Wednesday, May 6, 2009