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.
In modern computer systems, privacy and security are mandatory. However, connections from the outside over public networks automatically imply risks. One easily available solution to avoid eavesdroppers’ attempts is SSH. But, its wide adoption during the past 21 years has made it a target for attackers, so hardening your system properly is a must.
Additionally, in highly regulated markets, you must comply with specific operational requirements, proving that you conform to standards and even that you have included new mandatory authentication methods, such as two-factor authentication. In this ebook, I discuss SSH and how to configure and manage it to guarantee that your network is safe, your data is secure and that you comply with relevant regulations.Get the Guide
- Server Hardening
- EnterpriseDB's EDB Postgres Advanced Server and EDB Postgres Enterprise Manager
- The Death of RoboVM
- BitTorrent Inc.'s Sync
- The Humble Hacker?
- The US Government and Open-Source Software
- Open-Source Project Secretly Funded by CIA
- New Container Image Standard Promises More Portable Apps
- Canonical and BQ's Aquaris M10 Ubuntu Edition Tablet
- ACI Worldwide's UP Retail Payments