Skip to content
This repository has been archived by the owner on Jan 15, 2022. It is now read-only.

Commit

Permalink
expand short urls - initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jm3 committed Nov 4, 2011
0 parents commit 4735954
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.textile
@@ -0,0 +1,5 @@
h1. expander - expand short URLs in strings

!http://f.cl.ly/items/2O1n2H401f2C213k0G1O/expander.jpg!

* Short URLs are annoying. Expand them.
11 changes: 11 additions & 0 deletions README.txt
@@ -0,0 +1,11 @@
jm3 ~/bin> gem install expander-0.0.1.gem
Successfully installed expander-0.0.1
1 gem installed
jm3 ~/bin> irb -rubygems
ruby version: ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-darwin10.8.0]
ruby-1.8.7-p352 :001 > require 'expander'
=> true
ruby-1.8.7-p352 :003 > s = "some stuff http://t.co/xCCP4e8R more stuff #hello"
=> "some stuff http://t.co/xCCP4e8R more stuff #hello"
ruby-1.8.7-p352 :004 > s.expand_urls
=> "some stuff http://guides.rubygems.org/make-your-own-gem/ more stuff #hello"
13 changes: 13 additions & 0 deletions expander.gemspec
@@ -0,0 +1,13 @@
Gem::Specification.new do |s|
s.name = 'expander'
s.version = '0.0.1'
s.date = '2011-11-03'
s.summary = "Expand urls!"
s.description = "A simple URL expanding gem"
s.authors = ["John Manoogian III (jm3)"]
s.email = 'jm3@140proof.com'
s.files = ["lib/expander.rb"]
s.homepage =
'http://rubygems.org/gems/expander'
end

40 changes: 40 additions & 0 deletions lib/expander.rb
@@ -0,0 +1,40 @@
require 'net/http'

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

def expand_urls
s = dup
s.expand_urls!
s
end

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

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

class String
include JM3::ExpandUrl
end

0 comments on commit 4735954

Please sign in to comment.