This repository has been archived by the owner on Jan 15, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4735954
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |