Sending Tweets from your Rails app
Since I'm on the road, I'll have to make this short.
- Use the Twitter4R gem. I tried using Twitter, but could never install it properly in the vendor directory - probably my own gem naivete, though, YMMV.
- Like Chris Wanstrath says - "vendor everything". Unpack it into your vendor/gem directory. If the gem is already installed, cd to vendor/gem and use this command:
gem unpack twitter4r
- Add Chris' code to the Rails::Initializer.run block in your config/environment.rb:
config.load_paths += Dir["#{RAILS_ROOT}/vendor/gems/**"].map do dir File.directory?(lib = "#{dir}/lib") ? lib : dir end - Add this line of code under the Rails::Initializer.run block in your config/environment.rb:
[Ok, I forgot the exact syntax of this line, but it tells Rails 2.1 that the "twitter" gem is found in "twitter4r.rb"]
- Using the following code snippet, you should be able to send a tweet. However, on the Twitter page, that tweet will say it's coming from Twitter4R. To change that, you'll have to register your app with Twitter (more details to come). And add a init file that alters the Twitter "source" token that Twitter sends you once you register. Don't bother grepping for Twitter4R and changing URLs randomly. It won't work. I promise.
The following code snippets are off the top of my head and may not be absolutely correct.
Authorizing
#return whether username/password is a valid Twitter account def twitter_auth(username,password) c = Twitter::Client.new c.authorize(username, password) end
Sending a tweet
# Send a tweet from username/password. Throws exception if username/password
# is not valid
def send_tweet(username, password, tweet)
c = Twitter::Client.new(:login => username, :password => password)
c.status(:post, tweet)
end
