Module: TinyUrl

Defined in:
lib/tiny_url.rb

Class Method Summary collapse

Class Method Details

taken from rails



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
37
38
39
40
41
42
43
# File 'lib/tiny_url.rb', line 11

def auto_link_urls(text)
  auto_link_re = %r{
                          (                          # leading text
                            <\w+.*?>|                # leading HTML tag, or
                            [^=!:'"/]|               # leading punctuation, or
                            ^                        # beginning of line
                          )
                          (
                            (?:https?://)|           # protocol spec, or
                            (?:www\.)                # www.*
                          )
                          (
                            [-\w]+                   # subdomain or domain
                            (?:\.[-\w]+)*            # remaining subdomains or domain
                            (?::\d+)?                # port
                            (?:/(?:[~\w\+@%=\(\)-]|(?:[,.;:'][^\s$]))*)* # path
                            (?:\?[\w\+@%&=.;-]+)?     # query string
                            (?:\#[\w\-]*)?           # trailing anchor
                          )
                          ([[:punct:]]|<|$|)       # trailing text
                         }x 

  text.gsub(auto_link_re) do
    all, a, b, c, d = $&, $1, $2, $3, $4
    if a =~ /<a\s/i # don't replace URL's that are already linked
      all
    else
      text = b + c
      url = create("http://#{c}")
      %(#{a}#{url})
    end
  end
end

.create(url) ⇒ Object



4
5
6
7
8
# File 'lib/tiny_url.rb', line 4

def create(url)
  uri = 'http://tinyurl.com/api-create.php?url=' + url
  uri = URI.parse(uri)
  tiny_url = Net::HTTP.get_response(uri).body
end