grape-route-helpers 1.0.0 → 1.0.1

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: 70edfbe7fe2afd10d2030ffbfec49dc642f57b40
4
- data.tar.gz: 86b00458bc103bbd7c953bf4842ccedae8d4f9cd
3
+ metadata.gz: 7faecd03e24a18fca8562818d59b4013fd20e7a0
4
+ data.tar.gz: 59813a8cd72952eed995fe2b4e5111a64ad7312e
5
5
  SHA512:
6
- metadata.gz: 5c98fd40561face8eb970ce8ac4d7de721835999a137f3ca6f25fd2761ac96160833d2c996f9d497a41202e8d51ea4c2cc7f95b17a13469d53c4f152c4b5dc8b
7
- data.tar.gz: d6519d1439e8e86d67469d3cbac343d7f033123dd673a488469e800f3fa2567a152f6225b993a28d719f6ee68ce278ddbd4085a8648c131193fae90a544f82c0
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- grape-route-helpers (1.0.0)
4
+ grape-route-helpers (1.0.1)
5
5
  activesupport
6
6
  grape
7
7
  rake
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 + extension
45
+ path + content_type
34
46
  end
35
47
  RUBY
36
48
  instance_eval method_body
@@ -1,4 +1,4 @@
1
1
  # Gem version
2
2
  module GrapeRouteHelpers
3
- VERSION = '1.0.0'
3
+ VERSION = '1.0.1'
4
4
  end
@@ -1,6 +1,6 @@
1
1
  namespace :grape do
2
2
  desc 'Print route helper methods.'
3
- task routes: :environment do
3
+ task route_helpers: :environment do
4
4
  GrapeRouteHelpers::RouteDisplayer.new.display
5
5
  end
6
6
  end
@@ -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.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-28 00:00:00.000000000 Z
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