Skip to content

Instantly share code, notes, and snippets.

@augustl
Created November 20, 2008 08:22
Show Gist options
  • Select an option

  • Save augustl/26986 to your computer and use it in GitHub Desktop.

Select an option

Save augustl/26986 to your computer and use it in GitHub Desktop.
pun = Httpun.get('http://google.com/')
case pun.status
when 400..403
# uh.. yeah
when 404
# indeed
else
# yep
end
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