Created
April 25, 2009 21:51
-
-
Save taf2/101768 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'rubygems' | |
require 'curb' | |
require 'cgi' | |
# expand a tiny url to a real large url, use twitter api and the tiny url see who wins | |
def tiny2real(tiny_url) | |
# send both in a multi handle the first back wins | |
mh = Curl::Multi.new | |
# set up a head request to the tiny url | |
ch1 = Curl::Easy.new(tiny_url) do|curl| | |
curl.head = true | |
end | |
# set up a GET request to twitter's service | |
ch2 = Curl::Easy.new("http://search.twitter.com/hugeurl?url=#{CGI::escape(tiny_url)}") | |
@response = "" | |
ch1.on_complete { puts "HEAD wins"; mh.remove ch2; ch1.header_str.scan(/Location: (.*)$/); @response = $1.strip; } | |
ch2.on_complete { puts "GET wins"; mh.remove ch1; @response = ch2.body_str } | |
mh.add ch1 | |
mh.add ch2 | |
mh.perform | |
@response | |
end | |
if $0 == __FILE__ | |
require 'test/unit' | |
class SomeTests < Test::Unit::TestCase | |
def test_tiny2real | |
assert_equal "http://lists.rpmfusion.org/pipermail/rpmfusion-developers/2008-August/000804.html", | |
tiny2real("http://tinyurl.com/d9w5fy") | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment