Skip to content

Instantly share code, notes, and snippets.

Created June 19, 2013 23:45
Show Gist options
  • Select an option

  • Save anonymous/5819146 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/5819146 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Jun 19, 2013.
    34 changes: 34 additions & 0 deletions application.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    $(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);
    });
    }
    };
    43 changes: 43 additions & 0 deletions index.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    $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

    10 changes: 10 additions & 0 deletions tweet_worker.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    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
    18 changes: 18 additions & 0 deletions user.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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