The Ruby ecosystem has seen its fair share of HTTP clients. From the built-in Net::HTTP, to the various libcurl bindings, to Faraday, which lets applications switch between backends as they please.
All of them have one thing in common though: you have to write code to use them.
# Ugh, all this code Net::HTTP.get_response(URI("http://ifconfig.me/ip"))
When you think about it, HTTP is all about URLs. What if the URL was the code?
ForwardSlash is a new library, built on top of Net::HTTP, that does away with all the ceremony by adding language-level support for REST to Ruby.
It does this by introducing REST literals. Rather than using strings to represent the URL we want to fetch, the URL becomes a first-class citizen of the code.
Reclaiming the symbol
For centuries, a bunch of maths nerds have claimed sole usage of the forward slash.
If we should successfully construct a machine on which to perform computation, yet lack the foresight to include an input for the division symbol, I propose we repurpose the humble forward slash, which is sure to be present.
– Charles Babbage, 1857
If ForwardSlash was going to be a reality, we would have to reclaim the
/ symbol for its real purpose: REST.
Through Ruby’s syntactic flexibility, we were able to achieve just that.
/, known as the RESTful operator, to domains, we are able to fetch the resources hosted on those domains. As we know, RESTful resources can be nested, so to support this, the operator can be applied repeatedly:
Once the end of the URL is reached, ForwardSlash makes a HTTP request, and stores the response body in the
result variable. Automatically storing the result eliminates two problems: variable naming, and the fact that attempting to assign the result of a REST literal is a syntax error in Ruby.
Let’s do this
Enough chat. From today, you can install ForwardSlash and include it in your apps. Simply add it to your Gemfile, include it in your class, and you’ll literally be writing REST:
class CurrentIp include ForwardSlash def self.print http://ifconfig.me/ip puts result end end
Why let maths nerds have all the fun?