zooplankton 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae65d74db6edba8ebc5b96293d864003f6aefad3
4
- data.tar.gz: e0ba84294a367c0a767754a100562708131ccb1c
3
+ metadata.gz: 07178fdc94e41e3d73816c73617dca152a1c2220
4
+ data.tar.gz: 202ce012feb0b347b5863219052b008096d36bf2
5
5
  SHA512:
6
- metadata.gz: 00604a8f9af348e7d6ed678b29a9ad73bff37a520c50b1e6ae812425ba78beb10bc63a1eaa4231828bbcfac9f4f7885e9df8f7d5bf867e7ebd026762f76e797d
7
- data.tar.gz: abebc6d574ec3c4bdc692f995de68e9c72a33097f05dab13881f9ace19a05ff66d4eb908c75e38155abb3348bbe3409c31f5145fee998f3d7dc6b7f3c840eaff
6
+ metadata.gz: 3f81add78dbebd1b84126ace5d89e9eec047196a48d3ea33086ed8374b3a255e87b22900c92a8e76ff57938d37c209f6158404603f3aa1f88d7cb9086e201cb5
7
+ data.tar.gz: d02fa7b216e7b9c71e18bf7c9175437996e35e5422698519941fa899505afed5ea57a144b78907fefe742e5193a5c3e3b5c3ddde88307870d2c44fcf89c559f3
data/CHANGES.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changes
2
2
 
3
+ ## 1.1.0
4
+
5
+ * Added supplied query parameter handling, including the use of continuation
6
+ marker. - Ben Hamill
7
+
3
8
  ## 1.0.0
4
9
 
5
10
  * Added ability to append query parameters to your templates. - Ben Hamill
data/README.md CHANGED
@@ -62,6 +62,13 @@ you need:
62
62
  # => '/post/{slug}/comment/{comment_id}{?foo,bar}'
63
63
  ```
64
64
 
65
+ If you supply a query parameter for replacement, it'll denote a continuation:
66
+
67
+ ``` ruby
68
+ > Zooplankton.path_template_for(:comment, %i(foo bar), slug: 'the-best-post-ever', bar: 'baz')
69
+ # => '/post/the-best-post-ever/comment/{comment_id}?bar=baz{&foo}'
70
+ ```
71
+
65
72
  It'll generate URLs, too, not just paths.
66
73
 
67
74
  ``` ruby
@@ -1,3 +1,3 @@
1
1
  module Zooplankton
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/zooplankton.rb CHANGED
@@ -4,12 +4,12 @@ require "zooplankton/version"
4
4
 
5
5
  module Zooplankton
6
6
  class << self
7
- def path_template_for(helper_name, query_params={}, required_params=nil)
8
- build_template(:path, helper_name, *parse_params(query_params, required_params))
7
+ def path_template_for(helper_name, query_params={}, supplied_params=nil)
8
+ build_template(:path, helper_name, *parse_params(query_params, supplied_params))
9
9
  end
10
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))
11
+ def url_template_for(helper_name, query_params={}, supplied_params=nil)
12
+ build_template(:url, helper_name, *parse_params(query_params, supplied_params))
13
13
  end
14
14
 
15
15
  private
@@ -17,34 +17,51 @@ module Zooplankton
17
17
  def parse_params(*args)
18
18
  if args.first.respond_to?(:to_h)
19
19
  query_params = []
20
- required_params = args.first.to_h
20
+ supplied_params = args.first.to_h
21
21
  else
22
22
  query_params = Array(args.shift)
23
- required_params = args.first || {}
23
+ supplied_params = args.first || {}
24
24
  end
25
25
 
26
- [query_params, required_params]
26
+ [query_params, supplied_params]
27
27
  end
28
28
 
29
- def build_template(type, helper_name, query_params, required_params)
29
+ def build_template(type, helper_name, query_params, supplied_params)
30
30
  return unless named_routes.names.include?(helper_name)
31
31
 
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)
32
+ escaped_template_without_query_params = expand_helper(helper_name, type, supplied_params)
33
+ escaped_template = append_query_params(escaped_template_without_query_params, query_params, supplied_params)
34
34
 
35
35
  unescape_template(escaped_template)
36
36
  end
37
37
 
38
- def append_query_params(template, query_params)
38
+ def append_query_params(template, query_params, supplied_params)
39
39
  return template unless query_params.any?
40
40
 
41
- "#{template}{?#{query_params.join(',')}}"
41
+ supplied_query_params = query_params & supplied_params.keys
42
+ supplied_query_string = ''
43
+
44
+ if supplied_query_params.any?
45
+ continuation_or_expansion = '&'
46
+
47
+ supplied_query_string << '?'
48
+
49
+ supplied_query_string << supplied_query_params.map do |key|
50
+ "#{key.to_s}=#{URI.encode(supplied_params[key].to_s)}"
51
+ end.join('&')
52
+
53
+ query_params = query_params - supplied_query_params
54
+ else
55
+ continuation_or_expansion = '?'
56
+ end
57
+
58
+ "#{template}#{supplied_query_string}{#{continuation_or_expansion}#{query_params.join(',')}}"
42
59
  end
43
60
 
44
61
  def expand_helper(helper_name, path_or_url, params)
45
62
  helper_method = "#{helper_name}_#{path_or_url}"
46
63
 
47
- url_helpers.send(helper_method, *templated_required_params_for(helper_name, params))
64
+ url_helpers.send(helper_method, *templated_supplied_params_for(helper_name, params))
48
65
  end
49
66
 
50
67
  def named_routes
@@ -55,7 +72,7 @@ module Zooplankton
55
72
  named_routes.routes[helper_name]
56
73
  end
57
74
 
58
- def templated_required_params_for(helper_name, params)
75
+ def templated_supplied_params_for(helper_name, params)
59
76
  route_object_for(helper_name).required_parts.map do |required_part|
60
77
  params.fetch(required_part) { "{#{required_part}}" }
61
78
  end
@@ -112,6 +112,14 @@ describe Zooplankton do
112
112
  expect(subject).to eq('/post/post-slug/comment/{comment_id}{?q,f}')
113
113
  end
114
114
  end
115
+
116
+ context "with some query parameters that are supplied" do
117
+ subject { Zooplankton.path_template_for(:comment, %i(q f), slug: 'post-slug', q: 'find me') }
118
+
119
+ it "templateizes the query params" do
120
+ expect(subject).to eq('/post/post-slug/comment/{comment_id}?q=find me{&f}')
121
+ end
122
+ end
115
123
  end
116
124
  end
117
125
  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: 1.0.0
4
+ version: 1.1.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-15 00:00:00.000000000 Z
11
+ date: 2013-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler