V
Vincent Foley
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.
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.