zooplankton 1.2.0 → 1.3.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: c878d585575f883e203938c61e037c88a667446b
4
- data.tar.gz: 0636a9f4041b56d76e419b2e9c25ea0a70b5f4ff
3
+ metadata.gz: 41a78b4848286bffd070b3dfa475cc20a46315df
4
+ data.tar.gz: 6ce1b3626fa6f4a0cd21d0c260947fa7ef023e46
5
5
  SHA512:
6
- metadata.gz: 48c544b8b85fd25ccddab92c59a3c188d25939214a18ee531a313f9071b531f9df1d70dc1fb1c36dfd7d9621ca88ed81fffc68c91e3e3be70c3c4968d10a724b
7
- data.tar.gz: 1334e12ea45556b4e50dea5fe21c583b6d745b4a43f075adf02829b02b53cb3180ed0197b42e2070bd9f8d0353dce1f9ddb2c279427e37fcbc56fc1e35eac8cc
6
+ metadata.gz: 7f3a722e1590fbb1c536e91523d7811ce24ef3cbb3efe2784371db35890160d648ac02bd6c58a0ce9c4ecb8810aed349bfe1239d5485d6f7cb3077a2198d5dca
7
+ data.tar.gz: 2df979c72e073df641945139a5ca12e4b777e1344c56b6dc4afe640f7cf4873c6ac92cc1972c7931608d262295e59cc09289821837024da1bb96e4cac02556de
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ gemfiles/*.lock
@@ -1,4 +1,15 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.0.0
4
- - 2.1.2
4
+ - 2.2.2
5
+ gemfile:
6
+ - gemfiles/rails4.gemfile
7
+ - gemfiles/rails5.gemfile
8
+ matrix:
9
+ fast_finish: true
10
+ exclude:
11
+ - gemfile: gemfiles/rails4.gemfile
12
+ rvm: 2.2.2
13
+ - gemfile: gemfiles/rails5.gemfile
14
+ rvm: 2.0.0
15
+ cache: bundler
@@ -0,0 +1,7 @@
1
+ appraise "rails4" do
2
+ gem "rails", "~> 4.2.0"
3
+ end
4
+
5
+ appraise "rails5" do
6
+ gem "rails", "~> 5.0.0"
7
+ end
data/CHANGES.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changes
2
2
 
3
+ ## 1.3.0
4
+
5
+ * Support Rails 5 - Ismael Celis
6
+ * Fix bug where supplying all query parameters would leave in a trailing `{&}` - Ismael Celis
7
+
3
8
  ## 1.2.0
4
9
 
5
10
  * Added support for Ruby 2.1. - Ben Weiner
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem "appraisal"
4
+
3
5
  # Specify your gem's dependencies in zooplankton.gemspec
4
6
  gemspec
data/README.md CHANGED
@@ -126,6 +126,20 @@ like `"name?middle=dale{?first,last}"` which would end up as
126
126
  `"name?middle=dale&first=ben&last=hamill"`. Zooplankton will use this when it's
127
127
  appropriate for building query strings.
128
128
 
129
+ ## Testing
130
+
131
+ ```
132
+ rake spec
133
+ ```
134
+
135
+ This library uses the [Appraisal](https://github.com/thoughtbot/appraisal) gem to test against multiple versions of Rails. You can run them individually with
136
+
137
+ ```
138
+ appraisal rails4 rake spec
139
+ appraisal rails5 rake spec
140
+ ```
141
+
142
+ Look in `./Appraisals` for available Rails versions.
129
143
 
130
144
  ## Contributing
131
145
 
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
1
3
  require "bundler/gem_tasks"
2
4
  require "rspec/core/rake_task"
3
5
 
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "rails", "~> 4.2.0"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "rails", "~> 5.0.0"
7
+
8
+ gemspec :path => "../"
@@ -1,89 +1,16 @@
1
- require "rails"
2
-
3
1
  require "zooplankton/version"
2
+ require "zooplankton/parser"
4
3
 
5
4
  module Zooplankton
6
5
  class << self
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
- end
10
-
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
- end
14
-
15
- private
16
-
17
- def parse_params(*args)
18
- if args.first.respond_to?(:to_h) && !args.first.is_a?(Array)
19
- query_params = []
20
- supplied_params = args.first.to_h
21
- else
22
- query_params = Array(args.shift)
23
- supplied_params = args.first || {}
24
- end
25
-
26
- [query_params, supplied_params]
27
- end
28
-
29
- def build_template(type, helper_name, query_params, supplied_params)
30
- return unless named_routes.names.include?(helper_name)
31
-
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
-
35
- unescape_template(escaped_template)
36
- end
37
-
38
- def append_query_params(template, query_params, supplied_params)
39
- return template unless query_params.any?
40
-
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(',')}}"
59
- end
60
-
61
- def expand_helper(helper_name, path_or_url, params)
62
- helper_method = "#{helper_name}_#{path_or_url}"
63
-
64
- url_helpers.send(helper_method, *templated_supplied_params_for(helper_name, params))
65
- end
66
-
67
- def named_routes
68
- Rails.application.routes.named_routes
69
- end
70
-
71
- def route_object_for(helper_name)
72
- named_routes.routes[helper_name]
73
- end
74
-
75
- def templated_supplied_params_for(helper_name, params)
76
- route_object_for(helper_name).required_parts.map do |required_part|
77
- params.fetch(required_part) { "{#{required_part}}" }
78
- end
79
- end
6
+ @@parser = Parser.new
80
7
 
81
- def unescape_template(template)
82
- CGI.unescape(template)
8
+ def path_template_for(helper_name, query_params = {}, supplied_params = nil)
9
+ @@parser.path_template_for(helper_name, query_params, supplied_params)
83
10
  end
84
11
 
85
- def url_helpers
86
- Rails.application.routes.url_helpers
12
+ def url_template_for(helper_name, query_params = {}, supplied_params = nil)
13
+ @@parser.url_template_for(helper_name, query_params, supplied_params)
87
14
  end
88
15
  end
89
16
  end
@@ -0,0 +1,83 @@
1
+ require "rails"
2
+ require "zooplankton/resolver"
3
+
4
+ module Zooplankton
5
+ class Parser
6
+ AMPERSAND = '&'.freeze
7
+ QUESTION_MARK = '?'.freeze
8
+
9
+ def path_template_for(helper_name, query_params={}, supplied_params=nil)
10
+ build_template(:path, helper_name, *parse_params(query_params, supplied_params))
11
+ end
12
+
13
+ def url_template_for(helper_name, query_params={}, supplied_params=nil)
14
+ build_template(:url, helper_name, *parse_params(query_params, supplied_params))
15
+ end
16
+
17
+ private
18
+
19
+ def parse_params(*args)
20
+ if args.first.respond_to?(:to_h) && !args.first.is_a?(Array)
21
+ query_params = []
22
+ supplied_params = args.first.to_h
23
+ else
24
+ query_params = Array(args.shift)
25
+ supplied_params = args.first || {}
26
+ end
27
+
28
+ [query_params, supplied_params]
29
+ end
30
+
31
+ def build_template(type, helper_name, query_params, supplied_params)
32
+ return unless resolver.has_route?(helper_name)
33
+
34
+ escaped_template_without_query_params = expand_helper(helper_name, type, supplied_params)
35
+ escaped_template = append_query_params(escaped_template_without_query_params, query_params, supplied_params)
36
+
37
+ unescape_template(escaped_template)
38
+ end
39
+
40
+ def append_query_params(template, defined_keys, supplied_params)
41
+ return template unless defined_keys.any?
42
+
43
+ supplied_keys = defined_keys & supplied_params.keys
44
+ not_supplied_keys = defined_keys - supplied_params.keys
45
+
46
+ segments = []
47
+ init_symbol = QUESTION_MARK
48
+
49
+ if supplied_keys.any?
50
+ segments << supplied_keys.map{|k| escaped_pair(k, supplied_params[k]) }.join(AMPERSAND)
51
+ end
52
+
53
+ if not_supplied_keys.any?
54
+ init_symbol, sep = if segments.size > 0
55
+ [QUESTION_MARK, AMPERSAND]
56
+ else
57
+ ['', QUESTION_MARK]
58
+ end
59
+ segments << "{#{sep}#{not_supplied_keys.join(",")}}"
60
+ end
61
+
62
+ "#{template}#{init_symbol}#{segments.join}"
63
+ end
64
+
65
+ def expand_helper(helper_name, path_or_url, params)
66
+ helper_method = "#{helper_name}_#{path_or_url}"
67
+
68
+ resolver.generate(helper_method, helper_name, params)
69
+ end
70
+
71
+ def resolver
72
+ @resolver ||= Resolver.instance
73
+ end
74
+
75
+ def escaped_pair(key, value)
76
+ [key, URI.encode(value.to_s)].join('=')
77
+ end
78
+
79
+ def unescape_template(template)
80
+ CGI.unescape(template)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,39 @@
1
+ require 'action_controller/metal/strong_parameters'
2
+
3
+ module Zooplankton
4
+ class Resolver
5
+ def self.instance
6
+ new(Rails.application.routes)
7
+ end
8
+
9
+ def initialize(routes)
10
+ @routes = routes
11
+ end
12
+
13
+ def has_route?(name)
14
+ names.include?(name)
15
+ end
16
+
17
+ def generate(helper_method, helper_name, params = {})
18
+ route = named_routes[helper_name]
19
+ routes.url_helpers.public_send(helper_method, *tokenized_params_for(route, params))
20
+ end
21
+
22
+ private
23
+ attr_reader :routes
24
+
25
+ def tokenized_params_for(route, params)
26
+ route.required_parts.map do |required_part|
27
+ params.fetch(required_part) { "{#{required_part}}" }
28
+ end
29
+ end
30
+
31
+ def names
32
+ named_routes.names
33
+ end
34
+
35
+ def named_routes
36
+ routes.named_routes
37
+ end
38
+ end
39
+ end
@@ -1,3 +1,3 @@
1
1
  module Zooplankton
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -39,6 +39,14 @@ describe Zooplankton do
39
39
  expect(subject).to eq('/{?q,f}')
40
40
  end
41
41
  end
42
+
43
+ context "with all query parameters" do
44
+ subject { Zooplankton.path_template_for(:root, %w(s q aa), "s" => "foo", "q" => "bar", "aa" => "hello", "ignore" => "me") }
45
+
46
+ it "templateizes all query params with the right separators" do
47
+ expect(subject).to eq('/?s=foo&q=bar&aa=hello')
48
+ end
49
+ end
42
50
  end
43
51
 
44
52
  context "for a route with one required param" do
@@ -23,5 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "pry"
24
24
  spec.add_development_dependency "rspec", "~> 2.14"
25
25
 
26
- spec.add_dependency "rails", "~> 4.0"
26
+ spec.add_dependency "rails", [">= 4.0", "< 6.0"]
27
27
  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.2.0
4
+ version: 1.3.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: 2014-07-16 00:00:00.000000000 Z
11
+ date: 2016-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,16 +70,22 @@ dependencies:
70
70
  name: rails
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '4.0'
76
+ - - "<"
77
+ - !ruby/object:Gem::Version
78
+ version: '6.0'
76
79
  type: :runtime
77
80
  prerelease: false
78
81
  version_requirements: !ruby/object:Gem::Requirement
79
82
  requirements:
80
- - - "~>"
83
+ - - ">="
81
84
  - !ruby/object:Gem::Version
82
85
  version: '4.0'
86
+ - - "<"
87
+ - !ruby/object:Gem::Version
88
+ version: '6.0'
83
89
  description: A library for turning Rails routes into URI Templates, like maybe for
84
90
  HAL.
85
91
  email:
@@ -91,12 +97,17 @@ files:
91
97
  - ".gitignore"
92
98
  - ".rspec"
93
99
  - ".travis.yml"
100
+ - Appraisals
94
101
  - CHANGES.md
95
102
  - Gemfile
96
103
  - LICENSE.txt
97
104
  - README.md
98
105
  - Rakefile
106
+ - gemfiles/rails4.gemfile
107
+ - gemfiles/rails5.gemfile
99
108
  - lib/zooplankton.rb
109
+ - lib/zooplankton/parser.rb
110
+ - lib/zooplankton/resolver.rb
100
111
  - lib/zooplankton/version.rb
101
112
  - spec/spec_helper.rb
102
113
  - spec/zooplankton_spec.rb
@@ -121,11 +132,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
132
  version: '0'
122
133
  requirements: []
123
134
  rubyforge_project:
124
- rubygems_version: 2.2.2
135
+ rubygems_version: 2.5.1
125
136
  signing_key:
126
137
  specification_version: 4
127
138
  summary: A library for turning Rails routes into URI Templates.
128
139
  test_files:
129
140
  - spec/spec_helper.rb
130
141
  - spec/zooplankton_spec.rb
131
- has_rdoc: