Created
June 19, 2013 23:45
-
-
Save anonymous/5819146 to your computer and use it in GitHub Desktop.
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
$(document).ready(function() { | |
$('#tweet-form').submit(function(event) { | |
event.preventDefault(); | |
var $form = $('#tweet-form'); | |
var data = $form.serialize(); | |
var url = $form.attr('action'); | |
// var jobDone = "0" | |
$.post(url, data, function(jobId) { | |
$.get('/status/' + jobId, function(complete){ | |
poll(complete, jobId); | |
}); | |
}); | |
}); | |
}); | |
function poll(complete, jobId){ | |
if(complete === "true") { | |
if(timeout) clearTimeout(timeout); | |
$('#working').css("visibility", "hidden"); | |
$('#complete').css("visibility", "visible"); | |
} else { | |
$('#working').css("visibility", "visible"); | |
$.get('/status/' + jobId, function(complete){ | |
timeout = setTimeout(function(){ | |
poll(complete, jobId); | |
}, 750); | |
}); | |
} | |
}; |
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
$redis = Redis.connect | |
get '/' do | |
erb :index | |
end | |
get '/sign_in' do | |
# the `request_token` method is defined in `app/helpers/oauth.rb` | |
redirect request_token.authorize_url | |
end | |
get '/sign_out' do | |
session.clear | |
redirect '/' | |
end | |
get '/auth' do | |
# the `request_token` method is defined in `app/helpers/oauth.rb` | |
@access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier]) | |
# our request token is only valid until we use it to get an access token, so let's delete it from our session | |
session.delete(:request_token) | |
# at this point in the code is where you'll need to create your user account and store the access token | |
username = @access_token.params[:screen_name] | |
token = @access_token.params[:oauth_token] | |
secret = @access_token.params[:oauth_token_secret] | |
user = User.find_or_create_by_username(username: username, | |
oauth_token: token, | |
oauth_secret: secret) | |
session[:user] = user.id | |
redirect to '/' | |
end | |
post '/submit_tweet' do | |
current_user.tweet(params[:text], params[:schedule]) | |
end | |
get '/status/:job_id' do | |
job_is_complete(params[:job_id]).to_json | |
end | |
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
class TweetWorker | |
include Sidekiq::Worker | |
def perform(tweet_id) | |
tweet = Tweet.find(tweet_id) | |
user = tweet.user | |
user.twitter_client.update(tweet.text) | |
end | |
end |
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
class User < ActiveRecord::Base | |
has_many :tweets | |
validates :oauth_token, :uniqueness => :true | |
def tweet(text, schedule) | |
tweet = tweets.create!(:text => text, :schedule => schedule) | |
TweetWorker.perform_in(tweet.schedule, tweet.id) | |
end | |
def twitter_client | |
@twitter_client ||= Twitter::Client.new( | |
:oauth_token => self.oauth_token, | |
:oauth_token_secret => self.oauth_secret | |
) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment