Skip to content

Commit

Permalink
Make API namespace configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
nashby committed Jul 9, 2018
1 parent 80230de commit d58edd1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@ Then a `rake routes` would show your desires fulfilled:
DELETE /foo/:id(.:format) api/v2/foo#destroy
```

It's also possible to configure route's namespace with `:namespace` option (if you want to remove namespacing at all just pass a blank string):

```ruby
api vendor_string: 'myvendor', default_version: 1, namespace: 'auth_api' do
version 1 do
cache as: 'v1' do
resources :foo, only: :index
end
end
end
```

Then a `rake routes` would show your desires fulfilled:

```
GET /auth_api/foo(.:format) auth_api/v1/foo#index
```

## Testing
Because controller tests will not go through the routing constraints, you will get routing errors when testing API
controllers.
Expand Down
8 changes: 7 additions & 1 deletion lib/api-versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ def api(options = {}, &block)
VersionCheck.default_version = options[:default_version]
VersionCheck.vendor_string = options[:vendor_string]

namespace(:api, options) { DSL.new(self, &block) }
api_namespace = options.fetch(:namespace, :api)

if api_namespace.blank?
DSL.new(self, &block)
else
namespace(api_namespace, options) { DSL.new(self, &block) }
end
end
end

Expand Down
8 changes: 8 additions & 0 deletions spec/dummy/app/controllers/auth_api/v1/quux_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AuthApi::V1::QuuxController < ActionController::Base
def new
respond_to do |format|
format.json { render json: {} }
format.xml { render xml: {} }
end
end
end
8 changes: 8 additions & 0 deletions spec/dummy/app/controllers/v1/qux_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class V1::QuxController < ActionController::Base
def new
respond_to do |format|
format.json { render json: {} }
format.xml { render xml: {} }
end
end
end
16 changes: 16 additions & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,21 @@
end
end

api vendor_string: 'myvendor', namespace: '' do
version 1 do
cache as: 'v1' do
resources :qux
end
end
end

api vendor_string: 'myvendor', namespace: 'auth_api' do
version 1 do
cache as: 'v1' do
resources :quux
end
end
end

get '*a' => 'errors#not_found'
end
10 changes: 10 additions & 0 deletions spec/routing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,14 @@
new_api_baz_path.should == '/baz/new'
end
end

describe 'namespace' do
it "should be possible to remove api namespace" do
new_qux_path.should == '/qux/new'
end

it "should be possible to overwrite api namespace" do
new_auth_api_quux_path.should == '/auth_api/quux/new'
end
end
end

0 comments on commit d58edd1

Please sign in to comment.