Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/d4be4st/api-swagger-test
Testing the swagger and grape api for rails
https://github.com/d4be4st/api-swagger-test
Last synced: 29 days ago
JSON representation
Testing the swagger and grape api for rails
- Host: GitHub
- URL: https://github.com/d4be4st/api-swagger-test
- Owner: d4be4st
- Created: 2013-07-29T19:37:30.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-02-28T15:05:06.000Z (over 10 years ago)
- Last Synced: 2023-04-05T07:12:30.830Z (over 1 year ago)
- Language: Ruby
- Size: 156 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Relevant repositories
* [grape](https://github.com/intridea/grape)
* [swagger-ui](https://github.com/wordnik/swagger-ui)
* [grape-entity](https://github.com/intridea/grape-entity)
* [grape-swagger](https://github.com/tim-vandecasteele/grape-swagger)
* [swagger-ui_rails](https://github.com/d4be4st/swagger-ui_rails)## Preparing application
Add to your Gemfile:
gem 'grape', '0.6.1'
gem 'grape-entity', '0.3.0'
gem 'grape-swagger', '0.7.6', :github => 'd4be4st/grape-swagger'
gem 'swagger-ui_rails', '0.1.7'## Adding swagger-ui
add to application.css and application.js
require swagger-ui
create a api_docs view with:
= render 'swagger_ui/swagger_ui', discovery_url: '/path/to/swagger_doc'
add route to routes.rb
get 'api/docs' => 'api_docs#index'
## Create Grape API
please read [grape documentation](https://github.com/intridea/grape)
### Example
module API
class Users < Grape::API
desc 'Get all users'
get :users do
users = User.all
enddesc 'Get one user'
get 'users/:id' do
User.find(params[:id])
enddesc 'Create user'
params do
group :user do
requires :username, type: String
requires :email, type: String
optional :password, type: String
end
end
post :user do
User.create(params[:user])
end
endend
## Create Grape Swagger Documenatation
please read [grape-swagger documentation](https://github.com/tim-vandecasteele/grape-swagger)
### Example
module API
class Root < Grape::API
prefix 'api'
version 'v1'
format :jsonmount API::Users
add_swagger_documentation api_version: 'v1', hide_documentation_path: true, hide_format: true
endend
## Add Api to Routes
mount API::Root => '/'
Because of the prefix and the version, api urls are:
GET /api/v1/swagger_doc
GET /api/v1/users
GET /api/v1/users/:id
POST /api/v1/user## RESTful Model Representations
Use [grape-entities](https://github.com/intridea/grape#restful-model-representations)
### Example
module API
class UserEntity < Grape::Entity
expose :email
expose :username
endclass Users < Grape::API
desc 'Get all users'
get :users do
users = User.all
present users, with: Users::Entity
end
end
end## Try it!
1. Clone this repository
2. run bundle
3. start server
4. open localhost:3000/api/docs