Ruby On Braille

Technology Agnostic Rubyist : Open Source Lights The Way

Friday, June 21, 2013

Two line HTTP Server with Rack

Working APIs often has its own set of challenges. One of these that I've come across often enough to blog about is composing an initial request to the API and tracking down errors.

To test if the problem arises from a malformed querystring or the http header, I needed to mock the http_request and echo the http_response.

To compose an HTTP requests I like to use the CURL library.

To echo the full HTTP request back to the requester, I wrote a simple two line HTTP Server.
require 'rack'
echo = lambda { |env| [200, {}, env.map{|k,v|"#{k}:\t#{v}\n"} ] }

To run the server, simply fire up an IRB console and copy/paste the code above and type.
Rack::Server.start :app => echo, :Port => 8080

That's it, you're ready to see it in action! curl an HTTP request.
curl -v http://localhost:8080/show_me_the_money?in_small_unmarked_bills=100bills

And you'll get a full echo of the HTTP request.
* About to connect() to localhost port 8080 (#0)
*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET /show_me_the_money?in_small_unmarked_bills=100bills HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:8080
> Accept: */*

< HTTP/1.1 200 OK 
< Server: WEBrick/1.3.1 (Ruby/1.9.3/2013-05-15)
< Date: Fri, 21 Jun 2013 14:22:32 GMT
< Content-Length: 703
< Connection: Keep-Alive

GATEWAY_INTERFACE: CGI/1.1
PATH_INFO: /show_me_the_money
QUERY_STRING: in_small_unmarked_bills=100bills
REMOTE_ADDR: ::1
REMOTE_HOST: localhost
REQUEST_METHOD: GET
REQUEST_URI: http://localhost:8080/show_me_the_money?in_small_unmarked_bills=100bills
SCRIPT_NAME:
SERVER_NAME: localhost
SERVER_PORT: 8080
SERVER_PROTOCOL: HTTP/1.1
SERVER_SOFTWARE: WEBrick/1.3.1 (Ruby/1.9.3/2013-05-15)
HTTP_USER_AGENT: curl/7.29.0
HTTP_HOST: localhost:8080
HTTP_ACCEPT: */*
rack.version: [1, 1]
rack.input: #<StringIO:0x007f84131738d0>
rack.errors: #<IO:0x007f841306f420>
rack.multithread: true
rack.multiprocess: false
rack.run_once: false
rack.url_scheme: http
HTTP_VERSION: HTTP/1.1

REQUEST_PATH: /show_me_the_money

Thursday, January 17, 2013

instance.method(:split).source_location

Discover monkey-patched code by invoking source_location via IRB,
r = 'some string'
r.method(:split).source_location
 => nil # String.split method code is not monkey patched

Then, via IRB
r = 'some string'
class String
  def split(e)
    puts 'what?'
  end
end
r.method(:split).source_location
 => what?
 => ["(irb)", 38] # Now I know where the method lives!

Reference, The split is not enough whitespace shenanigans for rubyists



Tuesday, December 18, 2012

begin #dosomething rescue false end

I'm becoming a fan of the following Ruby code idiom, where a single line of code evaluates whether something is true or false.

def interrogative_function?(args)
    begin 1==1 rescue false end
end

Monday, December 3, 2012

Oracle Data Modeler, unsigned apps, and OSX Mountain Lion

I am loving this new macbook pro. As with any new computer, I've been installing most of the open source programs that I've gotten used to without a hitch. But today I came across an issue installing Oracle Data Modeler. The default security settings in OS/X 10.8 prevent installing unsigned apps and Oracle Data Modeler is one such unsigned app.

For all the noobs including me, the solution is to adjust the security settings in Apple's new Gatekeeper.

Here's a link to an explanation by that Jeff Smith => http://is.gd/Z8KuoO.

Saturday, June 23, 2012

Rails['2.0'..'3.x'].each do |learning|

I am porting an old RoR site I built in 2005/6 over to latest version of rails.

It took me days of effort to figure out why templates wouldn't render inside of the application layout.

It was b/c I was using <%= @content_for_layout %> in my application.html.erb file and it's been deprecated in favor of a simple <%= yield %>. #refactoring! hindsight is 20/20!

This script by John Nunemaker was useful for renaming my old view files from .erb to .html.erb.

Monday, February 22, 2010

reverse engineering and acts_as_state_machine

I started using railroad to help me understand (by way of reverse engineering) a complex system I'm working on.

I really like the fact that it generates .dot files which can be opened and manipulated using Omnigraffle, but it was failing on AASM due to complicated dependencies which I don't care to resolve.

Today I discovered that I can use ruby-graphviz to make pretty drawings of complex state machine events in my models. I highly recommend it.

Tuesday, November 3, 2009

Fresh Cucumber

I've been putting together a little project using Rspec and Cucumber. It's a way of coding called Behaviour Driven Development and I'm loving it.

Here is Aslak Hellesøy's excellent backgrounder on Cucumber: BDD that talks to domain experts first and code second

.

Tweets

Contributors