grape-route-helpers 1.2.0 → 1.2.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: 6c89b2de6b727ea2d4e1953bca91338051e68e86
4
- data.tar.gz: e2706efbbd654e9b1e19cb9f43fa2dee2e03bd5d
3
+ metadata.gz: 7ac7c28ada79f5622bae6ba7893bb199e7b33fcb
4
+ data.tar.gz: 06ef83ad1b9cf2cb508f5fe113295cbb20673992
5
5
  SHA512:
6
- metadata.gz: 55a16e85e81fb667b224884f7d54a2e9c833728d62b37862769123e90f2caa4cbd5b123329efe83235c0de6e2b010bea93e8ceba7d6a2152a36b84ad2bb2fb10
7
- data.tar.gz: 50d1900fb057a31219f2fe58b7f2aaf683f5ed717391e6bd4428fd766f69925e2433feb1e221a5dc2ff3909a34b381e214a7243f316ce644af2b5c83bd23c6cc
6
+ metadata.gz: 3be39b62345e43228f2088b6b9728c14e709b8a0aca5a8e5d5b794b69a8fe02301266790a7cda1670fd90f820e205064401d0906cfe1d706425c76f07785e3d4
7
+ data.tar.gz: a91eb07644365db63eafbd180f13dedf13c76e2e18750ed5deb9b827891748f2d4ba012631aef99fb9180a2440c9361201ebbc6febb130df64f7a96eb86a98f9
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## October 11 2015
4
+
5
+ * Release 1.2.1
6
+ * Fixed issue #4
7
+
3
8
  ## September 27 2015
4
9
 
5
10
  * Release 1.2.0
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- grape-route-helpers (1.2.0)
4
+ grape-route-helpers (1.2.1)
5
5
  activesupport
6
6
  grape
7
7
  rake
data/README.md CHANGED
@@ -56,6 +56,13 @@ To see which methods correspond to which paths, and which options you can pass t
56
56
  $ rake grape:route_helpers
57
57
  ```
58
58
 
59
+ #### Use Helpers in IRB/Pry
60
+
61
+ You can use helper methods in your REPL session by including a module:
62
+
63
+ ```ruby
64
+ [1] pry(main)> include GrapeRouteHelpers::NamedRouteMatcher
65
+ ```
59
66
  #### Use Helpers in Your API
60
67
 
61
68
  Use the methods inside your Grape API actions. Given this example API:
@@ -4,6 +4,10 @@ module GrapeRouteHelpers
4
4
  attr_reader :route, :helper_names, :helper_arguments,
5
5
  :extension, :route_options
6
6
 
7
+ def self.sanitize_method_name(string)
8
+ string.gsub(/\W|^[0-9]/, '_')
9
+ end
10
+
7
11
  def initialize(route)
8
12
  @route = route
9
13
  @route_options = route.instance_variable_get(:@options)
@@ -80,7 +84,8 @@ module GrapeRouteHelpers
80
84
  end
81
85
  end
82
86
 
83
- name + '_path'
87
+ sanitized_name = self.class.sanitize_method_name(name)
88
+ sanitized_name + '_path'
84
89
  end
85
90
 
86
91
  def segment_to_value(segment, opts = {})
@@ -4,6 +4,7 @@ module GrapeRouteHelpers
4
4
  # helper function name
5
5
  module NamedRouteMatcher
6
6
  def method_missing(method_id, *arguments)
7
+ super unless method_id.to_s.match(/_path$/)
7
8
  segments = arguments.first || {}
8
9
 
9
10
  route = Grape::API.decorated_routes.detect do |r|
@@ -1,4 +1,4 @@
1
1
  # Gem version
2
2
  module GrapeRouteHelpers
3
- VERSION = '1.2.0'
3
+ VERSION = '1.2.1'
4
4
  end
@@ -5,7 +5,7 @@ describe GrapeRouteHelpers::AllRoutes do
5
5
 
6
6
  describe '#all_routes' do
7
7
  context 'when API is mounted within another API' do
8
- let(:mounting_api) { Spec::Support::RouteMatcherHelpers.mounting_api }
8
+ let(:mounting_api) { Spec::Support::MountedAPI }
9
9
 
10
10
  it 'does not include the same route twice' do
11
11
  mounting_api
@@ -20,7 +20,7 @@ describe GrapeRouteHelpers::AllRoutes do
20
20
  end
21
21
 
22
22
  expect(duplicates).to be_empty
23
- expect(all_route_options.size).to eq(5)
23
+ expect(all_route_options.size).to eq(6)
24
24
  end
25
25
  end
26
26
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe GrapeRouteHelpers::DecoratedRoute do
4
- let(:api) { Spec::Support::RouteMatcherHelpers.api }
4
+ let(:api) { Spec::Support::API }
5
5
 
6
6
  let(:routes) do
7
7
  api.routes.map do |route|
@@ -25,6 +25,27 @@ describe GrapeRouteHelpers::DecoratedRoute do
25
25
  routes.detect { |route| route.route_path =~ /custom_name/ }
26
26
  end
27
27
 
28
+ let(:ping_route) do
29
+ routes.detect { |route| route.route_path =~ /ping/ }
30
+ end
31
+
32
+ describe '#sanitize_method_name' do
33
+ it 'removes characters that are illegal in Ruby method names' do
34
+ illegal_names = ['beta-1', 'name_with_+', 'name_with_(']
35
+ sanitized = illegal_names.map do |name|
36
+ described_class.sanitize_method_name(name)
37
+ end
38
+ expect(sanitized).to match_array(%w(beta_1 name_with__ name_with__))
39
+ end
40
+
41
+ it 'only replaces integers if they appear at the beginning' do
42
+ illegal_name = '1'
43
+ legal_name = 'v1'
44
+ expect(described_class.sanitize_method_name(illegal_name)).to eq('_')
45
+ expect(described_class.sanitize_method_name(legal_name)).to eq('v1')
46
+ end
47
+ end
48
+
28
49
  describe '#helper_names' do
29
50
  context 'when a route is given a custom helper name' do
30
51
  it 'uses the custom name instead of the dynamically generated one' do
@@ -40,15 +61,11 @@ describe GrapeRouteHelpers::DecoratedRoute do
40
61
  end
41
62
 
42
63
  context 'when an API has multiple versions' do
43
- let(:api_versions) { %w(beta alpha v1) }
44
-
45
- before do
46
- api.version api_versions
47
- end
64
+ let(:api) { Spec::Support::APIWithMultipleVersions }
48
65
 
49
66
  it "returns the route's helper name for each version" do
50
- helper_names = show_route.helper_names
51
- expect(helper_names.size).to eq(api_versions.size)
67
+ helper_names = ping_route.helper_names
68
+ expect(helper_names.size).to eq(api.version.size)
52
69
  end
53
70
  end
54
71
 
@@ -191,13 +208,12 @@ describe GrapeRouteHelpers::DecoratedRoute do
191
208
  end
192
209
 
193
210
  context "when a route's API has multiple versions" do
194
- before(:each) do
195
- api.version %w(v1 v2)
196
- end
211
+ let(:api) { Spec::Support::APIWithMultipleVersions }
197
212
 
198
213
  it 'returns a path for each version' do
199
- expect(index_route.api_v1_cats_path).to eq('/api/v1/cats.json')
200
- expect(index_route.api_v2_cats_path).to eq('/api/v2/cats.json')
214
+ expect(ping_route.alpha_ping_path).to eq('/alpha/ping')
215
+ expect(ping_route.beta_ping_path).to eq('/beta/ping')
216
+ expect(ping_route.v1_ping_path).to eq('/v1/ping')
201
217
  end
202
218
  end
203
219
 
@@ -3,16 +3,13 @@ require 'spec_helper'
3
3
  describe GrapeRouteHelpers::NamedRouteMatcher do
4
4
  include described_class
5
5
 
6
- let(:api) { Spec::Support::RouteMatcherHelpers.api }
7
-
8
6
  let(:routes) do
9
- api
10
7
  Grape::API.decorated_routes
11
8
  end
12
9
 
13
10
  let(:ping_route) do
14
11
  routes.detect do |route|
15
- route.route_path =~ /ping/
12
+ route.route_path =~ /ping/ && route.route_version == 'v1'
16
13
  end
17
14
  end
18
15
 
@@ -120,16 +117,12 @@ describe GrapeRouteHelpers::NamedRouteMatcher do
120
117
  describe '#method_missing' do
121
118
  context 'when method name matches a Grape::Route path helper name' do
122
119
  it 'returns the path for that route object' do
123
- api
124
-
125
120
  path = api_v1_ping_path
126
121
  expect(path).to eq('/api/v1/ping.json')
127
122
  end
128
123
 
129
124
  context 'when argument to the helper is not a hash' do
130
125
  it 'raises an ArgumentError' do
131
- api
132
-
133
126
  expect do
134
127
  api_v1_ping_path(1234)
135
128
  end.to raise_error(ArgumentError)
@@ -139,8 +132,6 @@ describe GrapeRouteHelpers::NamedRouteMatcher do
139
132
 
140
133
  context 'when method name does not match a Grape::Route path helper name' do
141
134
  it 'raises a NameError' do
142
- api
143
-
144
135
  expect do
145
136
  some_method_name
146
137
  end.to raise_error(NameError)
@@ -151,8 +142,6 @@ describe GrapeRouteHelpers::NamedRouteMatcher do
151
142
  context 'when Grape::Route objects share the same helper name' do
152
143
  context 'when helpers require different segments to generate their path' do
153
144
  it 'uses arguments to infer which route to use' do
154
- api
155
-
156
145
  show_path = api_v1_cats_path('id' => 1)
157
146
  expect(show_path).to eq('/api/v1/cats/1.json')
158
147
 
@@ -163,8 +152,6 @@ describe GrapeRouteHelpers::NamedRouteMatcher do
163
152
 
164
153
  context 'when query params are passed in' do
165
154
  it 'uses arguments to infer which route to use' do
166
- api
167
-
168
155
  show_path = api_v1_cats_path('id' => 1, params: { 'foo' => 'bar' })
169
156
  expect(show_path).to eq('/api/v1/cats/1.json?foo=bar')
170
157
 
@@ -5,5 +5,3 @@ require 'grape/route_helpers'
5
5
 
6
6
  support_files = Dir.glob('spec/support/*')
7
7
  support_files.each { |f| require File.expand_path(f) }
8
-
9
- include Spec::Support::RouteMatcherHelpers
@@ -0,0 +1,57 @@
1
+ module Spec
2
+ module Support
3
+ # Test API
4
+ class API < Grape::API
5
+ version 'v1'
6
+ prefix 'api'
7
+ format 'json'
8
+
9
+ get 'custom_name', as: :my_custom_route_name do
10
+ 'hello'
11
+ end
12
+
13
+ get 'ping' do
14
+ 'pong'
15
+ end
16
+
17
+ resource :cats do
18
+ get '/' do
19
+ %w(cats cats cats)
20
+ end
21
+
22
+ route_param :id do
23
+ get do
24
+ 'cat'
25
+ end
26
+ end
27
+ end
28
+
29
+ route :any, '*path' do
30
+ 'catch-all route'
31
+ end
32
+ end
33
+
34
+ # API with more than one version
35
+ class APIWithMultipleVersions < Grape::API
36
+ version %w(beta alpha v1)
37
+
38
+ get 'ping' do
39
+ 'pong'
40
+ end
41
+ end
42
+
43
+ # API with another API mounted inside it
44
+ class MountedAPI < Grape::API
45
+ mount Spec::Support::API
46
+ end
47
+
48
+ # API with a version that would be illegal as a method name
49
+ class APIWithIllegalVersion < Grape::API
50
+ version 'beta-1'
51
+
52
+ get 'ping' do
53
+ 'pong'
54
+ end
55
+ end
56
+ end
57
+ 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.2.0
4
+ version: 1.2.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-09-27 00:00:00.000000000 Z
11
+ date: 2015-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape
@@ -122,7 +122,7 @@ files:
122
122
  - spec/grape_route_helpers/decorated_route_spec.rb
123
123
  - spec/grape_route_helpers/named_route_matcher_spec.rb
124
124
  - spec/spec_helper.rb
125
- - spec/support/route_matcher_helpers.rb
125
+ - spec/support/api.rb
126
126
  homepage: https://github.com/reprah/grape-route-helpers
127
127
  licenses:
128
128
  - MIT
@@ -1,44 +0,0 @@
1
- module Spec
2
- module Support
3
- # container for methods used in specs
4
- module RouteMatcherHelpers
5
- def self.api
6
- Class.new(Grape::API) do
7
- version 'v1'
8
- prefix 'api'
9
- format 'json'
10
-
11
- get 'custom_name', as: :my_custom_route_name do
12
- 'hello'
13
- end
14
-
15
- get 'ping' do
16
- 'pong'
17
- end
18
-
19
- resource :cats do
20
- get '/' do
21
- %w(cats cats cats)
22
- end
23
-
24
- route_param :id do
25
- get do
26
- 'cat'
27
- end
28
- end
29
- end
30
-
31
- route :any, '*path' do
32
- 'catch-all route'
33
- end
34
- end
35
- end
36
-
37
- def self.mounting_api
38
- Class.new(Grape::API) do
39
- mount Spec::Support::RouteMatcherHelpers.api
40
- end
41
- end
42
- end
43
- end
44
- end