[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

TinyUrl class

Vincent Foley

6/1/2005 2:53:00 AM

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.

6 Answers

james_b

6/1/2005 4:43:00 AM

0

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|


:)


James

--

http://www.ru... - The Ruby Documentation Site
http://www.r... - News, Articles, and Listings for Ruby & XML
http://www.rub... - The Ruby Store for Ruby Stuff
http://www.jame... - Playing with Better Toys


Vincent Foley

6/1/2005 5:27:00 AM

0

Heh, indeed :) Could have both...

Vincent Foley

6/1/2005 6:18:00 AM

0

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

Devin Mullins

6/2/2005 12:40:00 AM

0

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


James Gray

6/2/2005 2:18:00 AM

0

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

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. ;)

James Edward Gray II



Dominik Bathon

6/2/2005 3:37:00 PM

0

Hi,

here is a shorter version ;-)

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