Skip to content

Instantly share code, notes, and snippets.

@bdwill
Created February 19, 2017 05:01
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 bdwill/48daa290c3f9a7288e75b86457526f9b to your computer and use it in GitHub Desktop.
Save bdwill/48daa290c3f9a7288e75b86457526f9b to your computer and use it in GitHub Desktop.
Ruby gem example for Twitter
# TwitterTrends.rb
# From http://www.alphadevx.com/a/88-Writing-a-REST-Client-in-Ruby
require 'rubygems'
require 'rest_client'
require 'json'
class TwitterTrends
# the URL for the Twitter Trends endpoint
@url
# constructor
def initialize
@url = 'http://api.twitter.com/1/trends.json'
end
# performs the GET request to get the trends from Twitter
def getTrends
response = RestClient.get(@url)
return response.body
end
# returns the raw JSON of the response from Twitter
def getJSON
return getTrends()
end
# returns a human-friendly text version of the response from Twitter
def getText
hashOfResponse = JSON.parse(getJSON())
textOfResponse = "Twitter Trends\n----------------\n\n"
textOfResponse += "Results for: "+hashOfResponse['as_of']+"\n\n"
# loop over the trends URLs returned and append them to the string to return
hashOfResponse['trends'].each { |trend|
textOfResponse += "Trend: "+trend['name']+", URL: "+trend['url']+"\n"
}
return textOfResponse
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment