grape-path-helpers 1.0.5 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitlab-ci.yml +3 -3
- data/CHANGELOG.md +20 -0
- data/Gemfile.lock +43 -32
- data/LICENSE.txt +2 -1
- data/README.md +12 -4
- data/Rakefile +1 -0
- data/grape-path-helpers.gemspec +2 -2
- data/lib/grape-path-helpers.rb +1 -1
- data/lib/grape-path-helpers/all_routes.rb +1 -1
- data/lib/grape-path-helpers/decorated_route.rb +20 -7
- data/lib/grape-path-helpers/named_route_matcher.rb +2 -2
- data/lib/grape-path-helpers/route_displayer.rb +1 -1
- data/lib/grape-path-helpers/version.rb +1 -1
- data/spec/grape_path_helpers/all_routes_spec.rb +12 -3
- data/spec/grape_path_helpers/decorated_route_spec.rb +146 -2
- data/spec/grape_path_helpers/named_route_matcher_spec.rb +6 -6
- data/spec/support/api.rb +22 -5
- metadata +7 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e6a9a810d64b890207e58f89f7da7a104827f733953c85e8487d10f45caf14d6
|
4
|
+
data.tar.gz: b76980cef67c4967fc4b4f49bb736603e67d74579624738ed5affd11317fa681
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 990b3eebe65bae4d982c98900e7a44d9b5a5b992383354bb696612fd27ba90c1738ae28c06c3f679ca854098e03bfbdc2fa30a3471f1e7da03a19006f5262b2e
|
7
|
+
data.tar.gz: 27e9befba4f4a89a61372bceadc45c27f55d492012cdfa56519e1aed030690c1cdb97591635f317baf225ef6a63788adbcd091e7e37c3abd4ef919ee938bb177
|
data/.gitlab-ci.yml
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
image: "ruby:2.
|
1
|
+
image: "ruby:2.6-alpine"
|
2
2
|
|
3
3
|
cache:
|
4
4
|
paths:
|
@@ -7,11 +7,11 @@ cache:
|
|
7
7
|
before_script:
|
8
8
|
- apk add --update git
|
9
9
|
- gem install bundler
|
10
|
-
- bundle install
|
10
|
+
- bundle install --path vendor/ruby
|
11
11
|
|
12
12
|
rubocop:
|
13
13
|
script:
|
14
|
-
- rubocop
|
14
|
+
- bundle exec rubocop
|
15
15
|
|
16
16
|
rspec:
|
17
17
|
script:
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,25 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 1.4.0
|
4
|
+
|
5
|
+
* [Support using a base class other than Grape::API::Instance](https://gitlab.com/gitlab-org/grape-path-helpers/-/merge_requests/23)
|
6
|
+
|
7
|
+
## 1.3.0
|
8
|
+
|
9
|
+
* [Upgrade to Grape 1.3.1](https://gitlab.com/gitlab-org/grape-path-helpers/-/merge_requests/21)
|
10
|
+
|
11
|
+
## 1.2.0
|
12
|
+
|
13
|
+
* [Add wildcard segments support](https://gitlab.com/gitlab-org/grape-path-helpers/merge_requests/16)
|
14
|
+
|
15
|
+
## 1.1.0
|
16
|
+
|
17
|
+
* [Relax dependency on ActiveSupport](https://gitlab.com/gitlab-org/grape-path-helpers/merge_requests/12)
|
18
|
+
|
19
|
+
## 1.0.6
|
20
|
+
|
21
|
+
* [Fix segments parsing for optional segments](https://gitlab.com/gitlab-org/grape-path-helpers/merge_requests/10)
|
22
|
+
|
3
23
|
## 1.0.5
|
4
24
|
|
5
25
|
* [Relax dependencies](https://gitlab.com/gitlab-org/grape-path-helpers/merge_requests/9)
|
data/Gemfile.lock
CHANGED
@@ -1,48 +1,62 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
grape-path-helpers (1.0
|
5
|
-
activesupport
|
6
|
-
grape (~> 1.
|
4
|
+
grape-path-helpers (1.4.0)
|
5
|
+
activesupport
|
6
|
+
grape (~> 1.3)
|
7
7
|
rake (~> 12)
|
8
8
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
|
-
activesupport (
|
12
|
+
activesupport (6.0.3.4)
|
13
13
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
14
|
i18n (>= 0.7, < 2)
|
15
15
|
minitest (~> 5.1)
|
16
16
|
tzinfo (~> 1.1)
|
17
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
17
18
|
ast (2.4.0)
|
18
|
-
|
19
|
-
descendants_tracker (~> 0.0.4)
|
20
|
-
ice_nine (~> 0.11.0)
|
21
|
-
thread_safe (~> 0.3, >= 0.3.1)
|
22
|
-
builder (3.2.3)
|
19
|
+
builder (3.2.4)
|
23
20
|
coderay (1.1.2)
|
24
|
-
|
25
|
-
descendants_tracker (~> 0.0.1)
|
26
|
-
concurrent-ruby (1.0.5)
|
27
|
-
descendants_tracker (0.0.4)
|
28
|
-
thread_safe (~> 0.3, >= 0.3.1)
|
21
|
+
concurrent-ruby (1.1.7)
|
29
22
|
diff-lcs (1.3)
|
30
|
-
|
31
|
-
|
23
|
+
dry-configurable (0.11.6)
|
24
|
+
concurrent-ruby (~> 1.0)
|
25
|
+
dry-core (~> 0.4, >= 0.4.7)
|
26
|
+
dry-equalizer (~> 0.2)
|
27
|
+
dry-container (0.7.2)
|
28
|
+
concurrent-ruby (~> 1.0)
|
29
|
+
dry-configurable (~> 0.1, >= 0.1.3)
|
30
|
+
dry-core (0.4.9)
|
31
|
+
concurrent-ruby (~> 1.0)
|
32
|
+
dry-equalizer (0.3.0)
|
33
|
+
dry-inflector (0.2.0)
|
34
|
+
dry-logic (1.0.8)
|
35
|
+
concurrent-ruby (~> 1.0)
|
36
|
+
dry-core (~> 0.2)
|
37
|
+
dry-equalizer (~> 0.2)
|
38
|
+
dry-types (1.4.0)
|
39
|
+
concurrent-ruby (~> 1.0)
|
40
|
+
dry-container (~> 0.3)
|
41
|
+
dry-core (~> 0.4, >= 0.4.4)
|
42
|
+
dry-equalizer (~> 0.3)
|
43
|
+
dry-inflector (~> 0.1, >= 0.1.2)
|
44
|
+
dry-logic (~> 1.0, >= 1.0.2)
|
45
|
+
grape (1.5.0)
|
32
46
|
activesupport
|
33
47
|
builder
|
48
|
+
dry-types (>= 1.1)
|
34
49
|
mustermann-grape (~> 1.0.0)
|
35
50
|
rack (>= 1.3.0)
|
36
51
|
rack-accept
|
37
|
-
|
38
|
-
i18n (1.0.1)
|
52
|
+
i18n (1.8.5)
|
39
53
|
concurrent-ruby (~> 1.0)
|
40
|
-
ice_nine (0.11.2)
|
41
54
|
method_source (0.9.0)
|
42
|
-
minitest (5.
|
43
|
-
mustermann (1.
|
44
|
-
|
45
|
-
|
55
|
+
minitest (5.14.2)
|
56
|
+
mustermann (1.1.1)
|
57
|
+
ruby2_keywords (~> 0.0.1)
|
58
|
+
mustermann-grape (1.0.1)
|
59
|
+
mustermann (>= 1.0.0)
|
46
60
|
parallel (1.12.1)
|
47
61
|
parser (2.5.1.0)
|
48
62
|
ast (~> 2.4.0)
|
@@ -50,11 +64,11 @@ GEM
|
|
50
64
|
pry (0.11.3)
|
51
65
|
coderay (~> 1.1.0)
|
52
66
|
method_source (~> 0.9.0)
|
53
|
-
rack (2.
|
67
|
+
rack (2.2.3)
|
54
68
|
rack-accept (0.4.5)
|
55
69
|
rack (>= 0.4)
|
56
70
|
rainbow (3.0.0)
|
57
|
-
rake (12.3.
|
71
|
+
rake (12.3.3)
|
58
72
|
rspec (3.7.0)
|
59
73
|
rspec-core (~> 3.7.0)
|
60
74
|
rspec-expectations (~> 3.7.0)
|
@@ -76,15 +90,12 @@ GEM
|
|
76
90
|
ruby-progressbar (~> 1.7)
|
77
91
|
unicode-display_width (~> 1.0, >= 1.0.1)
|
78
92
|
ruby-progressbar (1.9.0)
|
93
|
+
ruby2_keywords (0.0.2)
|
79
94
|
thread_safe (0.3.6)
|
80
|
-
tzinfo (1.2.
|
95
|
+
tzinfo (1.2.7)
|
81
96
|
thread_safe (~> 0.1)
|
82
97
|
unicode-display_width (1.3.3)
|
83
|
-
|
84
|
-
axiom-types (~> 0.1)
|
85
|
-
coercible (~> 1.0)
|
86
|
-
descendants_tracker (~> 0.0, >= 0.0.3)
|
87
|
-
equalizer (~> 0.0, >= 0.0.9)
|
98
|
+
zeitwerk (2.4.0)
|
88
99
|
|
89
100
|
PLATFORMS
|
90
101
|
ruby
|
@@ -96,4 +107,4 @@ DEPENDENCIES
|
|
96
107
|
rubocop (~> 0.56)
|
97
108
|
|
98
109
|
BUNDLED WITH
|
99
|
-
1.
|
110
|
+
1.17.3
|
data/LICENSE.txt
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
MIT License (MIT)
|
2
2
|
|
3
|
-
Copyright (c) 2015 Harper Henn
|
3
|
+
Copyright (c) 2015,2016 Harper Henn
|
4
|
+
Copyright (c) 2018-2020 GitLab B.V.
|
4
5
|
|
5
6
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
7
|
of this software and associated documentation files (the "Software"), to deal
|
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# grape-path-helpers
|
2
2
|
|
3
|
-
[![Build status](https://gitlab.com/gitlab-org/grape-path-helpers/badges/master/
|
3
|
+
[![Build status](https://gitlab.com/gitlab-org/grape-path-helpers/badges/master/pipeline.svg)](https://gitlab.com/gitlab-org/grape-path-helpers/commits/master)
|
4
4
|
|
5
5
|
Provides named route helpers for Grape APIs, similar to [Rails' route helpers](http://edgeguides.rubyonrails.org/routing.html#path-and-url-helpers).
|
6
6
|
|
7
|
-
**This is a fork and rename of [
|
7
|
+
**This is a fork and rename of [grape-route-helpers](https://github.com/reprah/grape-route-helpers). It [includes some fixes](CHANGELOG.md) needed for GitLab.**
|
8
8
|
|
9
9
|
### Installation
|
10
10
|
|
@@ -76,7 +76,7 @@ You can use helper methods in your REPL session by including a module:
|
|
76
76
|
Use the methods inside your Grape API actions. Given this example API:
|
77
77
|
|
78
78
|
```ruby
|
79
|
-
class ExampleAPI < Grape::API
|
79
|
+
class ExampleAPI < Grape::API::Instance
|
80
80
|
version 'v1'
|
81
81
|
prefix 'api'
|
82
82
|
format 'json'
|
@@ -127,7 +127,7 @@ api_v1_anything_path # => '/api/v1/*anything'
|
|
127
127
|
If you want to assign a custom helper name to a route, pass the `:as` option when creating your route in your API:
|
128
128
|
|
129
129
|
```ruby
|
130
|
-
class Base < Grape::API
|
130
|
+
class Base < Grape::API::Instance
|
131
131
|
get 'ping', as: 'is_the_server_running'
|
132
132
|
'pong'
|
133
133
|
end
|
@@ -169,6 +169,14 @@ end
|
|
169
169
|
|
170
170
|
6.) Create a new pull request
|
171
171
|
|
172
|
+
### Releasing
|
173
|
+
|
174
|
+
1. Update the [CHANGELOG](CHANGELOG.md).
|
175
|
+
2. Update the version in [lib/grape-path-helpers/version.rb](lib/grape-path-helpers/version.rb).
|
176
|
+
3. [Tag](https://gitlab.com/gitlab-org/grape-path-helpers/-/tags) the commit with the version number prefixed by 'v'.
|
177
|
+
4. Run `gem build grape-path-helpers` locally.
|
178
|
+
5. Run `gem push $new_file.gem` locally, where `$new_file` is the file generated by the step above.
|
179
|
+
|
172
180
|
### License
|
173
181
|
|
174
182
|
See LICENSE
|
data/Rakefile
CHANGED
data/grape-path-helpers.gemspec
CHANGED
@@ -11,8 +11,8 @@ Gem::Specification.new do |gem|
|
|
11
11
|
gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
12
12
|
gem.homepage = 'https://gitlab.com/gitlab-org/grape-path-helpers'
|
13
13
|
|
14
|
-
gem.add_runtime_dependency 'activesupport'
|
15
|
-
gem.add_runtime_dependency 'grape', '~> 1.
|
14
|
+
gem.add_runtime_dependency 'activesupport'
|
15
|
+
gem.add_runtime_dependency 'grape', '~> 1.3'
|
16
16
|
gem.add_runtime_dependency 'rake', '~> 12'
|
17
17
|
|
18
18
|
gem.add_development_dependency 'pry', '~> 0.11'
|
data/lib/grape-path-helpers.rb
CHANGED
@@ -12,5 +12,5 @@ module GrapePathHelpers
|
|
12
12
|
require 'grape-path-helpers/railtie' if defined?(Rails)
|
13
13
|
end
|
14
14
|
|
15
|
-
Grape::API.extend GrapePathHelpers::AllRoutes
|
15
|
+
Grape::API::Instance.extend GrapePathHelpers::AllRoutes
|
16
16
|
Grape::Endpoint.send(:include, GrapePathHelpers::NamedRouteMatcher)
|
@@ -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
|
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 =
|
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,11 @@ module GrapePathHelpers
|
|
117
126
|
end
|
118
127
|
|
119
128
|
def dynamic_segment?(segment)
|
120
|
-
segment.start_with?(':')
|
129
|
+
segment.start_with?(':', '*')
|
130
|
+
end
|
131
|
+
|
132
|
+
def optional_segment?(segment)
|
133
|
+
segment.start_with?('(')
|
121
134
|
end
|
122
135
|
|
123
136
|
def required_helper_segments
|
@@ -8,7 +8,7 @@ module GrapePathHelpers
|
|
8
8
|
|
9
9
|
segments = arguments.first || {}
|
10
10
|
|
11
|
-
route = Grape::API.decorated_routes.detect do |r|
|
11
|
+
route = Grape::API::Instance.decorated_routes.detect do |r|
|
12
12
|
route_match?(r, method_id, segments)
|
13
13
|
end
|
14
14
|
|
@@ -22,7 +22,7 @@ module GrapePathHelpers
|
|
22
22
|
def respond_to_missing?(method_name, _include_private = false)
|
23
23
|
return super unless method_name =~ /_path$/
|
24
24
|
|
25
|
-
Grape::API.decorated_routes.detect do |route|
|
25
|
+
Grape::API::Instance.decorated_routes.detect do |route|
|
26
26
|
return true if route.respond_to?(method_name)
|
27
27
|
end
|
28
28
|
|
@@ -3,7 +3,7 @@ module GrapePathHelpers
|
|
3
3
|
# and required arguments for every Grape::Route.
|
4
4
|
class RouteDisplayer
|
5
5
|
def route_attributes
|
6
|
-
Grape::API.decorated_routes.map do |route|
|
6
|
+
Grape::API::Instance.decorated_routes.map do |route|
|
7
7
|
{
|
8
8
|
route_path: route.route_path,
|
9
9
|
route_method: route.route_method,
|
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
# rubocop:disable Metrics/BlockLength
|
3
4
|
describe GrapePathHelpers::AllRoutes do
|
4
|
-
Grape::API.extend described_class
|
5
|
+
Grape::API::Instance.extend described_class
|
5
6
|
|
6
7
|
describe '#all_routes' do
|
7
8
|
context 'when API is mounted within another API' do
|
@@ -11,7 +12,7 @@ describe GrapePathHelpers::AllRoutes do
|
|
11
12
|
mounting_api
|
12
13
|
|
13
14
|
# A route is unique if no other route shares the same set of options
|
14
|
-
all_route_options = Grape::API.all_routes.map do |r|
|
15
|
+
all_route_options = Grape::API::Instance.all_routes.map do |r|
|
15
16
|
r.instance_variable_get(:@options).merge(path: r.path)
|
16
17
|
end
|
17
18
|
|
@@ -28,10 +29,18 @@ describe GrapePathHelpers::AllRoutes do
|
|
28
29
|
it 'returns all POST routes' do
|
29
30
|
expected_routes = Spec::Support::MultiplePostsAPI.routes.map(&:path)
|
30
31
|
|
31
|
-
all_routes = Grape::API.all_routes
|
32
|
+
all_routes = Grape::API::Instance.all_routes
|
32
33
|
expect(all_routes.map(&:path)).to include(*expected_routes)
|
33
34
|
end
|
34
35
|
end
|
36
|
+
|
37
|
+
context 'when an API is created via an intermediate class' do
|
38
|
+
it 'includes those routes on both Grape::API::Instance and the base class' do
|
39
|
+
expect(Spec::Support::BaseAPI.all_routes.map(&:origin)).to include('/derived_ping')
|
40
|
+
expect(Grape::API::Instance.all_routes.map(&:origin)).to include('/derived_ping')
|
41
|
+
end
|
42
|
+
end
|
35
43
|
# rubocop:enable Metrics/LineLength
|
36
44
|
end
|
37
45
|
end
|
46
|
+
# rubocop:enable Metrics/BlockLength
|
@@ -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
|
@@ -30,6 +30,14 @@ describe GrapePathHelpers::DecoratedRoute do
|
|
30
30
|
routes.detect { |route| route.route_path =~ /ping/ }
|
31
31
|
end
|
32
32
|
|
33
|
+
let(:optional_route) do
|
34
|
+
routes.detect { |route| route.route_path =~ /optional/ }
|
35
|
+
end
|
36
|
+
|
37
|
+
let(:wildcard_route) do
|
38
|
+
routes.detect { |route| route.route_path =~ /\*owner_ids/ }
|
39
|
+
end
|
40
|
+
|
33
41
|
describe '#sanitize_method_name' do
|
34
42
|
it 'removes characters that are illegal in Ruby method names' do
|
35
43
|
illegal_names = ['beta-1', 'name_with_+', 'name_with_(']
|
@@ -47,6 +55,53 @@ describe GrapePathHelpers::DecoratedRoute do
|
|
47
55
|
end
|
48
56
|
end
|
49
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
|
+
|
50
105
|
describe '#helper_names' do
|
51
106
|
context 'when a route is given a custom helper name' do
|
52
107
|
it 'uses the custom name instead of the dynamically generated one' do
|
@@ -104,9 +159,18 @@ describe GrapePathHelpers::DecoratedRoute do
|
|
104
159
|
end
|
105
160
|
end
|
106
161
|
|
162
|
+
describe '#path_segments' do
|
163
|
+
context 'when path has optional segments' do
|
164
|
+
it 'leaves them intact' do
|
165
|
+
result = optional_route.path_segments
|
166
|
+
expect(result).to eq(%w[api :version cats :id (-/) optional])
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
107
171
|
describe '#path_segments_with_values' do
|
108
172
|
context 'when path has dynamic segments' do
|
109
|
-
it 'replaces segments with corresponding values found in
|
173
|
+
it 'replaces segments with corresponding values found in options' do
|
110
174
|
opts = { id: 1 }
|
111
175
|
result = show_route.path_segments_with_values(opts)
|
112
176
|
expect(result).to include(1)
|
@@ -120,6 +184,74 @@ describe GrapePathHelpers::DecoratedRoute do
|
|
120
184
|
end
|
121
185
|
end
|
122
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
|
123
255
|
end
|
124
256
|
|
125
257
|
describe '#path_helper_name' do
|
@@ -152,6 +284,13 @@ describe GrapePathHelpers::DecoratedRoute do
|
|
152
284
|
expect(result).to eq('api_v1_path_path')
|
153
285
|
end
|
154
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
|
155
294
|
end
|
156
295
|
|
157
296
|
describe '#segment_to_value' do
|
@@ -237,5 +376,10 @@ describe GrapePathHelpers::DecoratedRoute do
|
|
237
376
|
end
|
238
377
|
end
|
239
378
|
end
|
379
|
+
|
380
|
+
describe '#optional_segment?' do
|
381
|
+
it { expect(index_route.optional_segment?('mandatory')).to eq(false) }
|
382
|
+
it { expect(index_route.optional_segment?('(optional/)')).to eq(true) }
|
383
|
+
end
|
240
384
|
end
|
241
385
|
# rubocop:enable Metrics/BlockLength
|
@@ -5,7 +5,7 @@ describe GrapePathHelpers::NamedRouteMatcher do
|
|
5
5
|
include described_class
|
6
6
|
|
7
7
|
let(:routes) do
|
8
|
-
Grape::API.decorated_routes
|
8
|
+
Grape::API::Instance.decorated_routes
|
9
9
|
end
|
10
10
|
|
11
11
|
let(:ping_route) do
|
@@ -44,13 +44,13 @@ describe GrapePathHelpers::NamedRouteMatcher do
|
|
44
44
|
|
45
45
|
describe '#method_missing' do
|
46
46
|
it 'returns super method_missing if the method does not end with path' do
|
47
|
-
expect(Grape::API).not_to receive(:decorated_routes)
|
47
|
+
expect(Grape::API::Instance).not_to receive(:decorated_routes)
|
48
48
|
|
49
49
|
helper_class.test_method
|
50
50
|
end
|
51
51
|
|
52
52
|
it 'search for the route if the method ends with path' do
|
53
|
-
expect(Grape::API).to receive(:decorated_routes).and_call_original
|
53
|
+
expect(Grape::API::Instance).to receive(:decorated_routes).and_call_original # rubocop:disable Metrics/LineLength
|
54
54
|
|
55
55
|
helper_class.test_method_path
|
56
56
|
end
|
@@ -146,14 +146,14 @@ describe GrapePathHelpers::NamedRouteMatcher do
|
|
146
146
|
end
|
147
147
|
|
148
148
|
describe '#respond_to_missing?' do
|
149
|
-
it 'returns super if the method
|
150
|
-
expect(Grape::API).not_to receive(:decorated_routes)
|
149
|
+
it 'returns super if the method does not end with path' do
|
150
|
+
expect(Grape::API::Instance).not_to receive(:decorated_routes) # rubocop:disable Metrics/LineLength
|
151
151
|
|
152
152
|
expect(helper_class.send(:respond_to_missing?, :test)).to eq(false)
|
153
153
|
end
|
154
154
|
|
155
155
|
it 'search for the route if the method ends with path' do
|
156
|
-
expect(Grape::API).to receive(:decorated_routes).and_call_original
|
156
|
+
expect(Grape::API::Instance).to receive(:decorated_routes).and_call_original # rubocop:disable Metrics/LineLength
|
157
157
|
|
158
158
|
expect(helper_class.send(:respond_to_missing?, :test_path)).to eq(false)
|
159
159
|
end
|
data/spec/support/api.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Spec
|
2
2
|
module Support
|
3
3
|
# Test API
|
4
|
-
class API < Grape::API
|
4
|
+
class API < Grape::API::Instance
|
5
5
|
version 'v1'
|
6
6
|
prefix 'api'
|
7
7
|
format 'json'
|
@@ -25,6 +25,10 @@ module Spec
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
+
get ':id/(-/)optional' do
|
29
|
+
'optional content'
|
30
|
+
end
|
31
|
+
|
28
32
|
get ':id/owners' do
|
29
33
|
%w[owner1 owner2]
|
30
34
|
end
|
@@ -32,6 +36,10 @@ module Spec
|
|
32
36
|
get ':id/owners/:owner_id' do
|
33
37
|
'owner'
|
34
38
|
end
|
39
|
+
|
40
|
+
get ':id/owners/*owner_ids/cats' do
|
41
|
+
%w[cats cats cats]
|
42
|
+
end
|
35
43
|
end
|
36
44
|
|
37
45
|
route :any, '*path' do
|
@@ -40,7 +48,7 @@ module Spec
|
|
40
48
|
end
|
41
49
|
|
42
50
|
# API with more than one version
|
43
|
-
class APIWithMultipleVersions < Grape::API
|
51
|
+
class APIWithMultipleVersions < Grape::API::Instance
|
44
52
|
version %w[beta alpha v1]
|
45
53
|
|
46
54
|
get 'ping' do
|
@@ -49,13 +57,13 @@ module Spec
|
|
49
57
|
end
|
50
58
|
|
51
59
|
# API with another API mounted inside it
|
52
|
-
class MountedAPI < Grape::API
|
60
|
+
class MountedAPI < Grape::API::Instance
|
53
61
|
mount Spec::Support::API
|
54
62
|
mount Spec::Support::APIWithMultipleVersions
|
55
63
|
end
|
56
64
|
|
57
65
|
# API with a version that would be illegal as a method name
|
58
|
-
class APIWithIllegalVersion < Grape::API
|
66
|
+
class APIWithIllegalVersion < Grape::API::Instance
|
59
67
|
version 'beta-1'
|
60
68
|
|
61
69
|
get 'ping' do
|
@@ -64,7 +72,7 @@ module Spec
|
|
64
72
|
end
|
65
73
|
|
66
74
|
# API with multiple POST routes
|
67
|
-
class MultiplePostsAPI < Grape::API
|
75
|
+
class MultiplePostsAPI < Grape::API::Instance
|
68
76
|
resource :hamlet do
|
69
77
|
post 'to_be' do
|
70
78
|
end
|
@@ -73,5 +81,14 @@ module Spec
|
|
73
81
|
end
|
74
82
|
end
|
75
83
|
end
|
84
|
+
|
85
|
+
class BaseAPI < Grape::API::Instance
|
86
|
+
end
|
87
|
+
|
88
|
+
class DerivedAPI < BaseAPI
|
89
|
+
get 'derived_ping' do
|
90
|
+
'pong'
|
91
|
+
end
|
92
|
+
end
|
76
93
|
end
|
77
94
|
end
|
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.0
|
4
|
+
version: 1.4.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:
|
12
|
+
date: 2020-10-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -17,34 +17,28 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '
|
21
|
-
- - "<"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: '5.1'
|
20
|
+
version: '0'
|
24
21
|
type: :runtime
|
25
22
|
prerelease: false
|
26
23
|
version_requirements: !ruby/object:Gem::Requirement
|
27
24
|
requirements:
|
28
25
|
- - ">="
|
29
26
|
- !ruby/object:Gem::Version
|
30
|
-
version: '
|
31
|
-
- - "<"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '5.1'
|
27
|
+
version: '0'
|
34
28
|
- !ruby/object:Gem::Dependency
|
35
29
|
name: grape
|
36
30
|
requirement: !ruby/object:Gem::Requirement
|
37
31
|
requirements:
|
38
32
|
- - "~>"
|
39
33
|
- !ruby/object:Gem::Version
|
40
|
-
version: '1.
|
34
|
+
version: '1.3'
|
41
35
|
type: :runtime
|
42
36
|
prerelease: false
|
43
37
|
version_requirements: !ruby/object:Gem::Requirement
|
44
38
|
requirements:
|
45
39
|
- - "~>"
|
46
40
|
- !ruby/object:Gem::Version
|
47
|
-
version: '1.
|
41
|
+
version: '1.3'
|
48
42
|
- !ruby/object:Gem::Dependency
|
49
43
|
name: rake
|
50
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -152,8 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
146
|
- !ruby/object:Gem::Version
|
153
147
|
version: '0'
|
154
148
|
requirements: []
|
155
|
-
|
156
|
-
rubygems_version: 2.5.2.2
|
149
|
+
rubygems_version: 3.0.3
|
157
150
|
signing_key:
|
158
151
|
specification_version: 4
|
159
152
|
summary: Route path helpers for Grape
|