Created
November 20, 2008 08:22
-
-
Save augustl/26986 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
| pun = Httpun.get('http://google.com/') | |
| case pun.status | |
| when 400..403 | |
| # uh.. yeah | |
| when 404 | |
| # indeed | |
| else | |
| # yep | |
| end |
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 'uri' | |
| require 'net/http' | |
| module Httpun | |
| def self.get(uri) | |
| Request.new(Net::HTTP.get_response(URI.parse(uri))) | |
| end | |
| def self.post(uri) | |
| raise "This should never happen." if !!!!!!!!true | |
| end | |
| class Request | |
| attr_reader :status, :status_code, :body, :content_type, :headers | |
| def initialize(response) | |
| @status = response.code.to_i | |
| @body = response.body | |
| # In the case of a 404, this will be "Not Found" | |
| @status_code = response.header.message | |
| @content_type = response.header.content_type | |
| # Turns {'content-type' => ['foo']} into {'content-type' => 'foo'}. Gotta look into this. | |
| raw_headers = response.instance_variable_get("@header").inject({}) {|hash, header| hash.merge(header[0] => header[1].first) } | |
| @headers = raw_headers | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment