This is my simple, Ruby based Twitter client using Curl designed for UNIX systems like Linux, Mac OSX, FreeBSD, Solaris, etc. The only requirements are Curl and Ruby.
In order to use Tweet, simply copy all of the included code into your favourite text editor (I use vi) and save as ‘Tweet’. Don’t forget to “chmod a+x tweet” so that it is executable. I suggest moving Tweet into your path (perhaps you should consider /usr/local/bin as a recommended directory) to make it easier to use. I have designed Tweet to be useful to users on a multi-user UNIX system. It is a command-line utility that simply accepts text input and posts that text, maximum of 144 characters, to your Twitter account. An existing Twitter account is necessary so sign up if you do not have one already.
There is very little to know in order to use Tweet [Ruby]. (Should I name this RTweet perhaps?) The one thing that is needed is to set your username and password. Tweet [Ruby] is designed to accept username and password data from the system environmental variables $tweetuser and $tweetpass. This design decision was made because it makes it extremely simple to have multiple users on the same system be able to use Tweet [Ruby] transparently from one another. If you desire, you can bypass this setting by changing the “unset” user and pass settings in the code to your username and password. This hardcoding is not recommended but is available if needed.
Once you have your username and password set (you can see what your settings currently are by using the -t option) all you need to do is enter the text that you want to publish. Here is an example:
tweet ‘This is my first post from Tweet [Ruby]. Thanks Scott, this is great.’
Here is the code, go crazy.
#!/usr/bin/ruby -w #Scott Alan Miller's "Tweet" - Twitter Command Line Script text = ARGV[0].chomp user = "unset" #Supplied Username pass = "unset" #Supplied Password url = "http://twitter.com/statuses/update.xml" ver = "1.0" user = ENV['tweetuser'] if ENV['tweetuser'] pass = ENV['tweetpass'] if ENV['tweetpass'] if text.length <= 0 puts "Please enter text to post." elsif text.length >= 144 puts "Please limit post to 144 chars." elsif text == "-v" # Version Message puts "Current Version of Tweet [Ruby] is " + ver elsif text == "-h" # Help Message puts "Tweet [Ruby] Help: \n" puts "To set environmental username and password:" puts " export tweetuser=yourusername" puts " export tweetpass=yourpassword\n" puts "Usage:" puts " tweet \'This is my message.\'" elsif text == "-t" # Variable Test puts "Username: " + user puts "Password: " + pass else result = %x[curl -s -S -u #{user}:#{pass} -d status="#{text}" #{url}] puts "Update Failure" if result.grep(/text/) == nil end
If you end up using my little Twitter client, please send me a Tweet to let me know!
tweet ‘@scottalanmiller Using Tweet, best Twitter client ever. Ruby rulz.’
Hi
Line 4 chomp gives error if no arg passed, so won’t reach later line checking text length <=0 .
I’ll probably put in a read there to take input if no arg provided.
Nice script.
rahul
One of 2 tiny changes, if you don’t mind:
text = ARGV[0] && ARGV[0].chomp
if text.nil? or text.length <= 0
puts “Please enter text to post.”
text = ARGF.readline
end
thanks!
Nice article.
Regarding the username and password hardcoding in the script. It isn’t that hard to use OAuth so that you don’t have to leave your Twitter username and password in a script or in environmental variables.
I go over tweeting in Ruby with OAuth in my blog article: http://joshuaborn.info/blog/dc7a