Skip to content

Instantly share code, notes, and snippets.

@chrisconley
Created November 12, 2009 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisconley/232968 to your computer and use it in GitHub Desktop.
Save chrisconley/232968 to your computer and use it in GitHub Desktop.
class HuzzahsController < ApplicationController
before_filter :login_required
before_filter :set_twitter_oauth_client
def connect
request_token = @client.request_token
session[:twitter_request_token] = request_token.token
session[:twitter_request_token_secret] = request_token.secret
redirect_to request_token.authorize_url
end
def twitter_auth
@access_token = @client.authorize(
session[:twitter_request_token],
session[:twitter_request_token_secret])
if @client.authorized?
session[:twitter_access_token] = @access_token.token
session[:twitter_secret_token] = @access_token.secret
end
redirect_to session[:huzzah_redirect_to]
end
def new
@huzzah = current_user.huzzahs.build(params[:huzzah])
@huzzah.bid = @bid
if session[:twitter_access_token] && session[:twitter_secret_token]
@twitter_user = @client.info
render :action => 'new'
else
session[:huzzah_redirect_to] = request.url
render :action => 'pitstop'
end
end
def create
@huzzah = current_user.huzzahs.build(params[:huzzah])
@huzzah.bid = @bid
if update = @client.update(@huzzah.message)
@huzzah.save
flash[:notice] = "Your huzzah has been posted to Twitter! http://twitter.com/#{update["user"]["screen_name"]}/statuses/#{update["id"]}"
redirect_to root_url
else
flash[:notice] = "We're sorry but we had trouble communicating with Twitter. Please try again"
render :action => "new"
end
end
def set_twitter_oauth_client
@client = TwitterOAuth::Client.new(
:consumer_key => ::APP_CONFIG[:twitter_oath_keys]['consumer_key'],
:consumer_secret => ::APP_CONFIG[:twitter_oath_keys]['consumer_secret'],
:token => session[:twitter_access_token],
:secret => session[:twitter_secret_token]
)
end
end
%h1 Send a Twitter Huzzah!
%fieldset
%legend= "To be sent from your Twitter username: #{@twitter_user['screen_name']}"
= error_messages_for :huzzah
- form_for @huzzah, :url => bid_huzzahs_path(@bid) do |f|
%p
= f.label :message
%br
= f.text_area :message
= f.hidden_field :posted_to, :value => 'twitter'
%p
= f.submit 'Huzzah!'
= link_to 'Send a Huzzah to Twitter', connect_new_huzzah_path
You'll be sent to Twitter to authenticate your Twitter account. Then you'll be redirected back here where you can update your status with your winning bid details!
map.resources :huzzahs
map.connect_new_huzzah '/huzzahs/new/connect', :controller => 'huzzahs', :action => 'connect'
map.twitter_auth_new_huzzah '/huzzahs/new/twitter_auth', :controller => 'huzzahs', :action => 'twitter_auth'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment