grape-path-helpers 1.1.0 → 1.2.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
  SHA256:
3
- metadata.gz: e5de62b3e52c6df10f69653f7aad9f4c81a64298c4ad74eb0085b06985a98474
4
- data.tar.gz: bc29c3966d9f85de40b845d9e071fce2bc869d73776ce95078816c6f553a0494
3
+ metadata.gz: b434b7d1b56cd5b639816b364d237e3bd658b0cfe03c0d516c3135794a02bca6
4
+ data.tar.gz: 4d2958b9bb94e83fa61bb465ef564b100e1e1ff63dbb8748dbc821f4240b88e8
5
5
  SHA512:
6
- metadata.gz: 04bb77b1e0ee4b792ca6edc6ce1dfd1a18f9af85bc828f2e5f384f621f5ad42a7ad16e2712e9dfb50c8f8e2e8229e5947279122888169eeaeaf12d78860d64fc
7
- data.tar.gz: 0bfc95924b6ed43d02c39c6f77123c047122d2cfebc762745362273c837950e568fb22894008cd729de90a17373ea48d2dd933a8e9b85d5b7acdfab156aa836d
6
+ metadata.gz: dd5def728204b01e35b74e86d9c3b5115864b8e2ac1d1e912b0a7d6a623fda80a1ccbd855464f6cea73159280867eef46b33891aab34e883429dcb56051849be
7
+ data.tar.gz: d3c3dd5cace700e833448314055f7253ab93bf02af69ed70a4384f7e5176ac3914056cdf37000b3b1662fc9b0d23d5c1d8ab20d2bd147acb727edf7e41733958
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.2.0
4
+
5
+ * [Add wildcard segments support](https://gitlab.com/gitlab-org/grape-path-helpers/merge_requests/16)
6
+
3
7
  ## 1.1.0
4
8
 
5
9
  * [Relax dependency on ActiveSupport](https://gitlab.com/gitlab-org/grape-path-helpers/merge_requests/12)
@@ -4,6 +4,9 @@ module GrapePathHelpers
4
4
  attr_reader :route, :helper_names, :helper_arguments,
5
5
  :extension, :route_options
6
6
 
7
+ PATH_SEGMENTS_REGEXP = %r{\(/?\.:?\w+\)|/(?!\))|(?<=\))|\??\*}
8
+ PATH_SEGMENTS_WITH_WILDCARDS_REGEXP = %r{\(/?\.:?\w+\)|/(?!\))|(?<=\))|\?}
9
+
7
10
  def self.sanitize_method_name(string)
8
11
  string.gsub(/\W|^[0-9]/, '_')
9
12
  end
@@ -40,12 +43,12 @@ module GrapePathHelpers
40
43
 
41
44
  def define_path_helper(method_name, route_attributes)
42
45
  method_body = <<-RUBY
43
- def #{method_name}(attributes = {})
46
+ def #{method_name}(attributes = {}, include_wildcard_segments = false)
44
47
  attrs = #{route_attributes}.merge(attributes)
45
48
 
46
49
  query_params = attrs.delete(:params)
47
50
  content_type = attrs.delete(:format)
48
- path = '/' + path_segments_with_values(attrs).join('/')
51
+ path = '/' + path_segments_with_values(attrs, include_wildcard_segments).join('/')
49
52
 
50
53
  path + content_type + query_string(query_params)
51
54
  end
@@ -99,13 +102,19 @@ module GrapePathHelpers
99
102
  end
100
103
  end
101
104
 
102
- def path_segments_with_values(opts)
103
- segments = path_segments.map { |s| segment_to_value(s, opts) }
105
+ def path_segments_with_values(opts, include_wildcard_segments = false)
106
+ segments = path_segments(include_wildcard_segments).map do |s|
107
+ segment_to_value(s, opts)
108
+ end
104
109
  segments.reject(&:blank?)
105
110
  end
106
111
 
107
- def path_segments
108
- pattern = %r{\(/?\.:?\w+\)|/(?!\))|(?<=\))|\??\*}
112
+ def path_segments(include_wildcard_segments = false)
113
+ pattern = if include_wildcard_segments
114
+ PATH_SEGMENTS_WITH_WILDCARDS_REGEXP
115
+ else
116
+ PATH_SEGMENTS_REGEXP
117
+ end
109
118
  route_path.split(pattern).reject(&:blank?)
