RestlessRouter
This helps fill the gap where web services only provide their routing
via external documentation. In order to prevent URL building scattered
throughout your client, you can define the routes up-front via fully
qualified URIs or URI Templates.
You can then reference a URL by looking it up by it's link
relationship.
Installation
Add this line to your application's Gemfile:
gem 'restless_router'
And then execute:
$ bundle
Or install it yourself as:
$ gem install restless_router
Usage
The first step is to define the possible routes that a service may
utilize. In most cases they can be found in their online documentation
of the service.
require 'restless_router'
routes = RestlessRouter::Routes.new
routes.add_route(RestlessRouter::Route.new('directory', 'https://example.com/directory')
routes.add_route(RestlessRouter::Route.new('http://example.com/rels/user-detail', 'https://example.com/users/{id}', templated: true)
You may also use the <<
operator to add routes to the collection.
Once the routes have been defined, you may lookup the routes by their
IANA Link
Relationship
or Custom Link Relationships.
directory_route = routes.route_for('directory')
directory_url = directory_route.url_for
user_detail_route = routes.route_for('http://example.com/rels/user-detail')
user_defail_url = user_detail_route.url_for(id: '1234')
This can then be utilized as you see fit with your HTTP
adapter.
require 'faraday'
require 'restless_router'
class Application
def self.routes
end
end
directory_route = Application.routes.route_for('directory')
directory_url = directory_route.url_for
directory_request = Faraday.get(directory_url)
Approach
- There is a
Routes
collection that holds the route definitions. - There is a
Route
object that holds the details of the route definition. - There are mechanisms to find the route, and expand the route if necessary.
Some APIs may provide hypermedia envelopes and you should use those where
available.
Examples
I used this in the automatic-client
Gem to handle the small set of available routes. These are defined in the Automatic::Client
and available via the routes
class method.
Contributing
- Fork it ( http://github.com//restless_router/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request