zooplankton 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/.travis.yml +3 -0
- data/CHANGES.md +4 -0
- data/README.md +24 -1
- data/Rakefile +5 -0
- data/lib/zooplankton/version.rb +1 -1
- data/lib/zooplankton.rb +29 -6
- data/spec/zooplankton_spec.rb +64 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae65d74db6edba8ebc5b96293d864003f6aefad3
|
4
|
+
data.tar.gz: e0ba84294a367c0a767754a100562708131ccb1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00604a8f9af348e7d6ed678b29a9ad73bff37a520c50b1e6ae812425ba78beb10bc63a1eaa4231828bbcfac9f4f7885e9df8f7d5bf867e7ebd026762f76e797d
|
7
|
+
data.tar.gz: abebc6d574ec3c4bdc692f995de68e9c72a33097f05dab13881f9ace19a05ff66d4eb908c75e38155abb3348bbe3409c31f5145fee998f3d7dc6b7f3c840eaff
|
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
--color
|
2
|
-
--format
|
2
|
+
--format documentation
|
data/.travis.yml
ADDED
data/CHANGES.md
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Zooplankton
|
1
|
+
# Zooplankton [![Build Status](https://travis-ci.org/benhamill/zooplankton.png)](https://travis-ci.org/benhamill/zooplankton) [![Code Climate](https://codeclimate.com/github/benhamill/zooplankton.png)](https://codeclimate.com/github/benhamill/zooplankton)
|
2
2
|
|
3
3
|
Zooplankton is a library for helping you turn Rails routes into
|
4
4
|
[URI Template strings](http://tools.ietf.org/html/rfc6570). It's useful for
|
@@ -40,8 +40,31 @@ You can use it like this:
|
|
40
40
|
# => '/post/{slug}'
|
41
41
|
> Zooplankton.path_template_for(:comment)
|
42
42
|
# => '/post/{slug}/comment/{comment_id}'
|
43
|
+
```
|
44
|
+
|
45
|
+
It also handles replacing some (or all) of the templated variables if you want
|
46
|
+
to prepopulate some of them ahead of time:
|
47
|
+
|
48
|
+
``` ruby
|
43
49
|
> Zooplankton.path_template_for(:comment, slug: 'the-best-post-ever')
|
44
50
|
# => '/post/the-best-post-ever/comment/{comment_id}'
|
51
|
+
```
|
52
|
+
|
53
|
+
And you can add some query parameters when you're generating the template, if
|
54
|
+
you need:
|
55
|
+
|
56
|
+
``` ruby
|
57
|
+
> Zooplankton.path_template_for(:comment, :q, slug: 'the-best-post-ever')
|
58
|
+
# => '/post/the-best-post-ever/comment/{comment_id}{?q}'
|
59
|
+
> Zooplankton.path_template_for(:comment, %i(foo bar), slug: 'the-best-post-ever')
|
60
|
+
# => '/post/the-best-post-ever/comment/{comment_id}{?foo,bar}'
|
61
|
+
> Zooplankton.path_template_for(:comment, %i(foo bar))
|
62
|
+
# => '/post/{slug}/comment/{comment_id}{?foo,bar}'
|
63
|
+
```
|
64
|
+
|
65
|
+
It'll generate URLs, too, not just paths.
|
66
|
+
|
67
|
+
``` ruby
|
45
68
|
> Zooplankton.url_template_for(:root)
|
46
69
|
# => 'http://example.com/'
|
47
70
|
> Zooplankton.url_template_for(:post)
|
data/Rakefile
CHANGED
data/lib/zooplankton/version.rb
CHANGED
data/lib/zooplankton.rb
CHANGED
@@ -4,19 +4,42 @@ require "zooplankton/version"
|
|
4
4
|
|
5
5
|
module Zooplankton
|
6
6
|
class << self
|
7
|
-
def path_template_for(helper_name,
|
8
|
-
|
7
|
+
def path_template_for(helper_name, query_params={}, required_params=nil)
|
8
|
+
build_template(:path, helper_name, *parse_params(query_params, required_params))
|
9
|
+
end
|
10
|
+
|
11
|
+
def url_template_for(helper_name, query_params={}, required_params=nil)
|
12
|
+
build_template(:url, helper_name, *parse_params(query_params, required_params))
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
9
16
|
|
10
|
-
|
17
|
+
def parse_params(*args)
|
18
|
+
if args.first.respond_to?(:to_h)
|
19
|
+
query_params = []
|
20
|
+
required_params = args.first.to_h
|
21
|
+
else
|
22
|
+
query_params = Array(args.shift)
|
23
|
+
required_params = args.first || {}
|
24
|
+
end
|
25
|
+
|
26
|
+
[query_params, required_params]
|
11
27
|
end
|
12
28
|
|
13
|
-
def
|
29
|
+
def build_template(type, helper_name, query_params, required_params)
|
14
30
|
return unless named_routes.names.include?(helper_name)
|
15
31
|
|
16
|
-
|
32
|
+
escaped_template_without_query_params = expand_helper(helper_name, type, required_params)
|
33
|
+
escaped_template = append_query_params(escaped_template_without_query_params, query_params)
|
34
|
+
|
35
|
+
unescape_template(escaped_template)
|
17
36
|
end
|
18
37
|
|
19
|
-
|
38
|
+
def append_query_params(template, query_params)
|
39
|
+
return template unless query_params.any?
|
40
|
+
|
41
|
+
"#{template}{?#{query_params.join(',')}}"
|
42
|
+
end
|
20
43
|
|
21
44
|
def expand_helper(helper_name, path_or_url, params)
|
22
45
|
helper_method = "#{helper_name}_#{path_or_url}"
|
data/spec/zooplankton_spec.rb
CHANGED
@@ -23,6 +23,22 @@ describe Zooplankton do
|
|
23
23
|
it "returns just the path" do
|
24
24
|
expect(subject).to eq('/')
|
25
25
|
end
|
26
|
+
|
27
|
+
context "with one query parameter" do
|
28
|
+
subject { Zooplankton.path_template_for(:root, :q) }
|
29
|
+
|
30
|
+
it "templateizes the query param" do
|
31
|
+
expect(subject).to eq('/{?q}')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "with some query parameters" do
|
36
|
+
subject { Zooplankton.path_template_for(:root, %i(q f)) }
|
37
|
+
|
38
|
+
it "templateizes the query params" do
|
39
|
+
expect(subject).to eq('/{?q,f}')
|
40
|
+
end
|
41
|
+
end
|
26
42
|
end
|
27
43
|
|
28
44
|
context "for a route with one required param" do
|
@@ -31,6 +47,22 @@ describe Zooplankton do
|
|
31
47
|
it "uses simple string expansion to describe the parameter" do
|
32
48
|
expect(subject).to eq('/post/{slug}')
|
33
49
|
end
|
50
|
+
|
51
|
+
context "with one query parameter" do
|
52
|
+
subject { Zooplankton.path_template_for(:post, :q) }
|
53
|
+
|
54
|
+
it "templateizes the query param" do
|
55
|
+
expect(subject).to eq('/post/{slug}{?q}')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context "with some query parameters" do
|
60
|
+
subject { Zooplankton.path_template_for(:post, %i(q f)) }
|
61
|
+
|
62
|
+
it "templateizes the query params" do
|
63
|
+
expect(subject).to eq('/post/{slug}{?q,f}')
|
64
|
+
end
|
65
|
+
end
|
34
66
|
end
|
35
67
|
|
36
68
|
context "for a route with more than one required param" do
|
@@ -40,6 +72,22 @@ describe Zooplankton do
|
|
40
72
|
it "uses simple string expansion to describe the parameters" do
|
41
73
|
expect(subject).to eq('/post/{slug}/comment/{comment_id}')
|
42
74
|
end
|
75
|
+
|
76
|
+
context "with one query parameter" do
|
77
|
+
subject { Zooplankton.path_template_for(:comment, :q) }
|
78
|
+
|
79
|
+
it "templateizes the query param" do
|
80
|
+
expect(subject).to eq('/post/{slug}/comment/{comment_id}{?q}')
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context "with some query parameters" do
|
85
|
+
subject { Zooplankton.path_template_for(:comment, %i(q f)) }
|
86
|
+
|
87
|
+
it "templateizes the query params" do
|
88
|
+
expect(subject).to eq('/post/{slug}/comment/{comment_id}{?q,f}')
|
89
|
+
end
|
90
|
+
end
|
43
91
|
end
|
44
92
|
|
45
93
|
context "with one supplied parameter value" do
|
@@ -48,6 +96,22 @@ describe Zooplankton do
|
|
48
96
|
it "uses simple string expansion to describe the parameters" do
|
49
97
|
expect(subject).to eq('/post/post-slug/comment/{comment_id}')
|
50
98
|
end
|
99
|
+
|
100
|
+
context "with one query parameter" do
|
101
|
+
subject { Zooplankton.path_template_for(:comment, :q, slug: 'post-slug') }
|
102
|
+
|
103
|
+
it "templateizes the query param" do
|
104
|
+
expect(subject).to eq('/post/post-slug/comment/{comment_id}{?q}')
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "with some query parameters" do
|
109
|
+
subject { Zooplankton.path_template_for(:comment, %i(q f), slug: 'post-slug') }
|
110
|
+
|
111
|
+
it "templateizes the query params" do
|
112
|
+
expect(subject).to eq('/post/post-slug/comment/{comment_id}{?q,f}')
|
113
|
+
end
|
114
|
+
end
|
51
115
|
end
|
52
116
|
end
|
53
117
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zooplankton
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Hamill
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-11-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -90,6 +90,7 @@ extra_rdoc_files: []
|
|
90
90
|
files:
|
91
91
|
- .gitignore
|
92
92
|
- .rspec
|
93
|
+
- .travis.yml
|
93
94
|
- CHANGES.md
|
94
95
|
- Gemfile
|
95
96
|
- LICENSE.txt
|