Project

shigeru

0.01
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Define routes for your apps independent of your routing. Then generate your URIs with a uri_for helper.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.2.3
~> 10.0
 Project Readme

Shigeru build status

Shigeru helps you with the links and HTTP-URIs in your application. It is supposed to be a simpler version of what Rails offers. It is named after Shigeru Miyamoto, the creator of Link.

Shigeru is in an experimental phase while we figure out how to offer a simple uri_for helper that is agnostic of routing libraries. As it has not reached 1.0 yet, anything can change at any time. As soon as it stabilizes this will be marked by the 1.0 release.

Concept

In REST, Fielding defines four interface constraints. One of them is the identification of resources. This library tries to simplify that in the scope of an HTTP based web application. In order to do that, Shigeru offers a mapping from a resource to a resource identifier (in our case a URI as we are talking about HTTP based web applications). Fielding states a few examples for what a resource might be:

Any information that can be named can be a resource: a document or image, a temporal service (e.g. "today's weather in Los Angeles"), a collection of other resources, a non-virtual object (e.g. a person), and so on. In other words, any concept that might be the target of an author's hypertext reference must fit within the definition of a resource. -- Architectural Styles and the Design of Network-based Software Architectures, section 5.2.1.1: Resources and Resource Identifiers - Roy Thomas Fielding

Shigeru does not care about how your URIs look (in the same way that REST doesn't care about that). For Shigeru /users/12 is as valid as /createAllTheThings or /xaa/12/xhy. We don't judge you.

In Shigeru, you first define a route. Then you can use this definition throughout your code. For example:

require 'shigeru'

routes = Shigeru::Repository.new
routes.define :users, '/users'

# Somewhere later:
routes.uri_for(:users) #=> '/users'

Shigeru supports a subset of URI templates, Level 4 to make your URIs dynamic. It supports the simple insertion of a parameter:

routes.define :user, '/users/{id}'
routes.uri_for(:user, id: 13) #=> '/users/13'

It is also possible to explode a parameter (this is noted with a * suffix for that variable). It then expects an array instead of a single value:

routes.define :users, '/users/{ids*}'
routes.uri_for(:users, ids: [13,15]) #=> '/users/13,15'

It is also possible to insert query parameters. To do that, you add a question mark as the first character in your curly braces (we call this the sigil). It is possible to insert one or more variables in one curly brace:

routes.define :users, '/users{?name,country}'
routes.uri_for(:users, name: 'alice', country: 'argentina') #=> '/users?name=alice&country=argentina'

Finally, we can add route parameters. Again, we can use both explode modifiers as well as multiple parameters in one insertion:

routes.define :users, '/users{/xs*}'
routes.uri_for(:users, xs: ['alice', 'bob']) #=> '/users/alice/bob'

Design

The implementation of Shigeru is inspired by Michel Martens' mote and the codalyzed video about it. It trades performance (only executing a proc when calling uri_for that doesn't need to parse the template) against a slightly hairy implementation (building Ruby code by String concatenation and evaling it 😱).

Contributing

Bug reports and pull requests are welcome on GitHub. You can find more information about contributing in the CONTRIBUTING.md. This project is intended to be a safe, welcoming space for collaboration and discussion, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.