Skip to content
Browse files

some more touch-ups, prepping for some styling.

  • Loading branch information...
1 parent 2872fa4 commit ff1ac34585c7531230c0b47d6a628eb711f1f045 @gleuch committed Sep 7, 2009
Showing with 76 additions and 32 deletions.
  1. +55 −26 retweet.rb
  2. +4 −0 views/fail.haml
  3. +1 −0 views/home.haml
  4. +11 −2 views/layout.haml
  5. +4 −4 views/run.haml
  6. +1 −0 views/thanks.haml
View
81 retweet.rb
@@ -13,21 +13,29 @@
DataMapper.auto_upgrade!
set :sessions, true
-
- @title = 'Re-tweet Fucker / A F.A.T. Lab Project'
end
helpers do
def twitter_connect(user={})
- unless user.blank?
- @twitter_client = TwitterOAuth::Client.new(:consumer_key => configatron.twitter_oauth_token, :consumer_secret => configatron.twitter_oauth_secret, :token => user.oauth_token, :secret => user.oauth_secret) rescue nil
- else
- @twitter_client = TwitterOAuth::Client.new(:consumer_key => configatron.twitter_oauth_token, :consumer_secret => configatron.twitter_oauth_secret) rescue nil
+ begin
+ unless user.blank?
+ @twitter_client = TwitterOAuth::Client.new(:consumer_key => configatron.twitter_oauth_token, :consumer_secret => configatron.twitter_oauth_secret, :token => user.oauth_token, :secret => user.oauth_secret) rescue nil
+ else
+ @twitter_client = TwitterOAuth::Client.new(:consumer_key => configatron.twitter_oauth_token, :consumer_secret => configatron.twitter_oauth_secret) rescue nil
+ end
+ rescue
+ twitter_fail
end
# Do some error here if connection fails!
end
+ def twitter_fail(msg=false)
+ msg = 'An error has occured while trying to talk to Twitter. Please try again.' if msg.blank?
+ @error = msg
+ haml :fail and return
+ end
+
def get_user
@user = User.first(:id => session[:user])
end
@@ -46,7 +54,8 @@ def launch_retweet_hell
twitter_connect(@base_user)
unless @twitter_client.blank?
- info = @twitter_client.info
+ info = @twitter_client.info rescue nil
+
@tweet = "RT: @#{info['screen_name']}: %s #{configatron.twitter_hashtag}"
x = 142-@tweet.length
@@ -71,48 +80,61 @@ def launch_retweet_hell
@users = User.find_by_sql("SELECT id, account_id, screen_name, oauth_token, oauth_secret FROM users WHERE id != #{@base_user.id} ORDER BY RANDOM() LIMIT #{total}")#, :property => [ :id, :account_id, :screen_name, :oauth_token, :oauth_secret ])
@users.each do |user|
- twitter_connect(user)
- @twitter_client.update(@tweet)
+ begin
+ twitter_connect(user)
+ @twitter_client.update(@tweet)
+ rescue
+ twitter_fail('An error has occured while trying to post a retweet to Twitter. Please try again.')
+ end
end
-
- 'Finished.'
+
+ haml :run
else
- 'No tweets to tweet.'
+ @error = 'Could not load a tweet for this launch.'
+ haml :fail
end
-
end
-
-
-
end
get '/' do
get_user unless session[:user].blank?
unless @user.blank?
- "Thanks for signing up. There is nothing else you need to do. If you want to remove this, then goto your Twitter Settings > Connections, and remove it."
+ haml :thanks
else
- "Hello FFFFFattie! <a href=\"/connect\">Connect now!</a>"
+ haml :home
end
end
# Initiate the conversation with Twitter
get '/connect' do
+ @title = 'Connect to Twitter'
+
twitter_connect
- request_token = @twitter_client.request_token(:oauth_callback => 'http://localhost:4567/auth')
- session[:request_token] = request_token.token
- session[:request_token_secret] = request_token.secret
- redirect request_token.authorize_url.gsub('authorize', 'authenticate')
+ begin
+ request_token = @twitter_client.request_token(:oauth_callback => 'http://localhost:4567/auth')
+ session[:request_token] = request_token.token
+ session[:request_token_secret] = request_token.secret
+ redirect request_token.authorize_url.gsub('authorize', 'authenticate')
+ rescue
+ twitter_fail('An error has occured while trying to authenticate with Twitter. Please try again.')
+ end
end
# Callback URL to return to after talking with Twitter
get '/auth' do
+ @title = 'Authenticate with Twitter'
+
twitter_connect
@access_token = @twitter_client.authorize(session[:request_token], session[:request_token_secret], :oauth_verifier => params[:oauth_verifier])
if @twitter_client.authorized?
- info = @twitter_client.info
+ begin
+ info = @twitter_client.info
+ rescue
+ twitter_fail and return
+ end
@user = User.first_or_create(:account_id => info['id'])
@user.update_attributes(
@@ -128,18 +150,25 @@ def launch_retweet_hell
session[:request_token] = nil
session[:request_token_secret] = nil
- twitter_connect(@user)
- @twitter_client.update("#{twitter_sync_tweet} #{twiter_hashtag}")
+ begin
+ twitter_connect(@user)
+ @twitter_client.update("#{twitter_sync_tweet} #{twiter_hashtag}")
+ rescue
+ twitter_fail('An error has occured while trying to post a tweet to Twitter. Please try again.')
+ end
redirect '/'
else
redirect '/'
end
end
get '/run/*' do
+ @title = 'Launch Retweet Hell'
+
if params[:splat].to_s == configatron.secret_launch_code.to_s
launch_retweet_hell
else
- 'WTF!? You ain\'t got access to this. Fuck off.'
+ @error = 'WTF!? You ain\'t got access to this. Fuck off.'
+ haml :fail
end
end
View
4 views/fail.haml
@@ -0,0 +1,4 @@
+- unless @error.blank?
+ %p= @error
+- else
+ %p You've hit an undocumented error, sucka.
View
1 views/home.haml
@@ -0,0 +1 @@
+%p Hello FFFFFattie! <a href=\"/connect\">Connect now!</a>
View
13 views/layout.haml
@@ -1,2 +1,11 @@
-%h1= @title
-= yield
+!!! Strict
+%html{:xmlns=>'http://www.w3.org/1999/xhtml', :'xml:lang'=>'en', :lang=>'en'}
+ %head
+ %title= (!@title.blank? ? "#{@title} / " : '') + 'Re-tweet Fucker / A F.A.T. Lab Project'
+ %link{:rel=>'icon', :href=>'/favicon.ico'}
+ %link{:href=>'/stylesheets/screen.css', :media=>'all', :rel =>'stylesheet', :type=>'text/css'}
+
+ %body
+ - if @title
+ %h1= @title
+ = yield
View
8 views/run.haml
@@ -1,6 +1,6 @@
-%p To tweet: #{@tweet}
+%p Re-tweet: #{@tweet}
- unless @users.blank?
- - @users.each do |user|
- %p= user.screen_name
+ %p A retweet was sent out to the following users:
+ %p= @users.map{|u| u.screen_name}.join(', ')
- else
- %p No users.
+ %p No users were used to retweet.
View
1 views/thanks.haml
@@ -0,0 +1 @@
+%p Thanks for signing up. There is nothing else you need to do. If you want to remove this, then goto your Twitter Settings > Connections, and remove it.

0 comments on commit ff1ac34

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