TinyUrl class

Hi everyone,

I wrote a small TinyUrl class today. Very simple, probably not very
stable, but it does the job, so I'll post it here in case someone needs
something like that.

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

As you can see, not terribly robust. I might see if I can get
something more stable. In any case, I hope it can help some of you.

Cheers,

Vincent.

Vincent Foley wrote:

Hi everyone,

I wrote a small TinyUrl class today. Very simple, probably not very
stable, but it does the job, so I'll post it here in case someone needs
something like that.

class TinyUrl
  def initialize(url)
    @url = url
  end

  def shorten
    Net::HTTP.start("tinyurl.com", 80) { |http|

Um, I'm pretty sure that should be
      Net::HTTP.start("www.rubyurl.com", 80) { |http|

:slight_smile:

James

···

--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys

Heh, indeed :slight_smile: Could have both...

How's this?

require "net/http"
require "cgi"

class ShortURL
  def self.rubyurl(url)
    Net::HTTP.start("rubyurl.com", 80) { |http|
      response =
http.get("/rubyurl/create?rubyurl[website_url]=#{CGI.escape(url)}")

      if response.code == "302"
        body = response.read_body
        regex = /<a href="(.+)">/
        return regex.match(body)[1]
      end
    }
  end

  def self.tinyurl(url)
    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

Noob, here, just playing around with Ruby (if this is spam, please let me know). For the heck of it, I decided to refactor the duplication out of the ShortURL class. Just to spite me, the resulting code is actually bigger, but here you go...

require "net/http"
require "cgi"

class ShortURL
  def self.web(server, action, code)
    Net::HTTP.start(server, 80) { |http|
      response = http.instance_eval action
      if response.code == code
        yield response.read_body
      end
    }
  end

  def self.rubyurl(url)
    ret = nil
    web("rubyurl.com",'get("/rubyurl/create?rubyurl[website_url]='+CGI.escape(url)+'")',"302") { |body|
        regex = /<a href="(.+)">/
        ret = regex.match(body)[1]
    }
    ret
  end

  def self.tinyurl(url)
    ret = nil
    web("tinyurl.com",'post("/create.php", "url='+url+'")',"200") { |body|
        line = body.split("\n").find { |l| l =~ /hidden name=tinyurl/ }
        i1 = line.index("http")
        i2 = line.rindex("\"")
        ret = line[i1...i2]
    }
    ret
  end
end

Devin

Vincent Foley wrote:

···

How's this?

require "net/http"
require "cgi"

class ShortURL
def self.rubyurl(url)
   Net::HTTP.start("rubyurl.com", 80) { |http|
     response =
http.get("/rubyurl/create?rubyurl[website_url]=#{CGI.escape(url)}")

     if response.code == "302"
       body = response.read_body
       regex = /<a href="(.+)">/
       return regex.match(body)[1]
     end
   }
end

def self.tinyurl(url)
   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

Don't let that get you down! Your version is more DRY (Don't Repeat Yourself). That's important because it frees you of the burden of finding all the right spots to change in replicated code come maintenance time. I would rather have that than win a golf match (shortest keystrokes) any day.

Nice work. :wink:

James Edward Gray II

···

On Jun 1, 2005, at 7:39 PM, Devin Mullins wrote:

Noob, here, just playing around with Ruby (if this is spam, please let me know). For the heck of it, I decided to refactor the duplication out of the ShortURL class. Just to spite me, the resulting code is actually bigger, but here you go...

Hi,

here is a shorter version :wink:

require "net/http"
require "cgi"

class ShortURL
    def self.web(server, action, code)
       Net::HTTP.start(server, 80) { |http|
          response = http.instance_eval action
          response.code == code ? yield(response.read_body) : nil
       }
    end

    def self.rubyurl(url)
       web("rubyurl.com",'get("/rubyurl/create?rubyurl[website_url]='+CGI.escape(url)+'")',"302") { |body|
          /<a href="(.+)">/.match(body)[1]
       }
    end

    def self.tinyurl(url)
       web("tinyurl.com",'post("/create.php", "url='+url+'")',"200") { |body|
          /hidden name=tinyurl value="(.+)"/.match(body)[1]
       }
    end
end

Dominik

···

On Thu, 02 Jun 2005 02:39:34 +0200, Devin Mullins <twifkak@comcast.net> wrote:

Noob, here, just playing around with Ruby (if this is spam, please let me know). For the heck of it, I decided to refactor the duplication out of the ShortURL class. Just to spite me, the resulting code is actually bigger, but here you go...

require "net/http"
require "cgi"

class ShortURL
  def self.web(server, action, code)
    Net::HTTP.start(server, 80) { |http|
      response = http.instance_eval action
      if response.code == code
        yield response.read_body
      end
    }
  end

  def self.rubyurl(url)
    ret = nil
    web("rubyurl.com",'get("/rubyurl/create?rubyurl[website_url]='+CGI.escape(url)+'")',"302") { |body|
        regex = /<a href="(.+)">/
        ret = regex.match(body)[1]
    }
    ret
  end

  def self.tinyurl(url)
    ret = nil
    web("tinyurl.com",'post("/create.php", "url='+url+'")',"200") { |body|
        line = body.split("\n").find { |l| l =~ /hidden name=tinyurl/ }
        i1 = line.index("http")
        i2 = line.rindex("\"")
        ret = line[i1...i2]
    }
    ret
  end
end

Devin