shigeru 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/CONTRIBUTING.md +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +67 -0
- data/Rakefile +8 -0
- data/lib/shigeru.rb +68 -0
- data/lib/shigeru/version.rb +3 -0
- data/shigeru.gemspec +22 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 35c7d95ef534f5cb70343ad87648446a45249252
|
4
|
+
data.tar.gz: 6c8f52e52c6d9adabc957ab781eed40dc14ad082
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a03c5e0f19ba300904e7dc478ccdba6a5a4eb540e65f1b083ba9500d44764f11c82fdae4492507463db914706015a9b4960ff659dd7771289842fc1ead38d89b
|
7
|
+
data.tar.gz: 2aab28e69906f70badbe1a06fea619f9281dd949d2e740091bd3e3eb2d2d44e229881d6b8656fac1064508f8a14e14315bf4e00d183e03c8c1825011cffe1ef4
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
4
|
+
|
5
|
+
We welcome any kind of contribution: Documentation, code, issues, discussions, critique, speaking about it...
|
6
|
+
|
7
|
+
The tests are written using [cutest](https://github.com/djanowski/cutest). Please describe all new features with tests.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Lucas Dohmen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Shigeru [![build status](https://travis-ci.org/moonglum/shigeru.svg)](https://travis-ci.org/moonglum/shigeru)
|
2
|
+
|
3
|
+
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](https://en.wikipedia.org/wiki/Shigeru_Miyamoto), the creator of Link.
|
4
|
+
|
5
|
+
**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.**
|
6
|
+
|
7
|
+
|
8
|
+
# Concept
|
9
|
+
|
10
|
+
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:
|
11
|
+
|
12
|
+
> 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.
|
13
|
+
> -- Architectural Styles and the Design of Network-based Software Architectures, section 5.2.1.1: Resources and Resource Identifiers - [Roy Thomas Fielding](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_2)
|
14
|
+
|
15
|
+
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.
|
16
|
+
|
17
|
+
In Shigeru, you first define a route. Then you can use this definition throughout your code. For example:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require 'shigeru'
|
21
|
+
|
22
|
+
routes = Shigeru::Repository.new
|
23
|
+
routes.define :users, '/users'
|
24
|
+
|
25
|
+
# Somewhere later:
|
26
|
+
routes.uri_for(:users) #=> '/users'
|
27
|
+
```
|
28
|
+
|
29
|
+
Shigeru supports a subset of [URI templates](https://tools.ietf.org/html/rfc6570), Level 4 to make your URIs dynamic. It supports the simple insertion of a parameter:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
routes.define :user, '/users/{id}'
|
33
|
+
routes.uri_for(:user, id: 13) #=> '/users/13'
|
34
|
+
```
|
35
|
+
|
36
|
+
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:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
routes.define :users, '/users/{ids*}'
|
40
|
+
routes.uri_for(:users, ids: [13,15]) #=> '/users/13,15'
|
41
|
+
```
|
42
|
+
|
43
|
+
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:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
routes.define :users, '/users{?name,country}'
|
47
|
+
routes.uri_for(:users, name: 'alice', country: 'argentina') #=> '/users?name=alice&country=argentina'
|
48
|
+
```
|
49
|
+
|
50
|
+
Finally, we can add route parameters. Again, we can use both explode modifiers as well as multiple parameters in one insertion:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
routes.define :users, '/users{/xs*}'
|
54
|
+
routes.uri_for(:users, xs: ['alice', 'bob']) #=> '/users/alice/bob'
|
55
|
+
```
|
56
|
+
|
57
|
+
## Design
|
58
|
+
|
59
|
+
The implementation of Shigeru is inspired by [Michel Martens](https://github.com/soveran)' [mote](https://github.com/soveran/mote) and the [codalyzed video about it](https://codalyzed.com/videos/lesscode). 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 `eval`ing it :scream:).
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/moonglum/shigeru). You can find more information about contributing in the [CONTRIBUTING.md](https://github.com/moonglum/shigeru/blob/master/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](contributor-covenant.org) code of conduct.
|
64
|
+
|
65
|
+
## License
|
66
|
+
|
67
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/shigeru.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require "shigeru/version"
|
2
|
+
require "uri"
|
3
|
+
|
4
|
+
module Shigeru
|
5
|
+
class Repository
|
6
|
+
def initialize
|
7
|
+
@mapping = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def define(name, template)
|
11
|
+
@mapping[name] = UriTemplate.new(template)
|
12
|
+
end
|
13
|
+
|
14
|
+
def uri_for(name, parameters={})
|
15
|
+
@mapping.fetch(name).expand(parameters)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Handles a subset of [URI Template](https://tools.ietf.org/html/rfc6570), Level 4
|
20
|
+
class UriTemplate
|
21
|
+
PATTERN = /{([\?\/]?)([\w,*]+)}/
|
22
|
+
|
23
|
+
def initialize(template)
|
24
|
+
terms = template.split(PATTERN)
|
25
|
+
parts = ["Proc.new do |params, __o|", "params ||= {}", "__o ||= ''"]
|
26
|
+
while (term = terms.shift)
|
27
|
+
case term
|
28
|
+
when '' then parts.push(expansion('', ',', terms.shift.split(',')))
|
29
|
+
when '?' then parts.push(expansion('?', '&', terms.shift.split(',')))
|
30
|
+
when '/' then parts.push(expansion('/', '/', terms.shift.split(',')))
|
31
|
+
else parts << "__o << '#{term}'"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
parts.push("__o", "end")
|
35
|
+
@template = self.instance_eval(parts.join("\n"))
|
36
|
+
end
|
37
|
+
|
38
|
+
def expand(parameters)
|
39
|
+
@template[parameters]
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def expansion(sigil, seperator, terms)
|
45
|
+
result = []
|
46
|
+
|
47
|
+
case sigil
|
48
|
+
when '?' then result << "__o << '?'"
|
49
|
+
when '/' then result << "__o << '/'"
|
50
|
+
end
|
51
|
+
|
52
|
+
while (term = terms.shift)
|
53
|
+
if term[-1] == '*'
|
54
|
+
result << "__o << params[:#{term[0...-1]}].map do |t|"
|
55
|
+
result << " '#{term[0...-1]}=' +" if sigil == '?'
|
56
|
+
result << " URI.escape(t.to_s)"
|
57
|
+
result << "end.join('#{seperator}')"
|
58
|
+
else
|
59
|
+
result << "__o << '#{term}='" if sigil == '?'
|
60
|
+
result << "__o << URI.escape(params[:#{term}].to_s)"
|
61
|
+
end
|
62
|
+
result << "__o << '#{seperator}'" if terms.any?
|
63
|
+
end
|
64
|
+
|
65
|
+
result
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/shigeru.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'shigeru/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "shigeru"
|
8
|
+
spec.version = Shigeru::VERSION
|
9
|
+
spec.authors = ["Lucas Dohmen"]
|
10
|
+
spec.email = ["lucas@dohmen.io"]
|
11
|
+
|
12
|
+
spec.summary = %q{A routing library agnostic uri_for helper}
|
13
|
+
spec.description = %q{Define routes for your apps independent of your routing. Then generate your URIs with a uri_for helper.}
|
14
|
+
spec.homepage = "https://github.com/moonglum/shigeru"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test)/}) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "cutest", "~> 1.2.3"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shigeru
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lucas Dohmen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: cutest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.2.3
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.2.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Define routes for your apps independent of your routing. Then generate
|
42
|
+
your URIs with a uri_for helper.
|
43
|
+
email:
|
44
|
+
- lucas@dohmen.io
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- ".travis.yml"
|
51
|
+
- CODE_OF_CONDUCT.md
|
52
|
+
- CONTRIBUTING.md
|
53
|
+
- Gemfile
|
54
|
+
- LICENSE.txt
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- lib/shigeru.rb
|
58
|
+
- lib/shigeru/version.rb
|
59
|
+
- shigeru.gemspec
|
60
|
+
homepage: https://github.com/moonglum/shigeru
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.4.5
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: A routing library agnostic uri_for helper
|
84
|
+
test_files: []
|