SHOW:
|
|
- or go back to the newest paste.
1 | # Curl Request | |
2 | # curl -X POST -H "Content-Type:application/json" 0:9292/v1/articles -d '{"title":"hello","body":"world"}' | |
3 | # {"error":"article is missing"} | |
4 | # curl -X POST -H "Content-Type:application/json" 0:9292/v1/articles -d '{"article":{"title":"hello","body":"world"}}' | |
5 | # {"error":"article is invalid"} | |
6 | ||
7 | # Entity | |
8 | module API | |
9 | module Entities | |
10 | class Article < Grape::Entity | |
11 | expose :title, documentation: { type: 'string', desc: 'Title' } | |
12 | expose :body, documentation: { type: 'string', desc: 'Body' } | |
13 | end | |
14 | end | |
15 | end | |
16 | ||
17 | # API | |
18 | desc "Create an article" | |
19 | params do | |
20 | requires :article, type: API::Entities::Article, documentation: { example: "aklsdfj" } | |
21 | end | |
22 | post '/articles' do | |
23 | puts params | |
24 | article = Article.create(params(:title, :body)) | |
25 | represent(article, env) | |
26 | end | |
27 | ||
28 | # Add Swagger Docs | |
29 | add_swagger_documentation mount_path: 'api/doc', | |
30 | api_version: 'v1', | |
31 | markdown: GrapeSwagger::Markdown::KramdownAdapter, | |
32 | hide_documentation_path: true, | |
33 | base_path: Application.config.base_path, | |
34 | models: [API::Entities::Article] |