Skip to content

Instantly share code, notes, and snippets.

@SabretWoW
Last active December 21, 2015 14:58
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 SabretWoW/6323026 to your computer and use it in GitHub Desktop.
Save SabretWoW/6323026 to your computer and use it in GitHub Desktop.
A Ruby on Rails controller action showing how to fetch a group of Tweets/followers/friends & send them to a view. You can also create Tweets with this controller & the Twitter API, but you'll need to ensure your callback URL is correct. Follow the documentation for that.
class TweetsController < ApplicationController
before_action :create_client
def index
batch_size = 10
@twitter_handle = "dhh"
@tweets = @client.user_timeline(@twitter_handle).take(batch_size)
@friends = @client.friends(@twitter_handle).take(batch_size)
@followers = @client.followers(@twitter_handle).take(batch_size)
# You can only get mentions for the authenticated user, not any handle you pass to the client.
# @mentions = @client.mentions_timeline.take(batch_size)
end
def new
# Make sure to set your Twitter app's callback URL to:
# http://127.0.0.1:3000/auth/twitter/callback
# puts @client.update("I'm tweeting with my app!").inspect
render 'index'
end
# This is a sample of how you'd search for tweets via a full API call URL
# Retrieve latest tweet for @dhh
# Go to OAuth tool in your app settings
# Do a GET request to the following:
# https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=dhh&count=1
# (API docs - https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline)
# Copy the cURL signature
# Paste into your command line
# Copy the big response
# Paste in to http://jsonviewer.net/index.php
# Check it out
private
def create_client
@client = Twitter::REST::Client.new do |config|
config.consumer_key = " "
config.consumer_secret = " "
config.access_token = " "
config.access_token_secret = " "
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment