Skip to content

Commit 1262ad0

Browse files
committed
adds getting of specified resource
1 parent 3e855dd commit 1262ad0

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
#### Features
44

5+
* Your contribution here.
6+
* [#501](https://github.com/ruby-grape/grape-swagger/pull/501): Adds getting of a specified resource for Rake Tasks - [@LeFnord](https://github.com/LeFnord).
57
* [#500](https://github.com/ruby-grape/grape-swagger/pull/500): Adds Rake tasks to get and validate OAPI/Swagger documentation - [@LeFnord](https://github.com/LeFnord).
68
* [#493](https://github.com/ruby-grape/grape-swagger/pull/493): Swagger UI endpoint authorization. - [@texpert](https://github.com/texpert).
79
* [#492](https://github.com/ruby-grape/grape/pull/492): Define security requirements on endpoint methods - [@tomregelink](https://github.com/tomregelink).
810
* [#497](https://github.com/ruby-grape/grape-swagger/pull/497): Use ruby-grape-danger in Dangerfile - [@dblock](https://github.com/dblock).
9-
* Your contribution here.
1011

1112
#### Fixes
1213

13-
* [#494](https://github.com/ruby-grape/grape-swagger/pull/494): Header parametes are now included in documentation when body parameters have been defined - [@anakinj](https://github.com/anakinj).
1414
* Your contribution here.
15+
* [#494](https://github.com/ruby-grape/grape-swagger/pull/494): Header parametes are now included in documentation when body parameters have been defined - [@anakinj](https://github.com/anakinj).
1516

1617
### 0.23.0 (August 5, 2016)
1718

README.md

+16-11
Original file line numberDiff line numberDiff line change
@@ -803,10 +803,10 @@ end
803803
804804
The Swagger UI on Grape could be secured from unauthorized access using any middleware, which provides certain methods:
805805
806-
- a *before* method to be run in the Grape controller for authorization purpose;
806+
- a *before* method to be run in the Grape controller for authorization purpose;
807807
- some guard method, which could receive as argument a string or an array of authorization scopes;
808808
- a method which processes and returns the access token received in the HTTP request headers (usually in the 'HTTP_AUTHORIZATION' header).
809-
809+
810810
Below are some examples of securing the Swagger UI on Grape installed along with Ruby on Rails:
811811
812812
- The WineBouncer and Doorkeeper gems are used in the examples;
@@ -828,9 +828,9 @@ This is how to configure the grape_swagger documentation:
828828
829829
The guard method should inject the Security Requirement Object into the endpoint's route settings (see Grape::DSL::Settings.route_setting method).
830830

831-
The 'oauth2 false' added to swagger_documentation is making the main Swagger endpoint protected with OAuth, i.e. it
831+
The 'oauth2 false' added to swagger_documentation is making the main Swagger endpoint protected with OAuth, i.e. it
832832
is retreiving the access_token from the HTTP request, but the 'false' scope is for skipping authorization and showing
833-
the UI for everyone. If the scope would be set to something else, like 'oauth2 admin', for example, than the UI
833+
the UI for everyone. If the scope would be set to something else, like 'oauth2 admin', for example, than the UI
834834
wouldn't be displayed at all to unauthorized users.
835835
836836
Further on, the guard could be used, where necessary, for endpoint access protection. Put it prior to the endpoint's method:
@@ -841,20 +841,20 @@ Further on, the guard could be used, where necessary, for endpoint access protec
841841
get do
842842
render_users
843843
end
844-
844+
845845
oauth2 'admin'
846846
post do
847847
User.create!...
848848
end
849849
end
850850
```
851851

852-
And, finally, if you want to not only restrict the access, but to completely hide the endpoint from unauthorized
852+
And, finally, if you want to not only restrict the access, but to completely hide the endpoint from unauthorized
853853
users, you could pass a lambda to the :hidden key of a endpoint's description:
854-
854+
855855
```ruby
856856
not_admins = lambda { |token=nil| token.nil? || !User.find(token.resource_owner_id).admin? }
857-
857+
858858
resource :users do
859859
desc 'Create user', hidden: not_admins
860860
oauth2 'admin'
@@ -864,8 +864,8 @@ users, you could pass a lambda to the :hidden key of a endpoint's description:
864864
end
865865
```
866866
867-
The lambda is checking whether the user is authenticated (if not, the token is nil by default), and has the admin
868-
role - only admins can see this endpoint.
867+
The lambda is checking whether the user is authenticated (if not, the token is nil by default), and has the admin
868+
role - only admins can see this endpoint.
869869
870870
<a name="md_usage" />
871871
## Markdown in Detail
@@ -1122,15 +1122,20 @@ GrapeSwagger::Rake::OapiTasks.new(::Api::Base)
11221122
11231123
```
11241124
rake oapi:fetch
1125-
rake oapi:fetch store=true # writes to swagger_doc.json
1125+
params:
1126+
- store={ true | file_name } – save as JSON (optional)
1127+
- resource=resource_name – get only for this one (optional)
11261128
```
11271129
11281130
#### OpenApi/Swagger Validation
11291131
11301132
**requires**: `npm` and `swagger-cli` to be installed
11311133
1134+
11321135
```
11331136
rake oapi:validate
1137+
params:
1138+
- resource=resource_name – get only for this one (optional)
11341139
```
11351140
11361141

lib/grape-swagger/rake/oapi_tasks.rb

+20-9
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,22 @@ def define_tasks
2929
#
3030
# get swagger/OpenAPI documentation
3131
def fetch
32-
desc 'generates OpenApi documentation (`store=true`, stores to FS)'
32+
desc 'generates OpenApi documentation …
33+
params (usage: key=value):
34+
store – save as JSON file, default: false (optional)
35+
resource - if given only for that it would be generated (optional)'
3336
task fetch: :environment do
3437
make_request
35-
ENV['store'] ? File.write(file, @oapi) : print(@oapi)
38+
39+
save_to_file? ? File.write(file, @oapi) : print(@oapi)
3640
end
3741
end
3842

3943
# validates swagger/OpenAPI documentation
4044
def validate
41-
desc 'validates the generated OpenApi file'
45+
desc 'validates the generated OpenApi file …
46+
params (usage: key=value):
47+
resource - if given only for that it would be generated (optional)'
4248
task validate: :environment do
4349
ENV['store'] = 'true'
4450
::Rake::Task['oapi:fetch'].invoke
@@ -59,19 +65,24 @@ def make_request
5965
JSON.parse(
6066
last_response.body, symolize_names: true
6167
)
62-
)
68+
) + "\n"
6369
end
6470

6571
def url_for
6672
oapi_route = api_class.routes[-2]
67-
url = '/swagger_doc'
68-
url = "/#{oapi_route.version}#{url}" if oapi_route.version
69-
url = "/#{oapi_route.prefix}#{url}" if oapi_route.prefix
70-
url
73+
path = oapi_route.pattern.origin
74+
path.sub!(':version', oapi_route.version.to_s)
75+
76+
[path, ENV['resource']].join('/').chomp('/')
77+
end
78+
79+
def save_to_file?
80+
ENV['store'] && JSON.parse(@oapi).keys.first != 'error'
7181
end
7282

7383
def file
74-
File.join(Dir.getwd, 'swagger_doc.json')
84+
name = ENV['store'] == 'true' || ENV['store'].blank? ? 'swagger_doc.json' : ENV['store']
85+
File.join(Dir.getwd, name)
7586
end
7687

7788
def app

0 commit comments

Comments
 (0)