TinyUrl class

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

James Britt

Vincent said:
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.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
 
V

Vincent Foley

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
 
D

Devin Mullins

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
 
J

James Edward Gray II

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
 
D

Dominik Bathon

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top