Skip to content

Instantly share code, notes, and snippets.

@jagira
Created April 4, 2013 16:36
Show Gist options
  • Save jagira/5311919 to your computer and use it in GitHub Desktop.
Save jagira/5311919 to your computer and use it in GitHub Desktop.
Twitter Fetcher
class BrowseController < ApplicationController
# params - username, count
def tweets
@tweets = Twitter.fetch_tweets(params[:username], params[:count])
render json: @tweets
end
end
class Twitter
# could have implemented better error messages
def self.fetch_tweets(username, count = 10)
return {status: "Error!", message: "Invalid username"} unless username
begin
api_url = "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=#{username}&#count=#{count}"
tweet_objects = JSON.parse(open(api_url).read)
tweet_objects.collect {|t| {id: t['id'], tweet_text: t['text']} }
rescue
return {status: "Error!", message: "Exception message"}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment