Talking to Twitter
"Twitter" Gem for Ruby
Readers of this column know that I love the Ruby language, so it won't come as a surprise to hear that I intend to use Ruby for my examples. However, there are Twitter API clients in virtually every modern language, making it easy to access from whatever you prefer to use in your programming.
The twitter Ruby gem, as is the case for all Ruby gems (libraries), is available for installation via the gem program, which comes with modern versions of Ruby. The gem currently is maintained by Erik Michaels-Ober, also known as "sferik" on GitHub. You can type:
gem install twitter -V
and the gem should be installed. On many systems, including those not running a Ruby version manager like rvm, you need to execute the above line while logged in as root.
Once you have installed the gem, you can use it. There are three
parts to this process: bringing the gem into the program, configuring
it to use your keys and secrets, and then executing a Twitter command.
The first is handled with the Ruby
require command, which looks at
installed gems, as well as the Ruby core and standard libraries.
Configuration of the client is handled fairly straightforwardly from within a block that looks like this (filling in the values you got from Twitter's API documentation):
twitter_client = Twitter::REST::Client.new do |config| config.consumer_key = CONSUMER_KEY config.consumer_secret = CONSUMER_SECRET config.oauth_token = OAUTH_TOKEN config.oauth_token_secret = OAUTH_SECRET end
Notice that you are not merely executing the "new" method on
Twitter::REST::Client, but that you also are returning a value. Thus,
in contrast to previous versions of Ruby's Twitter gem, you should
accept the returned object, which is then the basis for all of the
additional actions you wish to take.
Finally, you send the tweet with the "update" method:
tweet = twitter_client.update("Hello, world. Tweet tweet.")
Invoking the #update method has the effect of sending the message to Twitter. If you go to the Web page for your Twitter user, you'll find that a new message has been sent, as if you had typed it.
If you capture the return value from the invocation of
twitter_client.update, you'll see that it is an instance of
Twitter::Tweet, a Ruby object that represents a tweet. This object
provides the functionality that you would want and expect from
something associated from Twitter. For example:
tweet.user # tells us who wrote the tweet tweet.retweeted? # indicates whether it was retweeted tweet.favorited? # indicates whether it was marked as a favorite
Now, it's also possible that you will not get a tweet object back at all, but rather that the "update" method will raise an exception. For example, Twitter forbids users from sending an identical tweet, at least within a short period of time. Thus, if you send the above "Hello, world" tweet (from the example above) a second time, you'll get an exception:
Twitter::Error::Forbidden: Status is a duplicate.
Of course, you can catch such errors with:
begin tweet = twitter_client.update("Hello again, ↪@reuvenmlerner Tweet tweet.") rescue Twitter::Error::Forbidden => e puts "You already tweeted that." rescue => e puts e.class # Twitter::Error::Forbidden puts e.message # 'Status is a duplicate.' end
If you include a Twitter @username, hashtag or URL in your tweet, the appropriate magic will happen automatically. Thus:
tweet = twitter_client.update("Go to @reuvenmlerner's ↪site at http://lerner.co.il/")
In the above tweet, the URL automatically will be shortened, using Twitter's standard t.co domain. Similarly, the @reuvenmlerner (my Twitter handle) will turn into a link. You can access both of these using methods on your tweet:
tweet.urls # returns an array of Twitter::Entity::URI tweet.user_mentions # returns an array of # Twitter::Entity::UserMention
You can more generally ask Twitter for information about tweets. For example, you can get the most recent tweets a user has sent with:
which returns an array of tweet objects. You can apply the "text" method to the first element, thus getting the text back from the user's most recent tweet:
If there are URLs embedded in the tweet, you can get those back:
This method returns an array of
Twitter::Entity::URI objects, each of
which has attributes, such as "url" and "expanded
Reuven M. Lerner, Linux Journal Senior Columnist, a longtime Web developer, consultant and trainer, is completing his PhD in learning sciences at Northwestern University.
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide
|Nativ Disc||Sep 23, 2016|
|Android Browser Security--What You Haven't Been Told||Sep 22, 2016|
|The Many Paths to a Solution||Sep 21, 2016|
|Synopsys' Coverity||Sep 20, 2016|
|Naztech's Roadstar 5 Car Charger||Sep 16, 2016|
|RPi-Powered pi-topCEED Makes the Case as a Low-Cost Modular Learning Desktop||Sep 15, 2016|
- Android Browser Security--What You Haven't Been Told
- Nativ Disc
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- The Many Paths to a Solution
- Naztech's Roadstar 5 Car Charger
- Synopsys' Coverity
- Securing the Programmer
- RPi-Powered pi-topCEED Makes the Case as a Low-Cost Modular Learning Desktop
- NordVPN for Android
- Glass Padding