This post originated from an RSS feed registered with Ruby Buzz
by Vincent Foley.
Original Post: The evolution of software
Feed Title: Vincent Foley-Bourgon
Feed URL: http://www.livejournal.com/~gnuvince/data/rss
Feed Description: Vincent Foley-Bourgon - LiveJournal.com
as I am writing my first sizeable contribution to Ruby, ShortURL, I realized that something I had read in an essay by Paul Graham had actually happened to me. I am only paraphrasing (can't find the exact quote), but it said that most programs start life as scripts. And indeed, that's what happened to me.
My friend Joel (the annoying anonymous commentor) was whining on IRC that the links I was pasting were too long, and said I should run them through TinyURL. Since I didn't feel like going over to www.tinyurl.com every time I wanted to give him an URL, I decided to write a quick script for that purpose. This is the result:
#!/usr/local/bin/ruby
require "net/http"
class TinyUrl
def initialize(url)
@url = url
end
def shorten
Net::HTTP.start("tinyurl.com", 80) { |http|
response = http.post("/create.php", "url=#{@url}")
if response.code == "200"
body = response.read_body
line = body.split("\n").find { |l| l =~ /hidden name=tinyurl/ }
i1 = line.index("http")
i2 = line.rindex("\"")
return line[i1...i2]
end
}
end
end
def main
if ARGV[0]
url = ARGV[0]
t = TinyUrl.new(url)
puts t.shorten
else
puts "Usage: #$0 "
end
end
if $0 == __FILE__
main
end
And then I made an alias in Irssi, so that when I typed /tinyurl , this script would be ran and the result would be put into the channel. I thought it was pretty useful, so I decided to post it to ruby-talk. James Brit suggested that I use RubyURL.com instead of TinyURL.com. So I added support for RubyURL.
With two different services, I decided to create a more genereal ShortURL class. The first versions were really hacky, but as release went on, I made things clearer and clearer by refactoring the program. You can follow the progress I made by downloading all the ShortURL gems and seeing how lib/shorturl.rb changed over time.
Now, I just released ShortURL 0.4.0. It has support for 10 different URL shortening services, the code-to-test ratio it 1:0.7, the Service class makes adding a service easy and nice. I use many cool features of Ruby, such as closures, the send message. I also had instance_eval in previous versions, but this code has been advantageously replaced with the current code base.
So, I guess that it is true, programs often start from little scripts. "The little script that could..."