Growing Devs

Sep 30, 2013

Ruby Resty

by Austen Ito

I spend a lot of my time working with REST APIs. At home, at work, at hackathons, it’s GET this and POST that. Resty is an amazing tool to simplify sending multiple HTTP requests with the same data and headers. My favorite part is having per-host configuration so you never have to worry about remembering your cryptic, hashed API keys and header names.

It’s such a great tool, except resty currently doesn’t work in zsh.

After thinking about it, a shell script isn’t the best approach for community development. Coming from a Ruby background, shell script syntax is so hard to read. What about adding functionality to Resty? Can I add a new command that Resty supports? Can I have nice tests documenting functionality? How about some continuous integration?

And I embarked on a Ruby Port of Resty.

What started as a straight port, morphed into something I didn’t expect.

I started a spike with my own REPL. Thinking more about features, I realized I should be using something like the superb Pry. Pry allows Ruby Resty to be easily extensible with it’s Custom Command infrastructure. Since we’re using plain ol’ Ruby, we should use plain old ruby objects!

POST /api/nyan {"name": "grumpy"} # JSON String
POST /api/nyan {name: "grumpy"}   # Ruby Hash
POST /api/nyan data               # User assigned variable in the REPL

Pretty nifty.

One way Ruby Resty differs from Resty is host aliasing. Rather than specifiying a hostname, which maps to the config file, users can specify an easy to remember alias defined in ~/.ruby_resty.yml.

This is still a working in progress and I would love some feedback. If you’re interested, submit a ruby-resty issue or reply to this post.