Skip to content

Instantly share code, notes, and snippets.

@dt1973
Last active August 29, 2015 14:25
Show Gist options
  • Save dt1973/ad05c8b7697dfe8956c5 to your computer and use it in GitHub Desktop.
Save dt1973/ad05c8b7697dfe8956c5 to your computer and use it in GitHub Desktop.

sferik/twitter demo app in Rails

  • Display tweets from a selection of users
  • Display all tweets in and around Chicago, IL
  • Collect tweets in the background using delayed_job_active_record or similar
  • Save tweets to db (helps avoid rate limits)
  • Query each stream separately and then mix them together manually in the model (e.g. ordered chronologically)

Live app which you can run on the fly: http://code.runnable.com/Vao93VmSdpgdQ0lO/twitter-feed-rails


Gemfile

gem "twitter"
gem "delayed_job"
gem "delayed_job_active_record"

config/initializers/twitter.rb

client = Twitter::REST::Client.new do |config|
  # I know these values shouldn't be public, but it's just a throwaway account :-)
  config.consumer_key        = "oQM83XT6kIcaBoVM1wBmP0bib"
  config.consumer_secret     = "udVq8Vx603JW3fmxoxwbleaRcAObUDmSPAz5rOnuwzVVGp9t4t"
  config.access_token        = "3355714197-wO1FXugi6jX8i9vhWYzG1MnpjuaE3xH1k575ruA"
  config.access_token_secret = "xcqv0ErFRAPPmDij5D30BI4sSsT9iHiGcfxLWtiA2cKOI"
end

app/views/main/index.html.erb

<h1><a href="https://github.com/sferik/twitter">sferik/twitter</a> demo app</h1>
<% client.get_all_tweets("sferik").each do |tweet| %>
  <%= tweet.username %>
  <%= tweet.time %>
  <%= tweet.body %>
<% end %>

app/models/main.rb

class Main < ActiveRecord::Base
  def fetch_users
  end
  
  def fetch_location
  end
end

app/controllers/main_controller.rb

class MainController < ApplicationController
  def index
  end
end

db/migrate/20150722120901_create_tweets.rb

...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment