Created
April 4, 2013 16:36
-
-
Save jagira/5311919 to your computer and use it in GitHub Desktop.
Twitter Fetcher
This file contains hidden or 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 BrowseController < ApplicationController | |
# params - username, count | |
def tweets | |
@tweets = Twitter.fetch_tweets(params[:username], params[:count]) | |
render json: @tweets | |
end | |
end | |
This file contains hidden or 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 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