0.0
No commit activity in last 3 years
No release in over 3 years
Many web services lack hypermedia or consistent routing. This gives a single place to house routes using URI Templates instead of building URLs throughout the client.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.5
>= 0
>= 0
>= 0

Runtime

 Project Readme

RestlessRouter

Build Status

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

# Add a fully qualified URI
routes.add_route(RestlessRouter::Route.new('directory', 'https://example.com/directory')

# Add a URI Templated
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.

Applications that don't wish to register a relation type can use an extension relation type, which is a URI that uniquely identifies the relation type. Extension Relation Types

# Look up the Directory route
directory_route = routes.route_for('directory')
directory_url   = directory_route.url_for
# => 'https://example.com/directory'

# Look up the User Detail route
user_detail_route = routes.route_for('http://example.com/rels/user-detail')
user_detail_url   = user_detail_route.url_for(id: '1234')
# => 'https://example.com/users/1234'

This can then be utilized as you see fit with your HTTP adapter.

require 'faraday'
require 'restless_router'

# Routes are defined in the core application
class Application
  def self.routes
    # Include route definitions here
  end
end

# We can then reference the routes
directory_route = Application.routes.route_for('directory')
directory_url   = directory_route.url_for

# And make a request
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

  1. Fork it ( http://github.com/nateklaiber/restless_router/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request