The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
The evolution of software

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Vincent Foley

Posts: 512
Nickname: gnuvince
Registered: Apr, 2005

Vincent Foley is a hobbyist Rubyist.
The evolution of software Posted: Jun 7, 2005 3:41 PM
Reply to this message Reply

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
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Vincent Foley
Latest Posts From Vincent Foley-Bourgon

Advertisement
Hi,

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..."

Read: The evolution of software

Topic: IE is teh 3v1l Previous Topic   Next Topic Topic: ZenTest and unit_diff

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use