Skip to content

Instantly share code, notes, and snippets.

@zliang-min
Created November 10, 2011 18:30
Show Gist options
  • Save zliang-min/1355677 to your computer and use it in GitHub Desktop.
Save zliang-min/1355677 to your computer and use it in GitHub Desktop.
You don't need a library for everything. For example, git.io :)
Git.io.generate 'https://gist.github.com/gists/1355677'
Git.io.generate 'https://github.com/gimi', 'gimi'
Git.io.recognize 'http://git.io/gimi'
require 'net/http'
module Git
module IO
URL = URI 'http://git.io'
class Error < RuntimeError; end
instance_eval do
def generate(url, code = nil)
handle_response Net::HTTP.post_form(URL, 'url' => url, 'code' => code), Net::HTTPSuccess
end
def recognize(url)
raise Error, 'Invalid git.io short url' unless %r/^#{URL}/ === url
handle_response Net::HTTP.get_response(URI(url)), Net::HTTPRedirection
end
private
def handle_response(response, expected_response_type)
if expected_response_type === response
response['Location']
else
raise Error, response.body
end
end
end
end
def self.io; IO end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment