Created
February 19, 2017 05:01
-
-
Save bdwill/48daa290c3f9a7288e75b86457526f9b to your computer and use it in GitHub Desktop.
Ruby gem example for Twitter
This file contains 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
# 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