Proxying HTTP Requests in Ruby

So building upon my other jaunts into Ruby I decided to post some code to make http requests to external urls. It could use come polish and there there is most definitely better mechanisms out there (probably a gem or two… or three) but tis fun to write well… code.

First, here is an example of using it.

require 'Proxy'
class HttpCaller
  include Proxy
end

caller = HttpCaller.new

response = caller.call "http://www.somewebsite.com"

if response[:error]
  puts response[:object].message
else
  response[:object].each_header do |key, value|
    puts "#{key}: #{value}"
  end
  puts response[:object].code.to_s
  puts response[:object].body 
end

And I have posted the full code sample here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.