-
-
Save WaYdotNET/1367466 to your computer and use it in GitHub Desktop.
You don't need a library for everything. For example, git.io :)
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
| Git.io.generate 'https://gist.github.com/gists/1355677' | |
| Git.io.generate 'https://github.com/gimi', 'gimi' | |
| Git.io.recognize 'http://git.io/gimi' |
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 '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