Skip to content

Commit

Permalink
Make versioner consider the mount destination path
Browse files Browse the repository at this point in the history
  • Loading branch information
namusyaka committed Feb 4, 2017
1 parent 081d09b commit 393d362
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 8 deletions.
14 changes: 7 additions & 7 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-01-17 06:04:25 -0500 using RuboCop version 0.47.0.
# on 2017-02-02 01:40:01 +0900 using RuboCop version 0.47.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -13,7 +13,7 @@ Metrics/AbcSize:
# Offense count: 266
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 3084
Max: 3104

# Offense count: 1
# Configuration parameters: CountBlocks.
Expand All @@ -23,19 +23,19 @@ Metrics/BlockNesting:
# Offense count: 8
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 279
Max: 280

# Offense count: 26
# Offense count: 27
Metrics/CyclomaticComplexity:
Max: 14

# Offense count: 975
# Offense count: 983
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 215

# Offense count: 54
# Offense count: 55
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 33
Expand All @@ -45,7 +45,7 @@ Metrics/MethodLength:
Metrics/ModuleLength:
Max: 212

# Offense count: 17
# Offense count: 18
Metrics/PerceivedComplexity:
Max: 14

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
### 0.19.2 (Next)

#### Fixes

* [#1570](https://github.com/ruby-grape/grape/pull/1570): Make versioner consider the mount destination path - [@namusyaka](https://github.com/namusyaka).
* Your contribution here.

#### Features

* [#1555](https://github.com/ruby-grape/grape/pull/1555): Added code coverage w/Coveralls - [@dblock](https://github.com/dblock).
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ def build_stack(helpers)
stack.use Grape::Middleware::Versioner.using(namespace_inheritable(:version_options)[:using]),
versions: namespace_inheritable(:version) ? namespace_inheritable(:version).flatten : nil,
version_options: namespace_inheritable(:version_options),
prefix: namespace_inheritable(:root_prefix)
prefix: namespace_inheritable(:root_prefix),
mount_path: namespace_stackable(:mount_path).first
end

stack.use Grape::Middleware::Formatter,
Expand Down
11 changes: 11 additions & 0 deletions lib/grape/middleware/versioner/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def default_options

def before
path = env[Grape::Http::Headers::PATH_INFO].dup
path.sub!(mount_path, '') if mounted_path?(path)

if prefix && path.index(prefix) == 0 # rubocop:disable all
path.sub!(prefix, '')
Expand All @@ -40,6 +41,16 @@ def before

private

def mounted_path?(path)
return false unless mount_path && path.start_with?(mount_path)
rest = path.slice(mount_path.length..-1)
rest.start_with?('/') || rest.empty?
end

def mount_path
@mount_path ||= options[:mount_path] && options[:mount_path] != '/' ? options[:mount_path] : ''
end

def prefix
Grape::Router.normalize_path(options[:prefix].to_s) if options[:prefix]
end
Expand Down
27 changes: 27 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3090,6 +3090,33 @@ def static
versioned_get '/users/hello', 'two', using: :header, vendor: 'test'
expect(last_response.body).to eq('two')
end

it 'recognizes potential versions with mounted path' do
a = Class.new(Grape::API) do
version :v1, using: :path

get '/hello' do
'hello'
end
end

b = Class.new(Grape::API) do
version :v1, using: :path

get '/world' do
'world'
end
end

subject.mount a => '/one'
subject.mount b => '/two'

get '/one/v1/hello'
expect(last_response.status).to eq 200

get '/two/v1/world'
expect(last_response.status).to eq 200
end
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/grape/middleware/versioner/path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@
expect(subject.call('PATH_INFO' => '/v3/foo').last).to eq('v3')
end
end

context 'with mount path' do
let(:options) { { mount_path: '/mounted', versions: [:v1] } }
it 'recognizes potential version' do
expect(subject.call('PATH_INFO' => '/mounted/v1/foo').last).to eq('v1')
end
end
end

0 comments on commit 393d362

Please sign in to comment.