api_schema 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd078f116a2eae7922f15408fd29d233d1af4e8d
4
+ data.tar.gz: 622afbb13b567654a91a2915995a6f9eda3dfaa3
5
+ SHA512:
6
+ metadata.gz: ba089ca4c00ec03ce5234b828212b3912bc43c65c643f7935ba39252127b964347574cf351cf76c1710e16614958c572be64f3272faf28959e95ff6e359dbb79
7
+ data.tar.gz: b286a9cca79d3f87661ca1a9550ec86faee2c6be18eb200ca523ae93803801f939add825e945e3859b83c747c229bbf4e4575525563da3a9f9abcbc0e7b07389
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at dmitry.chopey@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in api-schema.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Dimitry Chopey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,287 @@
1
+ # Api Schema
2
+ Provides a framework and DSL for describing APIs and generate swagger-json using minimalist, schema.db-like syntax.
3
+
4
+ <p align="center">
5
+ <a href="#installation">Installation</a> | <a href="#usage">Usage</a> | <a href="#license">License</a>
6
+ </p>
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'api_schema'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install api_schema
23
+
24
+ ## Usage
25
+
26
+ Just add `include ApiSchema` and configurations to your base class and inherit from it.
27
+
28
+ #### BaseDocs
29
+
30
+ ```ruby
31
+ module V1
32
+ class BaseDocs
33
+ include ApiSchema
34
+
35
+ configure do |config|
36
+ config.title = 'Users API'
37
+ config.description = 'API for users'
38
+ config.version = '1.0'
39
+ config.host = 'sample.com'
40
+ config.base_path = '/api'
41
+ config.terms_of_service = 'https://sample.com/terms'
42
+ config.contact_name = 'API Team'
43
+ config.consumes = 'application/json'
44
+ config.produces = 'application/json'
45
+ config.authorization = true
46
+ config.error_model = :error_model
47
+ config.error_desc = {
48
+ '401' => "Unauthorized",
49
+ '403' => "Forbidden",
50
+ '404' => "Not found",
51
+ '422' => "Unprocessable Entity"
52
+ }
53
+ end
54
+ end
55
+ end
56
+ ```
57
+
58
+ #### UsersController
59
+
60
+ ```ruby
61
+ module V1
62
+ module ControllersDocs
63
+ class UsersController < BaseDocs
64
+
65
+ get do
66
+ path_param :id, :integer
67
+ name 'Get User'
68
+ desc 'Returns user with provided id'
69
+ response 200, :user
70
+ error! 401, 403, 404, 422
71
+ end
72
+ end
73
+ end
74
+ ```
75
+
76
+ #### UserSerializer
77
+
78
+ ```ruby
79
+ module V1
80
+ module SerializersDocs
81
+ class UserSerializer < BaseDocs
82
+
83
+ serializer :user do |f|
84
+ f.integer :id, required: true
85
+ f.string :email, required: true
86
+ f.string :name
87
+ end
88
+ end
89
+ end
90
+ ```
91
+
92
+ ### Serializers
93
+
94
+ To describe serializers you can use `serializer` and `array_serializer` methods.
95
+
96
+ **Here `:user` and `:users` are unique identifiers**
97
+
98
+ For **member** responses:
99
+
100
+ ```ruby
101
+ serializer :user do |f|
102
+ f.integer :id, required: true
103
+ f.string :email, required: true
104
+ f.string :name
105
+ end
106
+ ```
107
+
108
+ Will have such a structure:
109
+
110
+ ```json
111
+ {
112
+ "user": {
113
+ "id": 1,
114
+ "email": "john.doe.gmail.com",
115
+ "name": "John Doe"
116
+ }
117
+ }
118
+ ```
119
+
120
+ For **collection** responses:
121
+
122
+ ```ruby
123
+ array_serializer :users do |f|
124
+ f.reference :meta
125
+ f.reference :user, type: :array
126
+ end
127
+ ```
128
+
129
+ Will have such a structure:
130
+
131
+ ```json
132
+ {
133
+ "meta": {...},
134
+ "users": [
135
+ {
136
+ "id": 1,
137
+ "email": "john.doe.gmail.com",
138
+ "name": "John Doe"
139
+ },
140
+ {...}]
141
+ }
142
+ ```
143
+
144
+ Then you can use this descriptions in the controllers with identifiers:
145
+
146
+ ```ruby
147
+ response 200, :user
148
+ ```
149
+
150
+ ```ruby
151
+ response 200, :users
152
+ ```
153
+
154
+ #### References
155
+
156
+ To user nested descriptions, you can use `reference` method:
157
+
158
+ ```ruby
159
+ serializer :file do |f|
160
+ f.integer :file_name, required: true
161
+ f.string :file_url, required: true
162
+ end
163
+ ```
164
+
165
+ ```ruby
166
+ serializer :attachment do |f|
167
+ f.integer :id, required: true
168
+ f.reference :file
169
+ end
170
+ ```
171
+
172
+ #### Parents
173
+
174
+ To inherit fields from another serializer, you can use `parent` parameter:
175
+
176
+ ```ruby
177
+ serializer :file do |f|
178
+ f.integer :file_name, required: true
179
+ f.string :file_url, required: true
180
+ end
181
+ ```
182
+
183
+ ```ruby
184
+ serializer :attachment, parent: :file do |f|
185
+ f.integer :id, required: true
186
+ end
187
+ ```
188
+
189
+ ### Controllers
190
+
191
+ #### Endpoints
192
+
193
+ To describe endpoints you can use `get`, `post`, `put`, `patch` methods.
194
+
195
+ Get **collection**:
196
+
197
+ ```ruby
198
+ get do
199
+ query_param :query, :string
200
+ query_param :sort_by, :string
201
+ name 'List Users'
202
+ desc "Returns list of the users"
203
+ response 200, :users
204
+ error! 401, 403, 422
205
+ end
206
+ ```
207
+
208
+ Will produce `/users` endpoint.
209
+
210
+ To get **member** you should use `path_param` method:
211
+
212
+ ```ruby
213
+ get do
214
+ path_param :id, :integer
215
+ name 'Get User'
216
+ desc 'Returns user with provided id'
217
+ response 200, :user
218
+ error! 401, 403, 422
219
+ end
220
+ ```
221
+
222
+ Will produce `/users/{id}` endpoint.
223
+
224
+ By default gem uses controller's name to generate endpoints, but you can make custom by passing first argument:
225
+
226
+ ```ruby
227
+ get 'people' do
228
+ path_param :id, :integer
229
+ name 'Get User'
230
+ desc 'Returns user with provided id'
231
+ response 200, :user
232
+ error! 401, 403, 422
233
+ end
234
+ ```
235
+
236
+ Will produce `/people/{id}` endpoint.
237
+
238
+ Use `extra_path` argument to add extra path to the endpoint
239
+
240
+ ```ruby
241
+ get extra_path: :read do
242
+ path_param :id, :integer
243
+ name 'Read Notification'
244
+ desc 'Reads notification with provided id'
245
+ response 200
246
+ error! 401, 403, 422
247
+ end
248
+ ```
249
+
250
+ Will produce `/notification/{id}/read` endpoint.
251
+
252
+ #### Parameters
253
+
254
+ To describe each endpoint you can use `header`, `body`, `path_param`, `query_param`
255
+
256
+ `header` and `body`:
257
+
258
+ ```ruby
259
+ post do
260
+ header :token, :string
261
+ body :create_user
262
+ name 'Create User'
263
+ desc 'Creates and returns new user'
264
+ response 200, :user
265
+ error! 401, 403, 422
266
+ end
267
+ ```
268
+
269
+ To describe body of the request you can use `request_body` method. It's just an alias for serializer:
270
+
271
+ ```ruby
272
+ request_body :create_user do |f|
273
+ f.string :email, required: true
274
+ f.string :first_name, required: true
275
+ f.string :last_name, required: true
276
+ end
277
+ ```
278
+
279
+ ## Dependencies
280
+
281
+ - [Active Support](https://github.com/rails/rails/tree/master/activesupport)
282
+ - [Swagger::Blocks](https://github.com/fotinakis/swagger-blocks)
283
+
284
+
285
+ ## License
286
+
287
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'api_schema/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "api_schema"
8
+ spec.version = ApiSchema::VERSION
9
+ spec.authors = ["Dmitry Chopey"]
10
+ spec.email = ["dmitry.chopey@gmail.com"]
11
+
12
+ spec.summary = %q{api_schema provides a framework and DSL for describing APIs
13
+ and generate swagger json.}
14
+ spec.description = %q{api_schema provides a framework and DSL for describing APIs
15
+ and generate swagger json using minimalist, schema.db-like syntax.}
16
+ spec.homepage = "https://github.com/qonto/api_schema."
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_runtime_dependency "activesupport"
27
+ spec.add_runtime_dependency "swagger-blocks", "~> 2.0"
28
+ spec.add_development_dependency "bundler", "~> 1.14"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "api_schema"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ module ApiSchema
2
+ class ApiVersion
3
+ attr_accessor :configuration, :resources, :serializers
4
+
5
+ def initialize(configuration)
6
+ @configuration = configuration
7
+ @resources = []
8
+ @serializers = []
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,41 @@
1
+ module ApiSchema
2
+ class Configuration
3
+ include ::Swagger::Blocks::ClassMethods
4
+
5
+ attr_accessor :title, :description, :version, :host,
6
+ :base_path, :terms_of_service, :contact_name,
7
+ :consumes, :produces, :authorization,
8
+ :error_model, :error_desc
9
+
10
+ def initialize
11
+ @error_model = 'error_model'
12
+ @consumes = 'application/json'
13
+ @produces = 'application/json'
14
+ end
15
+
16
+ def build
17
+ configuration = self
18
+ swagger_root do
19
+ key :swagger, '2.0'
20
+ info do
21
+ key :version, configuration.version
22
+ key :title, configuration.title
23
+ key :description, configuration.description
24
+ key :termsOfService, configuration.terms_of_service
25
+ contact do
26
+ key :name, configuration.contact_name
27
+ end
28
+ end
29
+ security_definition :authorization do
30
+ key :name, :Authorization
31
+ key :type, :apiKey
32
+ key :in, :header
33
+ end if configuration.authorization
34
+ key :host, configuration.host
35
+ key :basePath, configuration.base_path
36
+ key :consumes, configuration.consumes
37
+ key :produces, configuration.produces
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,18 @@
1
+ module ApiSchema
2
+ class Field
3
+
4
+ attr_accessor :type, :name, :format, :required, :description
5
+
6
+ def initialize(type, name, options = {})
7
+ @type = type
8
+ @name = name
9
+ @format = options.fetch(:format, nil)
10
+ @required = options.fetch(:required, false)
11
+ @description = options.fetch(:desc, '')
12
+ end
13
+
14
+ def required?
15
+ required
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,69 @@
1
+ module Swagger
2
+ module Blocks
3
+ module Nodes
4
+ class OperationNode < Node
5
+ def success_response(code, model_name = nil, fields = [])
6
+ response code do
7
+ schema do
8
+ key :'$ref', model_name if model_name
9
+ fields.each do |f|
10
+ property f.name do
11
+ key :type, f.type
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ def header_param(name, type)
19
+ parameter do
20
+ key :name, name
21
+ key :in, :header
22
+ key :required, true
23
+ key :type, type
24
+ end
25
+ end
26
+
27
+ def body_param(model_name)
28
+ parameter do
29
+ key :name, model_name
30
+ key :in, :body
31
+ key :required, true
32
+ schema do
33
+ key :'$ref', model_name
34
+ end
35
+ end
36
+ end
37
+
38
+ def query_param(name, type)
39
+ parameter do
40
+ key :name, name
41
+ key :in, :query
42
+ key :required, true
43
+ key :type, type
44
+ end
45
+ end
46
+
47
+ def path_param(name, type)
48
+ parameter do
49
+ key :name, name
50
+ key :in, :path
51
+ key :required, true
52
+ key :type, type
53
+ end
54
+ end
55
+
56
+ def error_responses(model_name, descriptions, *codes)
57
+ codes.each do |code|
58
+ response code do
59
+ key :description, descriptions[code]
60
+ schema do
61
+ key :'$ref', model_name
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,29 @@
1
+ module Swagger
2
+ module Blocks
3
+ module Nodes
4
+ class PropertyNode < Node
5
+ def requires(fields)
6
+ key :required, fields
7
+ end
8
+
9
+ def property_schema_for(serializer)
10
+ property serializer.name do
11
+ key :type, serializer.type
12
+ key :description, serializer.description
13
+ requires serializer.required_fields
14
+ serializer.fields.each do |f|
15
+ property f.name do
16
+ key :type, f.type
17
+ key :format, f.format if f.format
18
+ key :description, f.description
19
+ end
20
+ end
21
+ serializer.references.each do |r|
22
+ property_schema_for(r)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,66 @@
1
+ module Swagger
2
+ module Blocks
3
+ module Nodes
4
+ class SchemaNode < Node
5
+ def requires(fields)
6
+ key :required, fields
7
+ end
8
+
9
+ def schema_for(serializer)
10
+ key :title, serializer.id.to_s.humanize
11
+ serializer.type == :array ? array_schema(serializer) : single_schema(serializer)
12
+ end
13
+
14
+ def single_schema(serializer)
15
+ key :required, serializer.name
16
+ property serializer.name do
17
+ key :type, :object
18
+ requires serializer.required_fields
19
+ serializer.fields.each do |f|
20
+ property f.name do
21
+ key :type, f.type
22
+ key :format, f.format if f.format
23
+ key :description, f.description
24
+ end
25
+ end
26
+ serializer.references.each do |r|
27
+ property_schema_for(r) #schema_for from PropertyNode class
28
+ end
29
+ end
30
+ end
31
+
32
+ def array_schema(serializer)
33
+ requires serializer.required_fields
34
+ serializer.fields.each do |f|
35
+ property f.name do
36
+ key :type, f.type
37
+ key :format, f.format if f.format
38
+ key :description, f.description
39
+ end
40
+ end
41
+ serializer.references.each do |r|
42
+ property_schema_for(r)
43
+ end
44
+ end
45
+
46
+ def property_schema_for(serializer)
47
+ property serializer.name do
48
+ key :type, serializer.type
49
+ key :description, serializer.description
50
+ requires serializer.required_fields
51
+ serializer.fields.each do |f|
52
+ property f.name do
53
+ key :type, f.type
54
+ key :format, f.format if f.format
55
+ key :description, f.description
56
+ end
57
+ end
58
+ serializer.references.each do |r|
59
+ property_schema_for(r)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,121 @@
1
+ module ApiSchema
2
+ class ResourceDefinition
3
+ include ::Swagger::Blocks::ClassMethods
4
+
5
+ @@neighbors = {}
6
+
7
+ def initialize(method, base_path, extra_path = nil)
8
+ @base_path = base_path
9
+ @extra_path = extra_path
10
+ @method = method
11
+ @header_params = []
12
+ @path_params = []
13
+ @query_params = []
14
+ end
15
+
16
+ HeaderParam = ::Struct.new(:name, :type)
17
+ PathParam = ::Struct.new(:name, :type)
18
+ QueryParam = ::Struct.new(:name, :type)
19
+
20
+ attr_reader :method, :summary, :description, :header_params, :body_param,
21
+ :path_params, :query_params, :resp,
22
+ :errors, :base_path, :extra_path, :full_path
23
+
24
+ def name(name)
25
+ @summary = name
26
+ end
27
+
28
+ def desc(desc)
29
+ @description = desc
30
+ end
31
+
32
+ def header(name, type)
33
+ @header_params << HeaderParam.new(name, type)
34
+ end
35
+
36
+ def body(body_param)
37
+ @body_param = body_param
38
+ end
39
+
40
+ def path_param(name, type)
41
+ @path_params << PathParam.new(name, type)
42
+ end
43
+
44
+ def query_param(name, type)
45
+ @query_params << QueryParam.new(name, type)
46
+ end
47
+
48
+ def response(code, model_name = nil, &block)
49
+ @resp = Response.new(code, model_name)
50
+ if block && model_name.nil?
51
+ block.call(@resp)
52
+ end
53
+ end
54
+
55
+ def error!(*codes)
56
+ @errors = *codes
57
+ end
58
+
59
+ def with_path_param?
60
+ !path_params.empty?
61
+ end
62
+
63
+ def with_body?
64
+ !!body_param
65
+ end
66
+
67
+ def with_errors?
68
+ !errors.empty?
69
+ end
70
+
71
+ def generate_full_path
72
+ @full_path = with_path_param? ? "/#{base_path}/{id}" : "/#{base_path}"
73
+ @full_path << "/#{extra_path}" if extra_path
74
+ end
75
+
76
+ def build_neighbors
77
+ generate_full_path
78
+ @@neighbors[full_path] ||= []
79
+ @@neighbors[full_path] << self
80
+ end
81
+
82
+ def build
83
+ error_model = :error_model
84
+ error_desc = {
85
+ '401' => "Unauthorized",
86
+ '403' => "Forbidden",
87
+ '404' => "Not found",
88
+ '422' => "Unprocessable Entity"
89
+ }
90
+ resource = self
91
+
92
+ swagger_path resource.full_path do
93
+ @@neighbors[resource.full_path].each do |r|
94
+ operation(r.method) do
95
+ key :summary, r.summary
96
+ key :description, r.description
97
+ key :operationId, "#{r.method}_#{r.full_path}"
98
+ key :tags, r.base_path
99
+ security do
100
+ key :authorization, []
101
+ end
102
+ body_param(r.body_param) if r.with_body?
103
+
104
+ r.header_params.each do |p|
105
+ header_param(p.name, p.type)
106
+ end
107
+ r.path_params.each do |p|
108
+ path_param(p.name, p.type)
109
+ end
110
+ r.query_params.each do |p|
111
+ query_param(p.name, p.type)
112
+ end
113
+
114
+ success_response(r.resp.code, r.resp.model, r.resp.fields)
115
+ error_responses(error_model, error_desc, *r.errors) if r.with_errors?
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,45 @@
1
+ module ApiSchema
2
+ module ResourceMethods
3
+
4
+ private
5
+
6
+ def get(base_path = default_path, extra_path: nil, &block)
7
+ resource = ResourceDefinition.new(:get, base_path, extra_path)
8
+ resource.instance_eval(&block)
9
+ api_version.resources << resource
10
+ resource.build_neighbors
11
+ end
12
+
13
+ def post(base_path = default_path, extra_path: nil, &block)
14
+ resource = ResourceDefinition.new(:post, base_path, extra_path)
15
+ resource.instance_eval(&block)
16
+ api_version.resources << resource
17
+ resource.build_neighbors
18
+ end
19
+
20
+ def put(base_path = default_path, extra_path: nil, &block)
21
+ resource = ResourceDefinition.new(:put, base_path, extra_path)
22
+ resource.instance_eval(&block)
23
+ api_version.resources << resource
24
+ resource.build_neighbors
25
+ end
26
+
27
+ def patch(base_path = default_path, extra_path: nil, &block)
28
+ resource = ResourceDefinition.new(:patch, base_path, extra_path)
29
+ resource.instance_eval(&block)
30
+ api_version.resources << resource
31
+ resource.build_neighbors
32
+ end
33
+
34
+ def delete(base_path = default_path, extra_path: nil, &block)
35
+ resource = ResourceDefinition.new(:delete, base_path, extra_path)
36
+ resource.instance_eval(&block)
37
+ api_version.resources << resource
38
+ resource.build_neighbors
39
+ end
40
+
41
+ def default_path
42
+ self.to_s.split("::").last.gsub("Controller", "").split(/(?=[A-Z])/).map(&:downcase).join('_')
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,17 @@
1
+ module ApiSchema
2
+ class Response
3
+
4
+ attr_reader :code, :model, :fields
5
+
6
+ def initialize(code, model)
7
+ @code = code
8
+ @model = model
9
+ @fields = []
10
+ end
11
+
12
+ def method_missing(type, *args, &block)
13
+ options = args[1] || {}
14
+ @fields << Field.new(type, args[0], options)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ module ApiSchema
2
+ module RootMethods
3
+ def configure
4
+ configuration ||= Configuration.new
5
+ yield(configuration)
6
+ @@api_version = ApiVersion.new(configuration)
7
+ end
8
+
9
+ def api_version
10
+ @@api_version
11
+ end
12
+
13
+ def generate_json
14
+ @@api_version.configuration.build
15
+ @@api_version.serializers.each { |s| s.build }
16
+ @@api_version.resources.each { |r| r.build }
17
+
18
+ nodes = [@@api_version.configuration] + @@api_version.serializers + @@api_version.resources
19
+ ::Swagger::Blocks.build_root_json(nodes)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,52 @@
1
+ module ApiSchema
2
+ class SerializerDefinition
3
+ include ::Swagger::Blocks::ClassMethods
4
+
5
+ @@serializers = {}
6
+
7
+ PriorReference = ::Struct.new(:id, :type, :desc)
8
+
9
+ attr_reader :id, :fields, :references, :parent
10
+ attr_accessor :type, :name, :description
11
+
12
+ def initialize(id, type, name=nil, parent_id = nil)
13
+ @id = id
14
+ @type = type
15
+ @name = name || id
16
+ @parent = @@serializers[parent_id]
17
+ @fields = parent&.fields || []
18
+ @prior_references = parent&.prior_references || []
19
+ @references = []
20
+ @@serializers[id] = self
21
+ end
22
+
23
+ def required_fields
24
+ fields.select { |f| f.required? }.map(&:name) + references.map(&:name)
25
+ end
26
+
27
+ def reference(refernce_id, type: :object, desc: nil)
28
+ @prior_references << PriorReference.new(refernce_id, type, desc)
29
+ end
30
+
31
+ def build
32
+ build_references
33
+ sd = self
34
+ swagger_schema(id) { schema_for(sd) }
35
+ end
36
+
37
+ def build_references
38
+ @prior_references.each do |pr|
39
+ reference = @@serializers[pr.id].clone
40
+ reference.type = pr.type
41
+ reference.description = pr.desc
42
+ reference.name = reference.name.to_s.pluralize if reference.type == :array
43
+ @references << reference
44
+ end
45
+ end
46
+
47
+ def method_missing(type, *args, &block)
48
+ options = args[1] || {}
49
+ @fields << Field.new(type, args[0], options)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,20 @@
1
+ module ApiSchema
2
+ module SerializerMethods
3
+
4
+ private
5
+
6
+ def serializer(id, structure: :object, name: nil, parent: nil)
7
+ serializer = SerializerDefinition.new(id, structure, name, parent)
8
+ yield serializer if block_given?
9
+ api_version.serializers << serializer
10
+ end
11
+
12
+ def array_serializer(id, name: nil, parent: nil)
13
+ serializer = SerializerDefinition.new(id, :array, name, parent)
14
+ yield serializer if block_given?
15
+ api_version.serializers << serializer
16
+ end
17
+
18
+ alias_method :request_body, :serializer
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module ApiSchema
2
+ VERSION = "0.1.1"
3
+ end
data/lib/api_schema.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'active_support/inflector'
2
+ require 'swagger/blocks'
3
+
4
+ require 'api_schema/patches/operation_node'
5
+ require 'api_schema/patches/property_node'
6
+ require 'api_schema/patches/schema_node'
7
+
8
+ require "api_schema/configuration"
9
+ require "api_schema/api_version"
10
+ require "api_schema/root_methods"
11
+ require "api_schema/field"
12
+ require "api_schema/response"
13
+ require "api_schema/serializer_definition"
14
+ require "api_schema/serializer_methods"
15
+ require "api_schema/resource_definition"
16
+ require "api_schema/resource_methods"
17
+ require "api_schema/version"
18
+
19
+ module ApiSchema
20
+
21
+ def self.included(base)
22
+ base.extend(RootMethods)
23
+ base.extend(SerializerMethods)
24
+ base.extend(ResourceMethods)
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_schema
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Chopey
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: swagger-blocks
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: |-
84
+ api_schema provides a framework and DSL for describing APIs
85
+ and generate swagger json using minimalist, schema.db-like syntax.
86
+ email:
87
+ - dmitry.chopey@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".travis.yml"
95
+ - CODE_OF_CONDUCT.md
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - api_schema.gemspec
101
+ - bin/console
102
+ - bin/setup
103
+ - lib/api_schema.rb
104
+ - lib/api_schema/api_version.rb
105
+ - lib/api_schema/configuration.rb
106
+ - lib/api_schema/field.rb
107
+ - lib/api_schema/patches/operation_node.rb
108
+ - lib/api_schema/patches/property_node.rb
109
+ - lib/api_schema/patches/schema_node.rb
110
+ - lib/api_schema/resource_definition.rb
111
+ - lib/api_schema/resource_methods.rb
112
+ - lib/api_schema/response.rb
113
+ - lib/api_schema/root_methods.rb
114
+ - lib/api_schema/serializer_definition.rb
115
+ - lib/api_schema/serializer_methods.rb
116
+ - lib/api_schema/version.rb
117
+ homepage: https://github.com/qonto/api_schema.
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.6.8
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: api_schema provides a framework and DSL for describing APIs and generate
141
+ swagger json.
142
+ test_files: []