110
119
  end
111
120
 
@@ -117,7 +126,7 @@ module GrapePathHelpers
117
126
  end
118
127
 
119
128
  def dynamic_segment?(segment)
120
- segment.start_with?(':')
129
+ segment.start_with?(':', '*')
121
130
  end
122
131
 
123
132
  def optional_segment?(segment)
@@ -1,4 +1,4 @@
1
1
  # Gem version
2
2
  module GrapePathHelpers
3
- VERSION = '1.1.0'.freeze
3
+ VERSION = '1.2.0'.freeze
4
4
  end
@@ -19,7 +19,7 @@ describe GrapePathHelpers::DecoratedRoute do
19
19
  end
20
20
 
21
21
  let(:catch_all_route) do
22
- routes.detect { |route| route.route_path =~ /\*/ }
22
+ routes.detect { |route| route.route_path =~ /\*path/ }
23
23
  end
24
24
 
25
25
  let(:custom_route) do
@@ -34,6 +34,10 @@ describe GrapePathHelpers::DecoratedRoute do
34
34
  routes.detect { |route| route.route_path =~ /optional/ }
35
35
  end
36
36
 
37
+ let(:wildcard_route) do
38
+ routes.detect { |route| route.route_path =~ /\*owner_ids/ }
39
+ end
40
+
37
41
  describe '#sanitize_method_name' do
38
42
  it 'removes characters that are illegal in Ruby method names' do
39
43
  illegal_names = ['beta-1', 'name_with_+', 'name_with_(']
@@ -51,6 +55,53 @@ describe GrapePathHelpers::DecoratedRoute do
51
55
  end
52
56
  end
53
57
 
58
+ describe '#define_path_helper' do
59
+ context 'with only static segments' do
60
+ let(:route) { ping_route }
61
+
62
+ subject { route.api_v1_ping_path }
63
+
64
+ it { is_expected.to eq '/api/v1/ping.json' }
65
+ end
66
+
67
+ context 'with optional segments' do
68
+ let(:route) { optional_route }
69
+
70
+ subject { route.api_v1_cats______optional_path }
71
+
72
+ it { is_expected.to eq '/api/v1/cats/(-/)/optional.json' }
73
+ end
74
+
75
+ context 'with dynamic segments' do
76
+ let(:route) { show_route }
77
+
78
+ subject { route.api_v1_cats_path(id: 1) }
79
+
80
+ it { is_expected.to eq '/api/v1/cats/1.json' }
81
+ end
82
+
83
+ context 'with wildcard segments' do
84
+ let(:route) { wildcard_route }
85
+
86
+ context 'including them' do
87
+ subject do
88
+ route.api_v1_cats_owners_owner_ids_cats_path(
89
+ { owner_ids: 'foobar' },
90
+ true
91
+ )
92
+ end
93
+
94
+ it { is_expected.to eq '/api/v1/cats/owners/foobar/cats.json' }
95
+ end
96
+
97
+ context 'excluding them' do
98
+ subject { route.api_v1_cats_owners_owner_ids_cats_path }
99
+
100
+ it { is_expected.to eq '/api/v1/cats/owners/owner_ids/cats.json' }
101
+ end
102
+ end
103
+ end
104
+
54
105
  describe '#helper_names' do
55
106
  context 'when a route is given a custom helper name' do
56
107
  it 'uses the custom name instead of the dynamically generated one' do
@@ -119,7 +170,7 @@ describe GrapePathHelpers::DecoratedRoute do
119
170
 
120
171
  describe '#path_segments_with_values' do
121
172
  context 'when path has dynamic segments' do
122
- it 'replaces segments with corresponding values found in @options' do
173
+ it 'replaces segments with corresponding values found in options' do
123
174
  opts = { id: 1 }
124
175
  result = show_route.path_segments_with_values(opts)
125
176
  expect(result).to include(1)
@@ -133,6 +184,74 @@ describe GrapePathHelpers::DecoratedRoute do
133
184
  end
