Post to Twitter with Ruby

23. March 2012 • edited 26. May 2020

Just few hours ago I added the feature that new blog posts are posted to my Twitter account. With the twitter gem only a few lines of code necessary:

require 'twitter'

twitter = Twitter.new({
    :consumer_key       => 'YOUR_CONSUMER_KEY',
    :consumer_secret    => 'YOUR_CONSUMER_SECRET',
    :oauth_token        => 'YOUR_OAUTH_TOKEN',
    :oauth_token_secret => 'YOUR_OAUTH_TOKEN_SECRET'
})
twitter.update('Hello world!')

Shorten URIs with bitly gem is as simple as posting to twitter, too:

require 'bitly'

Bitly.use_api_version_3
bitly = Bitly.new('YOUR_USERNAME', 'YOUR_API_KEY')
short_url = bitly.shorten(longUrl).short_url
ProgrammingTwitterAPIRuby
Published under the THE BEER-WARE LICENSE.
If you like what I do you can subscribe my RSS feed or follow me on Twitter.

Debian and the Outdated Ruby Gems

Hudson vs. Jenkins