Class ShortURL
In: lib/shorturl.rb
Parent: Object

Methods

get_short_url   metamark   rubyurl   shorl   shorten   snipurl   tinyurl  

Public Class methods

Main method of ShortURL, its usage is quite simple, just give a url to shorten and an optional service. If no service is selected, RubyURL.com will be used. An invalid service symbol will raise an ArgumentError exception

Valid service values:

:rubyurl:uses RubyURL.com
:tinyurl:uses TinyURL.com

call-seq:

  ShortURL.shorten("http://mypage.com") => Uses RubyURL
  ShortURL.shorten("http://mypage.com", :tinyurl)

[Source]

    # File lib/shorturl.rb, line 26
26:   def self.shorten(url, service = :rubyurl)
27:     case service
28:     when :rubyurl then rubyurl(url)
29:     when :tinyurl then tinyurl(url)
30:     when :shorl then shorl(url)
31:     when :snipurl then snipurl(url)
32:     when :metamark then metamark(url)
33:     else raise ArgumentError, "Invalid service"
34:     end
35:   end

Private Class methods

Abstract method to shorten a URL. Respects the DRY principle. In case of a network-related exception, return nil.

Parameters:

  hostname: hostname of the service
  code: expected return code (200, 302, etc.)  Converted to a string
  action: string representing the action to be executed on the HTTP object with +instance_eval+
  block: block of code which scrapes the HTML and returns the shortened URL.  Takes one parameter.

[Source]

    # File lib/shorturl.rb, line 45
45:   def self.get_short_url(hostname, code, action, &block) # :yields: html_body
46:     begin
47:       Net::HTTP.start(hostname, 80) { |http|
48:         response = http.instance_eval(action)
49:         
50:         if response.code == code.to_s
51:           block.call(response.read_body)
52:         end
53:       }
54:     rescue SocketError, Net::HTTPExceptions => e
55:       return nil
56:     end
57:   end

Shorten a URL with metamark.com. Same technique as TinyURL

[Source]

     # File lib/shorturl.rb, line 107
107:   def self.metamark(url)
108:     get_short_url("metamark.net", 200,
109:                   "post('/add', 'long_url=#{url}')") { |body|
110:       line = body.split("\n").find { |l| l =~ /xrl\.us/ }
111:       short_url = URI.extract(line)[0]
112:       return short_url
113:     }
114:   end

Shortens an URL with RubyURL.com. Since RubyURL uses a redirection (code 302), instead of using HTTP#post, we send our data directly to the create action. The response is a small HTML line which contains our shortened URL plus /rubyurl/show, so we manually remove that with gsub.

[Source]

    # File lib/shorturl.rb, line 65
65:   def self.rubyurl(url)
66:     get_short_url("rubyurl.com", 302,
67:                   "get('/rubyurl/create?rubyurl[website_url]=#{CGI.escape(url)}')") { |body|
68:       short_url = URI.extract(body)[0].gsub("rubyurl/show/", "")
69:       return short_url
70:     }
71:   end

Shorten a URL through with shorl.com. Same technique as TinyURL

[Source]

    # File lib/shorturl.rb, line 88
88:   def self.shorl(url)
89:     get_short_url("shorl.com", 200,
90:                   "post('/create.php', 'url=#{url}')") { |body|
91:       short_url = URI.extract(body)[2]
92:       return short_url
93:     }
94:   end

Shorten a URL with snipurl.com. Same technique as TinyURL

[Source]

     # File lib/shorturl.rb, line 97
 97:   def self.snipurl(url)
 98:     get_short_url("snipurl.com", 200,
 99:                   "post('/index.php', 'link=#{url}')") { |body|
100:       line = body.split("\n").grep(/txt/)[0]
101:       short_url = URI.extract(line)[1][0..-2] # Remove trailing '
102:       return short_url
103:     }
104:   end

Shortens an URL with TinyURL.com. We post a request to the create.php action with our URL as a parameter. We then read the HTML code, split it into an array, find an element that contains our shortened URL and extract it.

[Source]

    # File lib/shorturl.rb, line 78
78:   def self.tinyurl(url)
79:     get_short_url("tinyurl.com", 200,
80:                   "post('/create.php', 'url=#{url}')") { |body|
81:       line = body.split("\n").find { |l| l =~ /hidden name=tinyurl/ }
82:       short_url = URI.extract(line)[0]
83:       return short_url
84:     }
85:   end

[Validate]