openapi-rails 0.3.2 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 04215cceee526a3cb07ce9e8dd82c372b9783dfa
4
- data.tar.gz: dda8613714499f1fdb7566ac5ba31d0b6568a49f
3
+ metadata.gz: 2b1aa34c54005e99ae4306aa08e2d7497f687327
4
+ data.tar.gz: a287a9ddddd26db598041115cb326be15eba9e06
5
5
  SHA512:
6
- metadata.gz: fac5762cc61ddf457b29c4533b1e8f409f74d614b3d5e9da57c77f3fd6c4552e77d47f301782d2c3f5ea21da2971a88fd4c372f1c01b8f10bb5a5e09df038cc4
7
- data.tar.gz: 3a3302a4499378929d39ea351bba0a7c92149ffaac2e772ef908bbda9c87199ad8c04284fd0dea34a1e338dc8bc279098de6aab6f782a09dc33c4ee46de0ac3e
6
+ metadata.gz: 8976d41adeeb8e08aef3acbf70f11a814d977b1e83868bc157aa861d8c6feb91dc8a840c280d79c4a38cfd90b77a83ed8300b349def8b224932fa1b29baec0e6
7
+ data.tar.gz: 92d5e820340fd7dc6c1c3d76dc03b74db721ae9766d04e6034529973166c349ee1151eede7f5d8192232a523b17c4cacf8d59c5d0ffdb72abbb0f6fbea47f41d
data/Gemfile.lock CHANGED
@@ -1,99 +1,87 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- openapi (0.3.0)
4
+ openapi-rails (0.3.3)
5
5
  bson_ext
6
6
  has_scope
7
- kaminari
8
7
  mongoid
9
8
  responders (~> 2.3)
10
- swagger-blocks
9
+ swagger-blocks (~> 2.0)
11
10
  yajl-ruby
12
11
 
13
12
  GEM
14
13
  remote: http://rubygems.org/
15
14
  specs:
16
- actionpack (4.2.6)
17
- actionview (= 4.2.6)
18
- activesupport (= 4.2.6)
19
- rack (~> 1.6)
20
- rack-test (~> 0.6.2)
21
- rails-dom-testing (~> 1.0, >= 1.0.5)
15
+ actionpack (5.0.0.1)
16
+ actionview (= 5.0.0.1)
17
+ activesupport (= 5.0.0.1)
18
+ rack (~> 2.0)
19
+ rack-test (~> 0.6.3)
20
+ rails-dom-testing (~> 2.0)
22
21
  rails-html-sanitizer (~> 1.0, >= 1.0.2)
23
- actionview (4.2.6)
24
- activesupport (= 4.2.6)
22
+ actionview (5.0.0.1)
23
+ activesupport (= 5.0.0.1)
25
24
  builder (~> 3.1)
26
25
  erubis (~> 2.7.0)
27
- rails-dom-testing (~> 1.0, >= 1.0.5)
26
+ rails-dom-testing (~> 2.0)
28
27
  rails-html-sanitizer (~> 1.0, >= 1.0.2)
29
- activemodel (4.2.6)
30
- activesupport (= 4.2.6)
31
- builder (~> 3.1)
32
- activesupport (4.2.6)
28
+ activemodel (5.0.0.1)
29
+ activesupport (= 5.0.0.1)
30
+ activesupport (5.0.0.1)
31
+ concurrent-ruby (~> 1.0, >= 1.0.2)
33
32
  i18n (~> 0.7)
34
- json (~> 1.7, >= 1.7.7)
35
33
  minitest (~> 5.1)
36
- thread_safe (~> 0.3, >= 0.3.4)
37
34
  tzinfo (~> 1.1)
38
- bson (4.1.1)
35
+ bson (4.2.0)
39
36
  bson_ext (1.5.1)
40
37
  builder (3.2.2)
38
+ concurrent-ruby (1.0.2)
41
39
  erubis (2.7.0)
42
40
  has_scope (0.7.0)
43
41
  actionpack (>= 4.1, < 5.1)
44
42
  activesupport (>= 4.1, < 5.1)
45
43
  i18n (0.7.0)
46
- json (1.8.3)
47
- kaminari (0.17.0)
48
- actionpack (>= 3.0.0)
49
- activesupport (>= 3.0.0)
50
44
  loofah (2.0.3)
51
45
  nokogiri (>= 1.5.9)
46
+ method_source (0.8.2)
52
47
  mini_portile2 (2.1.0)
53
- minitest (5.9.0)
54
- mongo (2.3.0)
55
- bson (~> 4.1)
56
- mongoid (5.1.3)
57
- activemodel (~> 4.0)
58
- mongo (~> 2.1)
59
- origin (~> 2.2)
60
- tzinfo (>= 0.3.37)
61
- nokogiri (1.6.8)
48
+ minitest (5.10.1)
49
+ mongo (2.4.0)
50
+ bson (~> 4.2.0)
51
+ mongoid (6.0.3)
52
+ activemodel (~> 5.0)
53
+ mongo (~> 2.3)
54
+ nokogiri (1.6.8.1)
62
55
  mini_portile2 (~> 2.1.0)
63
- pkg-config (~> 1.1.7)
64
- origin (2.2.0)
65
- pkg-config (1.1.7)
66
- rack (1.6.4)
56
+ rack (2.0.1)
67
57
  rack-test (0.6.3)
68
58
  rack (>= 1.0)
69
- rails-deprecated_sanitizer (1.0.3)
70
- activesupport (>= 4.2.0.alpha)
71
- rails-dom-testing (1.0.7)
72
- activesupport (>= 4.2.0.beta, < 5.0)
59
+ rails-dom-testing (2.0.1)
60
+ activesupport (>= 4.2.0, < 6.0)
73
61
  nokogiri (~> 1.6.0)
74
- rails-deprecated_sanitizer (>= 1.0.1)
75
62
  rails-html-sanitizer (1.0.3)
76
63
  loofah (~> 2.0)
77
- railties (4.2.6)
78
- actionpack (= 4.2.6)
79
- activesupport (= 4.2.6)
64
+ railties (5.0.0.1)
65
+ actionpack (= 5.0.0.1)
66
+ activesupport (= 5.0.0.1)
67
+ method_source
80
68
  rake (>= 0.8.7)
81
69
  thor (>= 0.18.1, < 2.0)
82
- rake (11.2.2)
70
+ rake (12.0.0)
83
71
  responders (2.3.0)
84
72
  railties (>= 4.2.0, < 5.1)
85
- swagger-blocks (1.3.4)
86
- thor (0.19.1)
73
+ swagger-blocks (2.0.0)
74
+ thor (0.19.4)
87
75
  thread_safe (0.3.5)
88
76
  tzinfo (1.2.2)
89
77
  thread_safe (~> 0.1)
90
- yajl-ruby (1.2.1)
78
+ yajl-ruby (1.3.0)
91
79
 
92
80
  PLATFORMS
93
81
  ruby
94
82
 
95
83
  DEPENDENCIES
96
- openapi!
84
+ openapi-rails!
97
85
 
98
86
  BUNDLED WITH
99
87
  1.12.4
data/README.md CHANGED
@@ -10,6 +10,7 @@ integration.
10
10
 
11
11
  **This gem supports only Mongoid, for other ORMs RPs are welcome.**
12
12
 
13
+ [Demo](https://openapi-demo1.herokuapp.com/openapi#/Books) project for basic `openapi-rails` integration.
13
14
 
14
15
  ## Installation
15
16
 
@@ -97,7 +98,6 @@ manually.
97
98
 
98
99
  Following features are supported out of the box:
99
100
 
100
- - pagination via [kaminari](https://github.com/amatsuda/kaminari)
101
101
  - has_scope via [has_scope](https://github.com/plataformatec/has_scope)
102
102
  - search via [mongoid-search](https://github.com/mauriciozaffari/mongoid_search)
103
103
  - version via [mongoid-history](https://github.com/aq1018/mongoid-history)
@@ -109,9 +109,20 @@ Following features are supported out of the box:
109
109
 
110
110
  In the controller there is a way override default behaviour with helpers:
111
111
 
112
- - `per_page` — set page size (default `50`) for `index` action
113
- - `resource_class` — set model class manually
114
- - `resource_params` — override default method that allows everything
112
+ - `paginates_per(number)` — set page size (default `50`) for `index` action
113
+ - `resource_class(klass)` — set model class manually
114
+ - `def resource_params` — override default method that allows everything
115
+
116
+ Helpers to customize specification build:
117
+
118
+ - `spec_params(options)`
119
+
120
+ Supported specification `options`:
121
+ - `collection_name` —
122
+ - `resource_name` —
123
+ - `resource_class` —
124
+ - `except_actions` —
125
+ - `relative_path` —
115
126
 
116
127
 
117
128
  ## Custom Actions
@@ -214,6 +225,12 @@ Controllers with custom logic would be placed at `app/controllers/api/v1` and
214
225
  ![OpenAPI Rails — Multiple versions demo](https://d3vv6lp55qjaqc.cloudfront.net/items/3J200Q0m2m0V2m0Q3V2i/openapi-rails-multiple-versions.png)
215
226
 
216
227
 
228
+ ## Related Documents
229
+
230
+ - [OpenAPI ❤️ Rails](http://www.kra.vc/openapi-rails) — tutorial for setting
231
+ up a new rails project with OpenAPI support.
232
+
233
+
217
234
  ## Contributors
218
235
 
219
236
  - [Alexander Kravets](http://www.kra.vc)
data/lib/openapi.rb CHANGED
@@ -7,20 +7,21 @@ require 'rails/routes'
7
7
  require 'has_scope'
8
8
  require 'responders'
9
9
  require 'renderers/csv'
10
- require 'kaminari'
11
- require 'openapi/mongoid/crud_actions'
12
10
 
13
11
  require 'swagger/blocks/schema_builder'
14
12
  require 'swagger/blocks/schema_node'
15
13
  require 'swagger/blocks/property_node'
16
14
  require 'swagger/blocks/items_node'
17
- require 'openapi/mongoid/spec_builder'
18
15
 
19
16
  require 'openapi/configuration'
20
17
  require 'openapi/routes_parser'
21
18
  require 'openapi/engine'
19
+ require 'openapi/middleware'
22
20
  require 'openapi/version'
23
21
 
22
+ require 'openapi/mongoid' if defined?(Mongoid)
23
+ require 'openapi/activerecord' if defined?(ActiveRecord)
24
+
24
25
  module Openapi
25
26
  extend Configuration
26
27
  end
File without changes
@@ -1,6 +1,7 @@
1
1
  module Openapi
2
2
  module Configuration
3
3
  attr_accessor :apis
4
+ attr_accessor :has_specs
4
5
 
5
6
  def configure
6
7
  yield self
@@ -12,6 +13,7 @@ module Openapi
12
13
 
13
14
  def set_default_configuration
14
15
  self.apis = {}
16
+ self.has_specs = false
15
17
  end
16
18
  end
17
19
  end
@@ -7,26 +7,10 @@ module Openapi
7
7
  openapi/favicon-32x32.png
8
8
  openapi/favicon-16x16.png)
9
9
  end
10
- end
11
-
12
- class Railtie < Rails::Railtie
13
- initializer 'openapi.builders', after: :load_config_initializers do
14
- Rails.application.reload_routes!
15
-
16
- Openapi.apis.each do |name, config|
17
- base_path = config[:base_path] || '/api'
18
-
19
- config[:controllers].each do |controller|
20
- controller.build_openapi_specification(base_path: base_path)
21
- end
22
-
23
- name = name.to_s.titleize.remove(' ')
24
- root_klass_name = "#{name}SwaggerRootController"
25
- klass = Object.const_set root_klass_name, Class.new(SwaggerRoot)
26
- klass.build_specification(config, config[:controllers])
27
10
 
28
- config[:controllers].push klass
29
- end
11
+ initializer 'openapi.builders_middleware' do
12
+ # NOTE: Use middleware cause we need application routes to be loaded.
13
+ Rails.application.middleware.use Openapi::Middleware
30
14
  end
31
15
  end
32
16
  end
@@ -0,0 +1,31 @@
1
+ module Openapi
2
+ class Middleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ unless Openapi.has_specs
9
+ puts "[Openapi::Middleware] Building openapi specs..."
10
+ Openapi.apis.each do |name, config|
11
+ base_path = config[:base_path] || '/api'
12
+
13
+ config[:controllers].each do |controller|
14
+ controller.build_openapi_specification(base_path: base_path)
15
+ end
16
+
17
+ name = name.to_s.titleize.remove(' ')
18
+ root_klass_name = "#{name}SwaggerRootController"
19
+ klass = Object.const_set root_klass_name, Class.new(SwaggerRoot)
20
+ klass.build_specification(config, config[:controllers])
21
+
22
+ config[:controllers].push klass
23
+ end
24
+
25
+ Openapi.has_specs = true
26
+ end
27
+
28
+ @app.call(env)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,2 @@
1
+ require 'openapi/mongoid/crud_actions'
2
+ require 'openapi/mongoid/spec_builder'
@@ -3,12 +3,22 @@ module Openapi
3
3
  module CrudActions
4
4
  extend ActiveSupport::Concern
5
5
 
6
+ class_methods do
7
+ def resource_class(klass)
8
+ self.crud_resource_class = klass
9
+ end
10
+
11
+ def paginates_per(number)
12
+ self.crud_per_page = number
13
+ end
14
+ end
15
+
6
16
  included do
7
17
  respond_to :json
8
18
  respond_to :csv, only: %w(index)
9
19
 
10
- class_attribute :resource_class
11
- class_attribute :per_page
20
+ class_attribute :crud_resource_class
21
+ class_attribute :crud_per_page
12
22
 
13
23
  ## Actions
14
24
 
@@ -104,7 +114,7 @@ module Openapi
104
114
  end
105
115
 
106
116
  def resource_class
107
- @resource_class ||= self.class.resource_class
117
+ @resource_class ||= self.class.crud_resource_class
108
118
  @resource_class ||= self.class.
109
119
  to_s.
110
120
  split('::').
@@ -147,40 +157,48 @@ module Openapi
147
157
  end
148
158
 
149
159
  def search_filter_chain!
150
- query = params[:search]
151
- if query && support_search?
160
+ query = params[:search] || ''
161
+ if !query.empty? && support_search?
152
162
  normalized_query = query.to_s.downcase
153
163
  @chain = @chain.search(normalized_query, match: :all)
154
164
  end
155
165
  end
156
166
 
157
167
  def page
158
- @page ||= params[:page]
168
+ @page ||= (params[:page] || 1).to_i
159
169
  end
160
170
 
161
171
  def per_page
162
- @per_page ||= (params[:perPage] || self.class.per_page || 50)
163
- end
164
-
165
- def paginate_chain!
166
- @chain = begin
167
- if page
168
- @chain.page(page).per(per_page)
172
+ @per_page ||= begin
173
+ value = params[:perPage].try(:to_i)
174
+ if value.nil?
175
+ self.class.crud_per_page || 25
176
+ elsif value == 0
177
+ 25
169
178
  else
170
- @chain.all
179
+ value
171
180
  end
172
181
  end
173
182
  end
174
183
 
184
+ def paginate_chain!
185
+ @unpaginated_chain = @chain
186
+ @chain = @chain.skip((page - 1) * per_page).limit(per_page)
187
+ end
188
+
189
+ def last_page?(total_number)
190
+ total_number <= (page - 1) * per_page + per_page
191
+ end
192
+
175
193
  def set_index_headers!
176
- total_objects = page ? @chain.total_count : @chain.size
177
- response.headers['X-Total-Count'] = total_objects
178
-
179
- if page and !@chain.last_page?
180
- url = request.url
181
- url.gsub!("page=#{page}", "page=#{@chain.next_page}")
182
- next_page = "<#{url}>; rel=\"next\""
183
- response.headers['Link'] = next_page
194
+ unpaginated_chain_size = @chain.count
195
+ response.headers['X-Page'] = page
196
+ response.headers['X-Total-Count'] = unpaginated_chain_size
197
+ response.headers['X-Pages-Count'] = (unpaginated_chain_size.to_f / per_page).ceil
198
+ response.headers['X-Per-Page'] = per_page
199
+
200
+ unless last_page?(unpaginated_chain_size)
201
+ response.headers['X-Next-Page'] = page + 1
184
202
  end
185
203
  end
186
204
 
@@ -204,16 +222,6 @@ module Openapi
204
222
  gsub('::', '_')
205
223
  end
206
224
  end
207
-
208
- class_methods do
209
- def resource_class(name)
210
- self.resource_class = name
211
- end
212
-
213
- def per_page(number)
214
- self.per_page = number
215
- end
216
- end
217
225
  end
218
226
  end
219
227
  end
@@ -21,7 +21,7 @@ module Openapi
21
21
 
22
22
  controller_classes.each do |c|
23
23
  tag do
24
- key :name, c.openapi_collection_name
24
+ key :name, c.spec_collection_name
25
25
  end
26
26
  end
27
27
  end
@@ -37,42 +37,42 @@ module Openapi
37
37
  included do
38
38
  include Swagger::Blocks
39
39
 
40
- class_attribute :openapi_collection_name
41
- class_attribute :openapi_resource_name
42
- class_attribute :openapi_resource_class
43
- class_attribute :openapi_except_actions
44
- class_attribute :openapi_relative_path
40
+ class_attribute :spec_collection_name
41
+ class_attribute :spec_resource_name
42
+ class_attribute :spec_resource_class
43
+ class_attribute :spec_except_actions
44
+ class_attribute :spec_relative_path
45
45
 
46
- class_attribute :openapi_base_path
46
+ class_attribute :spec_base_path
47
47
  end
48
48
 
49
49
  class_methods do
50
- def openapi_config(options)
51
- self.openapi_collection_name = options[:collection_name]
52
- self.openapi_resource_name = options[:resource_name]
53
- self.openapi_resource_class = options[:resource_class]
54
- self.openapi_except_actions = options[:except_actions]
55
- self.openapi_relative_path = options[:relative_path]
50
+ def spec_params(options)
51
+ self.spec_collection_name = options[:collection_name]
52
+ self.spec_resource_name = options[:resource_name]
53
+ self.spec_resource_class = options[:resource_class]
54
+ self.spec_except_actions = options[:except_actions]
55
+ self.spec_relative_path = options[:relative_path]
56
56
  end
57
57
 
58
58
  def build_openapi_specification(options)
59
- self.openapi_base_path = options[:base_path]
59
+ self.spec_base_path = options[:base_path]
60
60
 
61
- self.openapi_relative_path ||=
61
+ self.spec_relative_path ||=
62
62
  ('/' + to_s.remove(/Controller$/).gsub('::', '/').underscore).
63
- remove(openapi_base_path)
63
+ remove(spec_base_path)
64
64
 
65
- self.openapi_except_actions ||= []
65
+ self.spec_except_actions ||= []
66
66
 
67
- self.openapi_collection_name ||=
67
+ self.spec_collection_name ||=
68
68
  to_s.split('::').last.sub(/Controller$/, '')
69
69
 
70
- self.openapi_resource_name ||=
71
- self.openapi_collection_name.singularize
70
+ self.spec_resource_name ||=
71
+ self.spec_collection_name.singularize
72
72
 
73
- self.openapi_resource_class ||= self.try(:resource_class)
74
- self.openapi_resource_class ||=
75
- self.openapi_resource_name.constantize
73
+ self.spec_resource_class ||= self.try(:crud_resource_class)
74
+ self.spec_resource_class ||=
75
+ self.spec_resource_name.constantize
76
76
 
77
77
  build_openapi_definitions
78
78
  build_openapi_paths
@@ -88,9 +88,9 @@ module Openapi
88
88
  end
89
89
 
90
90
  def build_openapi_definitions
91
- collection_name = openapi_collection_name
92
- resource_class = openapi_resource_class
93
- resource_name = openapi_resource_name
91
+ collection_name = spec_collection_name
92
+ resource_class = spec_resource_class
93
+ resource_name = spec_resource_name
94
94
  resource_property_name = resource_name.underscore.to_sym
95
95
 
96
96
  swagger_schema resource_name do
@@ -103,6 +103,12 @@ module Openapi
103
103
  relation_name =
104
104
  relation.name.to_s.singularize.titleize.remove(' ')
105
105
 
106
+ # TODO: This is not accurate, class might be in the same
107
+ # namespace as related.
108
+ # namespace = resource_class.to_s.split('::')[0...-1].join('::')
109
+ # generated_embedded_resource_class_name = [namespace, relation_name].join('::')
110
+
111
+ # TODO: Add error handler in case class is not guessed here.
106
112
  embedded_resource_class =
107
113
  (relation.class_name || relation_name).constantize
108
114
 
@@ -131,32 +137,31 @@ module Openapi
131
137
  end
132
138
 
133
139
  def build_crud_specification(routes)
134
- name = openapi_resource_name
140
+ name = spec_resource_name
135
141
  sym_name = name.underscore.to_sym
136
- plural_name = openapi_collection_name
137
- path = openapi_relative_path
142
+ plural_name = spec_collection_name
143
+ path = spec_relative_path
138
144
  scopes = try(:scopes_configuration) || []
139
145
  actions = routes.map {|r| r[2]}.uniq
140
146
  json_mime = %w(application/json)
141
147
 
142
148
  include_index = actions.include?('index') &&
143
- !openapi_except_actions.include?('index')
149
+ !spec_except_actions.include?('index')
144
150
  include_create = actions.include?('create') &&
145
- !openapi_except_actions.include?('create')
151
+ !spec_except_actions.include?('create')
146
152
  include_show = actions.include?('show') &&
147
- !openapi_except_actions.include?('show')
153
+ !spec_except_actions.include?('show')
148
154
  include_update = actions.include?('update') &&
149
- !openapi_except_actions.include?('update')
155
+ !spec_except_actions.include?('update')
150
156
  include_destroy = actions.include?('destroy') &&
151
- !openapi_except_actions.include?('destroy')
157
+ !spec_except_actions.include?('destroy')
152
158
 
153
159
  include_collection_actions =
154
160
  (include_index || include_create)
155
161
  include_resource_actions =
156
162
  (include_show || include_update || include_destroy)
157
163
 
158
- support_search = openapi_resource_class.instance_methods(false)
159
- .include?(:search)
164
+ support_search = spec_resource_class.methods.include?(:search)
160
165
 
161
166
  if include_collection_actions
162
167
  swagger_path path do
@@ -426,7 +431,7 @@ module Openapi
426
431
  custom_routes = routes.select {|r| !CRUD_ACTIONS.include?(r[2])}
427
432
  no_spec_methods = custom_routes.select do |route|
428
433
  method = route[0].to_sym
429
- path = route[1].remove(openapi_base_path)
434
+ path = route[1].remove(spec_base_path)
430
435
  path_sym = path.gsub(/:(\w+)/, '{\1}').to_sym
431
436
 
432
437
  ! action_specification_exists?(method, path_sym)
@@ -1,3 +1,3 @@
1
1
  module Openapi
2
- VERSION = '0.3.2'.freeze
2
+ VERSION = '0.3.5'.freeze
3
3
  end
@@ -1,7 +1,9 @@
1
1
  module Swagger
2
2
  module Blocks
3
- class ItemsNode
4
- include SchemaBuilder
3
+ module Nodes
4
+ class ItemsNode
5
+ include SchemaBuilder
6
+ end
5
7
  end
6
8
  end
7
9
  end
@@ -1,7 +1,9 @@
1
1
  module Swagger
2
2
  module Blocks
3
- class PropertyNode
4
- include SchemaBuilder
3
+ module Nodes
4
+ class PropertyNode
5
+ include SchemaBuilder
6
+ end
5
7
  end
6
8
  end
7
9
  end
@@ -1,85 +1,87 @@
1
1
  module Swagger
2
2
  module Blocks
3
- module SchemaBuilder
4
- extend ActiveSupport::Concern
5
-
6
- SUPPORTED_TYPES = %w(Mongoid::Boolean
7
- BSON::ObjectId
8
- Object
9
- Time
10
- String
11
- Integer
12
- Array
13
- Date
14
- Symbol).freeze
15
-
16
- def get_required_fields(model_class)
17
- presence_validators = model_class.
18
- validators.
19
- select { |v| v.class == Mongoid::Validatable::PresenceValidator }
20
-
21
- required_fields = presence_validators.map { |v| v.attributes.first }
22
- required_fields << '_id'
23
- required_fields
24
- end
25
-
26
- def build_model_schema(model_class, include_only_required_fields=false)
27
- required_fields = get_required_fields(model_class)
3
+ module Nodes
4
+ module SchemaBuilder
5
+ extend ActiveSupport::Concern
6
+
7
+ SUPPORTED_TYPES = %w(Mongoid::Boolean
8
+ BSON::ObjectId
9
+ Object
10
+ Time
11
+ String
12
+ Integer
13
+ Array
14
+ Date
15
+ Symbol).freeze
16
+
17
+ def get_required_fields(model_class)
18
+ presence_validators = model_class.
19
+ validators.
20
+ select { |v| v.class == Mongoid::Validatable::PresenceValidator }
21
+
22
+ required_fields = presence_validators.map { |v| v.attributes.first }
23
+ required_fields << '_id'
24
+ required_fields
25
+ end
28
26
 
29
- key :required, required_fields
27
+ def build_model_schema(model_class, include_only_required_fields=false)
28
+ required_fields = get_required_fields(model_class)
30
29
 
31
- model_class.fields.each do |name, options|
32
- type = options.type.to_s
33
- defaul_value = options.options[:default]
30
+ key :required, required_fields
34
31
 
35
- next unless SUPPORTED_TYPES.include?(type)
32
+ model_class.fields.each do |name, options|
33
+ type = options.type.to_s
34
+ defaul_value = options.options[:default]
36
35
 
37
- if include_only_required_fields
38
- next if name == '_id'
39
- next unless required_fields.include?(name.to_sym)
40
- end
36
+ next unless SUPPORTED_TYPES.include?(type)
41
37
 
42
- property name do
43
- case type
44
- when 'Symbol'
45
- klass = options.options[:klass].to_s
46
- constant = name.sub('_', '').upcase
47
- values = "#{klass}::#{constant}".constantize
38
+ if include_only_required_fields
39
+ next if name == '_id'
40
+ next unless required_fields.include?(name.to_sym)
41
+ end
48
42
 
49
- key :type, :string
50
- key :enum, values
43
+ property name do
44
+ case type
45
+ when 'Symbol'
46
+ klass = options.options[:klass].to_s
47
+ constant = name.sub('_', '').upcase
48
+ values = "#{klass}::#{constant}".constantize
51
49
 
52
- when 'Array'
53
- key :type, :array
54
- # TODO: autodetect type of Array Item
55
- items do
56
50
  key :type, :string
57
- end
51
+ key :enum, values
58
52
 
59
- when 'BSON::ObjectId'
60
- key :type, :string
61
- key :format, :uuid
53
+ when 'Array'
54
+ key :type, :array
55
+ # TODO: autodetect type of Array Item
56
+ items do
57
+ key :type, :string
58
+ end
62
59
 
63
- when 'Date'
64
- key :type, :string
65
- key :format, :date
60
+ when 'BSON::ObjectId'
61
+ key :type, :string
62
+ key :format, :uuid
63
+
64
+ when 'Date'
65
+ key :type, :string
66
+ key :format, :date
66
67
 
67
- when 'Time'
68
- key :type, :string
69
- key :format, 'date-time'
68
+ when 'Time'
69
+ key :type, :string
70
+ key :format, 'date-time'
70
71
 
71
- when 'Mongoid::Boolean'
72
- key :type, :boolean
73
- key :default, defaul_value
72
+ when 'Mongoid::Boolean'
73
+ key :type, :boolean
74
+ key :default, defaul_value
74
75
 
75
- when 'Integer'
76
- key :type, :integer
77
- key :default, defaul_value.to_i
76
+ when 'Integer'
77
+ key :type, :integer
78
+ key :default, defaul_value.to_i
78
79
 
79
- else
80
- key :type, :string
81
- key :default, defaul_value.to_s
80
+ else
81
+ key :type, :string
82
+ key :default, defaul_value.to_s
82
83
 
84
+ end
83
85
  end
84
86
  end
85
87
  end
@@ -1,7 +1,9 @@
1
1
  module Swagger
2
2
  module Blocks
3
- class SchemaNode
4
- include SchemaBuilder
3
+ module Nodes
4
+ class SchemaNode
5
+ include SchemaBuilder
6
+ end
5
7
  end
6
8
  end
7
9
  end
@@ -19,17 +19,13 @@ Gem::Specification.new do |s|
19
19
  s.platform = Gem::Platform::RUBY
20
20
 
21
21
  # DSL for pure Ruby code blocks that can be turned into JSON
22
- s.add_dependency 'swagger-blocks'
22
+ s.add_dependency 'swagger-blocks', '~> 2.0'
23
23
  # A set of Rails responders to dry up application controllers
24
24
  s.add_dependency 'responders', '~> 2.3'
25
- # Clean, powerful, customizable and sophisticated paginator
26
- s.add_dependency 'kaminari'
27
25
  # C extensions to accelerate the Ruby BSON serialization
28
26
  s.add_dependency 'bson_ext'
29
27
  # Fast streaming JSON parsing and encoding library for Ruby
30
28
  s.add_dependency 'yajl-ruby'
31
- # Ruby ODM framework for MongoDB
32
- s.add_dependency 'mongoid'
33
29
  # Map incoming controller parameters to named scopes in resources
34
30
  s.add_dependency 'has_scope'
35
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Kravets
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-09-01 00:00:00.000000000 Z
12
+ date: 2017-01-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: swagger-blocks
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ">="
18
+ - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '0'
20
+ version: '2.0'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ">="
25
+ - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '0'
27
+ version: '2.0'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: responders
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -39,20 +39,6 @@ dependencies:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
41
  version: '2.3'
42
- - !ruby/object:Gem::Dependency
43
- name: kaminari
44
- requirement: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - ">="
47
- - !ruby/object:Gem::Version
48
- version: '0'
49
- type: :runtime
50
- prerelease: false
51
- version_requirements: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- version: '0'
56
42
  - !ruby/object:Gem::Dependency
57
43
  name: bson_ext
58
44
  requirement: !ruby/object:Gem::Requirement
@@ -81,20 +67,6 @@ dependencies:
81
67
  - - ">="
82
68
  - !ruby/object:Gem::Version
83
69
  version: '0'
84
- - !ruby/object:Gem::Dependency
85
- name: mongoid
86
- requirement: !ruby/object:Gem::Requirement
87
- requirements:
88
- - - ">="
89
- - !ruby/object:Gem::Version
90
- version: '0'
91
- type: :runtime
92
- prerelease: false
93
- version_requirements: !ruby/object:Gem::Requirement
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- version: '0'
98
70
  - !ruby/object:Gem::Dependency
99
71
  name: has_scope
100
72
  requirement: !ruby/object:Gem::Requirement
@@ -163,8 +135,11 @@ files:
163
135
  - lib/generators/openapi/templates/openapi.rb
164
136
  - lib/openapi-rails.rb
165
137
  - lib/openapi.rb
138
+ - lib/openapi/activerecord.rb
166
139
  - lib/openapi/configuration.rb
167
140
  - lib/openapi/engine.rb
141
+ - lib/openapi/middleware.rb
142
+ - lib/openapi/mongoid.rb
168
143
  - lib/openapi/mongoid/crud_actions.rb
169
144
  - lib/openapi/mongoid/spec_builder.rb
170
145
  - lib/openapi/routes_parser.rb