My First Ruby App

Okay, if you’ve got a working Ubuntu installation from the previous steps, this gets ruby working:

  • sudo apt-get -y install ruby1.9.1
  • sudo apt-get install rubygems1.9.1

Note that the commands installed are “ruby1.9.1” and “gem1.9.1” instead of “ruby” and “gem”.  You can now follow the Sinatra home page instructions and get Hello World running on your machine.

At this point, I’m starting to get annoyed at how long everything takes to install on windows.  But the next bit was a bit more of a shock to me.  Ruby fans will know what’s coming.

You see, my plan was to sit down, learn some Ruby and try to stick together a simple, well-specified program.  So, I thought, let’s try an HTTP Proxy Server.  It’s not hard, it’s easy to verify that it works, and it involves threads and sockets, so there’s plenty of stuff to play with.  Ruby fans will no what’s coming next: it’s also an API in the standard distribution.

So, if you want to write a proxy server in Ruby, consider the following code:

require ‘webrick’
require ‘webrick/httpproxy’

server = WEBrick::HTTPProxyServer.new(
    :Port => 8080,
    :RequestCallback => ->(request, response) { puts “Intercepted:  ” + request.request_line }
)
trap(“INT”) { server.shutdown }  # This catches Ctrl-C
server.start # start the server

It is, however, pretty slow, but I’m not sure if that’s just a reflection of the way I’ve got everything set up with VMware instances.  So next I’m going to see if I can get this working on IronRuby.

Technorati Tags: ,,

Published by

Julian Birch

Full time dad, does a bit of coding on the side.

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 )

Facebook photo

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

Connecting to %s