Module: JM3::ExpandUrl

Included in:
String
Defined in:
lib/expander.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.expand(path, host) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/expander.rb', line 28

def ExpandUrl.expand(path, host)
  result = ::Net::HTTP.new(host).head(path)
  case result
  when ::Net::HTTPRedirection
    result['Location']
  end
end

.servicesObject



19
20
21
22
23
24
25
26
# File 'lib/expander.rb', line 19

def ExpandUrl.services
  [
    { :host => "tinyurl.com", :pattern => %r'(http://tinyurl\.com(/[\w/]+))' },
    { :host => "is.gd",   :pattern => %r'(http://is\.gd(/[\w/]+))' },
    { :host => "bit.ly",  :pattern => %r'(http://bit\.ly(/[\w/]+))' },
    { :host => "t.co",    :pattern => %r'(http://t\.co(/[\w/]+))' },
  ]
end

Instance Method Details

#expand_urlsObject



13
14
15
16
17
# File 'lib/expander.rb', line 13

def expand_urls
  s = dup
  s.expand_urls!
  s
end

#expand_urls!Object



5
6
7
8
9
10
11
# File 'lib/expander.rb', line 5

def expand_urls!
  ExpandUrl.services.each do |service|
    gsub!(service[:pattern]) { |match|
      ExpandUrl.expand($2, service[:host]) || $1
    }
  end
end