grape-route-helpers 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/Gemfile.lock +1 -1
- data/README.md +3 -3
- data/lib/grape-route-helpers/decorated_route.rb +16 -4
- data/lib/grape-route-helpers/version.rb +1 -1
- data/lib/tasks/grape_route_helpers.rake +1 -1
- data/spec/grape_route_helpers/decorated_route_spec.rb +13 -5
- data/spec/grape_route_helpers/named_route_matcher_spec.rb +3 -3
- 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: 7faecd03e24a18fca8562818d59b4013fd20e7a0
|
4
|
+
data.tar.gz: 59813a8cd72952eed995fe2b4e5111a64ad7312e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2412a7a01687166ba0730618233df42c34efb7f90500183543acd4fe09195879421e4029c34610e8cb64448e2bf338fe54fc73c6728571aede71fa0f46f8909
|
7
|
+
data.tar.gz: 13d916287f03d1a7b3656c3e669786cda7ef8dd28fd827681d3ae3c71b97aeb11b37aef85c70d11ec5b0ab68ae956636b1c054039f571f42c2ca660036de6f88
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## June 28 2015
|
4
|
+
|
5
|
+
Release 1.0.1
|
6
|
+
|
7
|
+
* Rename rake task from `grape:routes` to `grape:route_helpers`
|
8
|
+
|
9
|
+
* If a Grape::Route has a specific format (json, etc.) in its route_path attribute, the helper will return a path with this extension at the end
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -87,13 +87,13 @@ You'd have the following methods available inside your Grape API actions:
|
|
87
87
|
|
88
88
|
```ruby
|
89
89
|
# specifying the version when using Grape's "path" versioning strategy
|
90
|
-
api_v1_ping_path # => '/api/v1/ping'
|
90
|
+
api_v1_ping_path # => '/api/v1/ping.json'
|
91
91
|
|
92
92
|
# specifying the format
|
93
|
-
api_v1_cats_path(format: 'xml') # => '/api/v1/cats.xml'
|
93
|
+
api_v1_cats_path(format: '.xml') # => '/api/v1/cats.xml'
|
94
94
|
|
95
95
|
# passing in values required to build a path
|
96
|
-
api_v1_cats_path(id: 1) # => '/api/v1/cats/1'
|
96
|
+
api_v1_cats_path(id: 1) # => '/api/v1/cats/1.json'
|
97
97
|
|
98
98
|
# catch-all paths have helpers
|
99
99
|
api_v1_anything_path # => '/api/v1/*anything'
|
@@ -1,18 +1,31 @@
|
|
1
1
|
module GrapeRouteHelpers
|
2
2
|
# wrapper around Grape::Route that adds a helper method
|
3
3
|
class DecoratedRoute
|
4
|
-
attr_reader :route, :helper_names, :helper_arguments
|
4
|
+
attr_reader :route, :helper_names, :helper_arguments, :extension
|
5
5
|
|
6
6
|
def initialize(route)
|
7
7
|
@route = route
|
8
8
|
@helper_names = []
|
9
9
|
@helper_arguments = required_helper_segments
|
10
|
+
@extension = default_extension
|
10
11
|
define_path_helpers
|
11
12
|
end
|
12
13
|
|
14
|
+
def default_extension
|
15
|
+
pattern = /\((\.\:?\w+)\)$/
|
16
|
+
match = route_path.match(pattern)
|
17
|
+
return '' unless match
|
18
|
+
ext = match.captures.first
|
19
|
+
if ext == '.:format'
|
20
|
+
''
|
21
|
+
else
|
22
|
+
ext
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
13
26
|
def define_path_helpers
|
14
27
|
route_versions.each do |version|
|
15
|
-
route_attributes = { version: version }
|
28
|
+
route_attributes = { version: version, format: extension }
|
16
29
|
method_name = path_helper_name(route_attributes)
|
17
30
|
@helper_names << method_name
|
18
31
|
define_path_helper(method_name, route_attributes)
|
@@ -28,9 +41,8 @@ module GrapeRouteHelpers
|
|
28
41
|
|
29
42
|
content_type = attrs.delete(:format)
|
30
43
|
path = '/' + path_segments_with_values(attrs).join('/')
|
31
|
-
extension = content_type ? '.' + content_type : ''
|
32
44
|
|
33
|
-
path +
|
45
|
+
path + content_type
|
34
46
|
end
|
35
47
|
RUBY
|
36
48
|
instance_eval method_body
|
@@ -132,10 +132,18 @@ describe GrapeRouteHelpers::DecoratedRoute do
|
|
132
132
|
end
|
133
133
|
|
134
134
|
describe 'path helper method' do
|
135
|
+
# handle different Grape::Route#route_path formats in Grape 0.12.0
|
136
|
+
context 'when route_path contains a specific format' do
|
137
|
+
it 'returns the correct path with the correct format' do
|
138
|
+
path = index_route.api_v1_cats_path
|
139
|
+
expect(path).to eq('/api/v1/cats.json')
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
135
143
|
context 'when helper does not require arguments' do
|
136
144
|
it 'returns the correct path' do
|
137
145
|
path = index_route.api_v1_cats_path
|
138
|
-
expect(path).to eq('/api/v1/cats')
|
146
|
+
expect(path).to eq('/api/v1/cats.json')
|
139
147
|
end
|
140
148
|
end
|
141
149
|
|
@@ -143,7 +151,7 @@ describe GrapeRouteHelpers::DecoratedRoute do
|
|
143
151
|
context 'when not missing arguments' do
|
144
152
|
it 'returns the correct path' do
|
145
153
|
path = show_route.api_v1_cats_path(id: 1)
|
146
|
-
expect(path).to eq('/api/v1/cats/1')
|
154
|
+
expect(path).to eq('/api/v1/cats/1.json')
|
147
155
|
end
|
148
156
|
end
|
149
157
|
end
|
@@ -154,14 +162,14 @@ describe GrapeRouteHelpers::DecoratedRoute do
|
|
154
162
|
end
|
155
163
|
|
156
164
|
it 'returns a path for each version' do
|
157
|
-
expect(index_route.api_v1_cats_path).to eq('/api/v1/cats')
|
158
|
-
expect(index_route.api_v2_cats_path).to eq('/api/v2/cats')
|
165
|
+
expect(index_route.api_v1_cats_path).to eq('/api/v1/cats.json')
|
166
|
+
expect(index_route.api_v2_cats_path).to eq('/api/v2/cats.json')
|
159
167
|
end
|
160
168
|
end
|
161
169
|
|
162
170
|
context 'when a format is given' do
|
163
171
|
it 'returns the path with a correct extension' do
|
164
|
-
path = show_route.api_v1_cats_path(id: 1, format: 'xml')
|
172
|
+
path = show_route.api_v1_cats_path(id: 1, format: '.xml')
|
165
173
|
expect(path).to eq('/api/v1/cats/1.xml')
|
166
174
|
end
|
167
175
|
end
|
@@ -123,7 +123,7 @@ describe GrapeRouteHelpers::NamedRouteMatcher do
|
|
123
123
|
api
|
124
124
|
|
125
125
|
path = api_v1_ping_path
|
126
|
-
expect(path).to eq('/api/v1/ping')
|
126
|
+
expect(path).to eq('/api/v1/ping.json')
|
127
127
|
end
|
128
128
|
|
129
129
|
context 'when argument to the helper is not a hash' do
|
@@ -154,10 +154,10 @@ describe GrapeRouteHelpers::NamedRouteMatcher do
|
|
154
154
|
api
|
155
155
|
|
156
156
|
show_path = api_v1_cats_path('id' => 1)
|
157
|
-
expect(show_path).to eq('/api/v1/cats/1')
|
157
|
+
expect(show_path).to eq('/api/v1/cats/1.json')
|
158
158
|
|
159
159
|
index_path = api_v1_cats_path
|
160
|
-
expect(index_path).to eq('/api/v1/cats')
|
160
|
+
expect(index_path).to eq('/api/v1/cats.json')
|
161
161
|
end
|
162
162
|
end
|
163
163
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-route-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Harper Henn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grape
|
@@ -102,6 +102,7 @@ extra_rdoc_files: []
|
|
102
102
|
files:
|
103
103
|
- .gitignore
|
104
104
|
- .rubocop.yml
|
105
|
+
- CHANGELOG.md
|
105
106
|
- Gemfile
|
106
107
|
- Gemfile.lock
|
107
108
|
- LICENSE.txt
|