Das Weltraumschaf

Post to Twitter with Ruby

23. Mar 2012 | 1 minute read
Share this:

Post to Twitter with Ruby

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 = Bitly.new('YOUR_USERNAME', 'YOUR_API_KEY')
short_url = bitly.shorten(longUrl).short_url