My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Author:: Brendan G. Lim (brendangl@gmail.com)

module ActsAsTinyURL

class ActsAsTinyURLError < StandardError; end;

def tiny_url(uri, options = {})
defaults = { :validate_uri => false }
options = defaults.merge options
return validate_uri(uri) if options[:validate_uri]
return generate_uri(uri)
end

private

def validate_uri(uri)
confirmed_uri = uri[/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix]
if confirmed_uri.blank?
return false
else
return true
end
end

def generate_uri(uri)
confirmed_uri = uri[/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix]
if !confirmed_uri.blank?
escaped_uri = URI.escape("http://tinyurl.com/api-create.php?url=#{confirmed_uri}")
uri_parsed = Net::HTTP.get_response(URI.parse(escaped_uri)).body
return uri_parsed
else
raise ActsAsTinyURLError.new("Provided URL is incorrectly formatted.")
end
end

end

Change log

r7 by brendangl on Jan 17, 2008   Diff
Adding in version 0.1.1 - now more of a
helper
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 1026 bytes, 36 lines
Powered by Google Project Hosting