Skip to content

Instantly share code, notes, and snippets.

@siyo
Created October 15, 2011 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siyo/1290324 to your computer and use it in GitHub Desktop.
Save siyo/1290324 to your computer and use it in GitHub Desktop.
無理矢理短縮URL展開 earthquake.gem plugin
# -*- coding: utf-8 -*-
#
Earthquake.init do
once do
SHORT_URL_REGEXP = %r|https?://\w+\.\w+/\w+[^\W]|
end
output_filter do |item|
next if item.nil? || item["text"].nil? || (urls = item["text"].scan(SHORT_URL_REGEXP)).empty?
f = lambda{|url|
uri = URI(url)
Net::HTTP.start(uri.host, uri.port) {|http|
http.open_timeout = 3
http.head(uri.request_uri)["Location"] || url
}
}
urls.each{|url|
tmp = url
while SHORT_URL_REGEXP =~ tmp do
begin
break tmp if tmp == (t = f.call(tmp))
tmp = t
rescue
break
end
end
item["text"].sub!(url,tmp.force_encoding(Encoding::UTF_8))
}
true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment