Skip to content

Commit a3c3825

Browse files
committed
Merge pull request #33 from dblock/tests-and-stuff
Fixes and tests and some API changes.
2 parents 608f019 + d9b9c80 commit a3c3825

File tree

12 files changed

+180
-30
lines changed

12 files changed

+180
-30
lines changed

README.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ Add this line to `./config/routes.rb`:
2828
mount GrapeSwaggerRails::Engine => '/swagger'
2929
```
3030

31-
Create an initializer (e.g. `./config/initializers/swagger.rb`) and specify the URL to your Swagger API schema:
31+
Create an initializer (e.g. `./config/initializers/swagger.rb`) and specify the URL to your Swagger API schema and app:
3232

3333
```ruby
3434
GrapeSwaggerRails.options.url = '/swagger_doc.json'
35-
GrapeSwaggerRails.options.app_name = 'Swagger'
3635
GrapeSwaggerRails.options.app_url = 'http://swagger.wordnik.com'
3736
```
3837

38+
You can set the app name, default is "Swagger".
39+
40+
``` ruby
41+
GrapeSwaggerRails.options.app_name = 'Swagger'
42+
```
43+
3944
You can specify additional headers to add to each request:
4045

4146
```ruby
@@ -75,22 +80,16 @@ You can use the ```api_key``` input box to fill in your API token.
7580
### Swagger UI Authorization
7681

7782
You may want to authenticate users before displaying the Swagger UI, particularly when the API is protected by Basic Authentication.
78-
Use the `authenticate_with` option to inspect the request to the Swagger UI:
83+
Use the `before` option to inspect the request before Swagger UI:
7984

8085
```ruby
81-
GrapeSwaggerRails.options.authenticate_with do |request|
82-
# 1. Inspect the `request` or access the Swagger UI controller via `self`
83-
# 2. Check `current_user` or `can? :access, :api`, etc....
84-
# 3. return a boolean value
86+
GrapeSwaggerRails.options.before_filter do |request|
87+
# 1. Inspect the `request` or access the Swagger UI controller via `self`.
88+
# 2. Check `current_user` or `can? :access, :api`, etc.
89+
# 3. Redirect or error in case of failure.
8590
end
8691
```
8792

88-
The block above is stored in the `authentication_proc` option:
89-
90-
```ruby
91-
GrapeSwaggerRails.options.authentication_proc: Proc.new{|request| # return a boolean value}
92-
```
93-
9493
## Contributing
9594

9695
1. Fork it

app/controllers/grape_swagger_rails/application_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module GrapeSwaggerRails
22
class ApplicationController < ActionController::Base
33
before_filter do
4-
if GrapeSwaggerRails.options.authentication_proc
5-
instance_exec(request, &GrapeSwaggerRails.options.authentication_proc)
4+
if GrapeSwaggerRails.options.before_filter
5+
instance_exec(request, &GrapeSwaggerRails.options.before_filter)
66
end
77
end
8-
8+
99
def index
1010
end
1111
end

app/views/grape_swagger_rails/application/index.html.erb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
url: options.app_url + options.url,
1414
dom_id:"swagger-ui-container",
1515
supportHeaderParams: true,
16-
headers: options.headers,
1716
supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
1817
onComplete: function(swaggerApi, swaggerUi){
1918
if('console' in window) {
@@ -46,6 +45,11 @@
4645
window.authorizations.add("key", null);
4746
}
4847
})
48+
49+
<% GrapeSwaggerRails.options.headers.each_pair do |key, value| %>
50+
<%=raw "window.authorizations.add('key', new ApiKeyAuthorization('#{CGI.escapeHTML(key)}', '#{CGI.escapeHTML(value)}', 'header'));" %>
51+
<% end %>
52+
4953
window.swaggerUi.load();
5054
});
5155
</script>

grape-swagger-rails.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ Gem::Specification.new do |spec|
3232
spec.add_development_dependency 'jquery-rails'
3333
spec.add_development_dependency 'grape-swagger-ui'
3434
spec.add_development_dependency 'sprockets'
35+
spec.add_development_dependency 'rack-cors'
3536
end

lib/grape-swagger-rails.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
require "grape-swagger-rails/engine"
22

33
module GrapeSwaggerRails
4-
class Options < Struct.new(:url, :api_key_name, :api_key_type, :api_auth, :headers, :app_name, :app_url, :authentication_proc)
5-
def authenticate_with(&block)
6-
self.authentication_proc = block
4+
class Options < OpenStruct
5+
def before_filter(&block)
6+
if block_given?
7+
self.before_filter_proc = block
8+
else
9+
self.before_filter_proc
10+
end
711
end
812
end
913

@@ -21,7 +25,7 @@ def authenticate_with(&block)
2125
api_key_name: 'api_key', # 'Authorization'
2226
api_key_type: 'query', # 'header'
2327

24-
authentication_proc: nil # Proc used as a controller before filter that returns a boolean
28+
before_filter_proc: nil # Proc used as a controller before filter
2529
)
2630

2731
end

spec/dummy/app/api/api.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,15 @@ class API < Grape::API
1717
end
1818
end
1919

20+
desc 'Get headers.'
21+
get '/headers' do
22+
request.headers.as_json
23+
end
24+
25+
desc 'Get params.'
26+
get '/params' do
27+
request.params.as_json
28+
end
29+
2030
add_swagger_documentation
2131
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<% flash.each do |name, msg| %>
2+
<% if msg.is_a?(String) %>
3+
<%= content_tag :div, msg, :class => "flash_#{name}" %>
4+
<% end %>
5+
<% end %>

spec/dummy/app/views/layouts/application.html.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
<html>
33
<head>
44
<title>Nambla</title>
5-
<%= stylesheet_link_tag 'application', media: 'all' %>
5+
<%= stylesheet_link_tag 'application', media: 'all' %>
66
<%= javascript_include_tag 'application' %>
77
<%= csrf_meta_tags %>
88
</head>
99
<body>
1010

11+
<%= render 'layouts/messages' %>
12+
1113
<%= yield %>
1214

1315
</body>

spec/dummy/config/application.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'action_view/railtie'
55
require 'sprockets/railtie'
66
require 'jquery-rails'
7+
require 'rack/cors'
78

89
Bundler.require(*Rails.groups)
910
require 'grape-swagger-rails'
@@ -12,5 +13,11 @@ module Dummy
1213
class Application < Rails::Application
1314
config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb')
1415
config.autoload_paths += Dir[Rails.root.join('app', 'api', '*')]
16+
config.middleware.use Rack::Cors do
17+
allow do
18+
origins '*'
19+
resource '*', :headers => :any, :methods => [:get, :post, :options]
20+
end
21+
end
1522
end
1623
end

spec/dummy/config/boot.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
66

7+
require 'sass'
78
require 'grape'
89
require 'grape-swagger'
9-
require 'sass'
1010
require 'coffee_script'

0 commit comments

Comments
 (0)