134
185
  end
135
186
  end
187
+
188
+ context 'when path has wildcard segments' do
189
+ context 'with regex excluding them' do
190
+ it "doesn't replaces wildcard segments" do
191
+ opts = { version: 4, id: 34, owner_ids: 'foobar' }
192
+
193
+ result = wildcard_route.path_segments_with_values(opts)
194
+ expect(result).to include(4)
195
+ expect(result).to include(34)
196
+ expect(result).to include('owner_ids')
197
+ expect(result).not_to include('foobar')
198
+ end
199
+
200
+ it "doesn't blank wildcard segments" do
201
+ opts = { version: 4, id: 34, owner_ids: '' }
202
+
203
+ result = wildcard_route.path_segments_with_values(opts)
204
+ expect(result).to include(4)
205
+ expect(result).to include(34)
206
+ expect(result).to include('owner_ids')
207
+ end
208
+ end
209
+
210
+ context 'with regex including them' do
211
+ context 'replaces segments' do
212
+ it 'with static text' do
213
+ opts = { version: 4, id: 34, owner_ids: 'foobar' }
214
+
215
+ result = wildcard_route.path_segments_with_values(opts, true)
216
+ expect(result).to include(4)
217
+ expect(result).to include(34)
218
+ expect(result).to include('foobar')
219
+ expect(result).not_to include('owner_ids')
220
+ end
221
+
222
+ it 'with additional segments' do
223
+ opts = { version: 4, id: 34, owner_ids: 'foo/bar/baz' }
224
+
225
+ result = wildcard_route.path_segments_with_values(opts, true)
226
+ expect(result).to include(4)
227
+ expect(result).to include(34)
228
+ expect(result).to include('foo/bar/baz')
229
+ expect(result).not_to include('owner_ids')
230
+ end
231
+ end
232
+
233
+ it 'blanks wildcard segments' do
234
+ opts = { version: 4, id: 34, owner_ids: '' }
235
+
236
+ result = wildcard_route.path_segments_with_values(opts, true)
237
+ expect(result).to include(4)
238
+ expect(result).to include(34)
239
+ expect(result).not_to include('owner_ids')
240
+ end
241
+
242
+ context 'when options contains string keys' do
243
+ it 'replaces segments' do
244
+ opts = { 'version' => 4, 'id' => 34, 'owner_ids' => 'foobar' }
245
+
246
+ result = wildcard_route.path_segments_with_values(opts, true)
247
+ expect(result).to include(4)
248
+ expect(result).to include(34)
249
+ expect(result).to include('foobar')
250
+ expect(result).not_to include('owner_ids')
251
+ end
252
+ end
253
+ end
254
+ end
136
255
  end
137
256
 
138
257
  describe '#path_helper_name' do
@@ -165,6 +284,13 @@ describe GrapePathHelpers::DecoratedRoute do
165
284
  expect(result).to eq('api_v1_path_path')
166
285
  end
167
286
  end
287
+
288
+ context 'when the path has a wildcard segment' do
289
+ it 'returns a name without the glob star' do
290
+ result = wildcard_route.path_helper_name
291
+ expect(result).to eq('api_v1_cats_owners_owner_ids_cats_path')
292
+ end
293
+ end
168
294
  end
169
295
 
170
296
  describe '#segment_to_value' do
@@ -36,6 +36,10 @@ module Spec
36
36
  get ':id/owners/:owner_id' do
37
37
  'owner'
38
38
  end
39
+
40
+ get ':id/owners/*owner_ids/cats' do
41
+ %w[cats cats cats]
42
+ end
39
43
  end
40
44
 
41
45
  route :any, '*path' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-path-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Drew Blessing
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-12-17 00:00:00.000000000 Z
12
+ date: 2020-01-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -146,8 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  - !ruby/object:Gem::Version
147
147
  version: '0'
148
148
  requirements: []
149
- rubyforge_project:
150
- rubygems_version: 2.7.6
149
+ rubygems_version: 3.0.3
151
150
  signing_key:
152
151
  specification_version: 4
153
152
  summary: Route path helpers for Grape