swagger_api 0.1.48 → 0.2.1

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.
data/Gemfile CHANGED
@@ -1,6 +1,18 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ source 'https://rubygems.org'
4
4
 
5
- # Specify your gem's dependencies in swagger-api.gemspec
6
- gemspec
5
+ # Specify your gem's dependencies in cubes.gemspec
6
+ gemspec
7
+
8
+ gem 'attr_encrypted'
9
+ gem 'pg'
10
+ gem 'rails'
11
+ gem 'simplecov'
12
+ gem 'webmock'
13
+
14
+ group :development, :test do
15
+ gem 'factory_bot'
16
+ gem 'pry'
17
+ gem 'pry-byebug'
18
+ end
data/Gemfile.lock CHANGED
@@ -53,16 +53,30 @@ GEM
53
53
  i18n (>= 0.7, < 2)
54
54
  minitest (~> 5.1)
55
55
  tzinfo (~> 1.1)
56
+ addressable (2.5.2)
57
+ public_suffix (>= 2.0.2, < 4.0)
56
58
  arel (9.0.0)
59
+ attr_encrypted (3.1.0)
60
+ encryptor (~> 3.0.0)
57
61
  builder (3.2.3)
62
+ byebug (10.0.2)
63
+ coderay (1.1.2)
58
64
  concurrent-ruby (1.0.5)
65
+ crack (0.4.3)
66
+ safe_yaml (~> 1.0.0)
59
67
  crass (1.0.4)
60
68
  diff-lcs (1.3)
69
+ docile (1.3.1)
70
+ encryptor (3.0.0)
61
71
  erubi (1.7.1)
72
+ factory_bot (4.10.0)
73
+ activesupport (>= 3.0.0)
62
74
  globalid (0.4.1)
63
75
  activesupport (>= 4.2.0)
76
+ hashdiff (0.3.7)
64
77
  i18n (1.0.1)
65
78
  concurrent-ruby (~> 1.0)
79
+ json (2.1.0)
66
80
  loofah (2.2.2)
67
81
  crass (~> 1.0.2)
68
82
  nokogiri (>= 1.5.9)
@@ -78,6 +92,14 @@ GEM
78
92
  nio4r (2.3.1)
79
93
  nokogiri (1.8.3)
80
94
  mini_portile2 (~> 2.3.0)
95
+ pg (1.0.0)
96
+ pry (0.11.3)
97
+ coderay (~> 1.1.0)
98
+ method_source (~> 0.9.0)
99
+ pry-byebug (3.6.0)
100
+ byebug (~> 10.0)
101
+ pry (~> 0.10)
102
+ public_suffix (3.0.2)
81
103
  rack (2.0.5)
82
104
  rack-test (1.0.0)
83
105
  rack (>= 1.0, < 3)
@@ -127,6 +149,12 @@ GEM
127
149
  rspec-mocks (~> 3.7.0)
128
150
  rspec-support (~> 3.7.0)
129
151
  rspec-support (3.7.1)
152
+ safe_yaml (1.0.4)
153
+ simplecov (0.16.1)
154
+ docile (~> 1.1)
155
+ json (>= 1.8, < 3)
156
+ simplecov-html (~> 0.10.0)
157
+ simplecov-html (0.10.2)
130
158
  sprockets (3.7.1)
131
159
  concurrent-ruby (~> 1.0)
132
160
  rack (> 1, < 3)
@@ -138,6 +166,10 @@ GEM
138
166
  thread_safe (0.3.6)
139
167
  tzinfo (1.2.5)
140
168
  thread_safe (~> 0.1)
169
+ webmock (3.4.2)
170
+ addressable (>= 2.3.6)
171
+ crack (>= 0.3.2)
172
+ hashdiff
141
173
  websocket-driver (0.7.0)
142
174
  websocket-extensions (>= 0.1.0)
143
175
  websocket-extensions (0.1.3)
@@ -146,10 +178,18 @@ PLATFORMS
146
178
  ruby
147
179
 
148
180
  DEPENDENCIES
181
+ attr_encrypted
149
182
  bundler (~> 1.16)
183
+ factory_bot
184
+ pg
185
+ pry
186
+ pry-byebug
187
+ rails
150
188
  rake (~> 10.0)
151
189
  rspec (~> 3.0)
190
+ simplecov
152
191
  swagger_api!
192
+ webmock
153
193
 
154
194
  BUNDLED WITH
155
195
  1.16.1
data/Rakefile CHANGED
@@ -1,5 +1,7 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_attr'
2
4
 
3
5
  module SwaggerApi
@@ -7,20 +9,14 @@ module SwaggerApi
7
9
  validate :validate_actions
8
10
  attr_accessor :controller
9
11
 
10
-
11
12
  def all!
12
- raise self.errors.full_messages unless self.valid?
13
+ raise errors.full_messages unless valid?
13
14
  return only_actions unless only_actions.blank?
14
15
  defined_actions
15
16
  end
16
17
 
17
18
  def defined_actions
18
- (restful_actions + controller_actions) - except_actions
19
- end
20
-
21
- def controller_actions
22
- return [] unless controller.actions.is_a? Array
23
- controller.actions
19
+ restful_actions - except_actions
24
20
  end
25
21
 
26
22
  def only_actions
@@ -32,12 +28,16 @@ module SwaggerApi
32
28
  end
33
29
 
34
30
  def restful_actions
35
- %w(index show create update delete)
31
+ %w[index show create update delete]
36
32
  end
37
33
 
38
34
  def validate_actions
39
35
  errors.add(:base, "`actions` must include at least one of #{restful_actions}") if restful_actions.blank?
40
- errors.add(:base, "`actions` can only include one of #{restful_actions}. #{(defined_actions - restful_actions)} are not allowed") unless (defined_actions - restful_actions).blank?
36
+ errors.add(:base, "`actions` can only include one of #{restful_actions}. #{(defined_actions - restful_actions)} are not allowed") unless extra_actions.blank?
37
+ end
38
+
39
+ def extra_actions
40
+ defined_actions - restful_actions
41
41
  end
42
42
  end
43
- end
43
+ end
@@ -1,22 +1,33 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwaggerApi
2
4
  class ColumnSchema
3
5
  include ActiveAttr::Model
4
6
  attr_accessor :column
5
7
 
6
8
  def create
7
- schema = {
9
+ schema = default_schema
10
+ if column.type == :integer
11
+ schema[:minimum] = if column.name.to_s.ends_with?('id')
12
+ 1
13
+ else
14
+ 0
15
+ end
16
+ end
17
+ schema
18
+ end
19
+
20
+ def default_schema
21
+ {
8
22
  type: type_from_column,
9
23
  format: format_from_column
10
24
  }
11
- schema[:minimum] = 1 if column.type == :integer && column.name.to_s.ends_with?('id')
12
- schema[:minimum] = 0 if column.type == :integer && !column.name.to_s.ends_with?('id')
13
- schema
14
25
  end
15
26
 
16
27
  def type_from_column
17
- if %i(datetime date time).include?(column.type)
28
+ if %i[datetime date time].include?(column.type)
18
29
  :string
19
- elsif %i(float double).include?(column.type)
30
+ elsif %i[float double].include?(column.type)
20
31
  :number
21
32
  else
22
33
  column.type
@@ -25,16 +36,16 @@ module SwaggerApi
25
36
 
26
37
  def format_from_column
27
38
  case column.type
28
- when :datetime
29
- 'date-time'
30
- when :integer
31
- :int64
39
+ when :datetime
40
+ 'date-time'
41
+ when :integer
42
+ :int64
43
+ else
44
+ if column.name.to_s == 'email'
45
+ :email
32
46
  else
33
- if column.name.to_s == 'email'
34
- :email
35
- else
36
- column.type
37
- end
47
+ column.type
48
+ end
38
49
  end
39
50
  end
40
51
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwaggerApi
2
4
  class ComponentSchema
3
5
  include ActiveAttr::Model
@@ -43,16 +45,18 @@ module SwaggerApi
43
45
 
44
46
  def properties
45
47
  columns.map do |column|
46
- next if column.name.end_with? '_iv'
47
- next if absence_attributes.include? column.name
48
+ next if column_should_skip column.name
48
49
  attribute_name = column.name.gsub(/^encrypted_/, '')
49
50
  [attribute_name, ColumnSchema.new(column: column).create]
50
51
  end.compact.to_h
51
52
  end
52
53
 
54
+ def column_should_skip(column_name)
55
+ column_name.end_with?('_iv') || absence_attributes.include?(column_name)
56
+ end
53
57
 
54
58
  def model
55
59
  controller.model.constantize
56
60
  end
57
61
  end
58
- end
62
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwaggerApi
2
4
  class Components
3
5
  include ActiveAttr::Model
@@ -24,15 +26,19 @@ module SwaggerApi
24
26
  end
25
27
 
26
28
  def create_model_components_from_controller(controller)
27
- if is_sti?(controller.model)
28
- controller.model.constantize.descendants.map do |klass|
29
- [klass.name, ComponentSchema.new(controller: OpenStruct.new(model: klass.name)).create]
30
- end.to_h
29
+ if sti?(controller.model)
30
+ sti_models(controller.model.constantize)
31
31
  else
32
32
  {
33
33
  controller.model => ComponentSchema.new(controller: controller).create
34
34
  }
35
35
  end
36
36
  end
37
+
38
+ def sti_models(model_klass)
39
+ model_klass.descendants.map do |klass|
40
+ [klass.name, ComponentSchema.new(controller: OpenStruct.new(model: klass.name)).create]
41
+ end.to_h
42
+ end
37
43
  end
38
44
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwaggerApi
2
4
  module Concerns
3
5
  module Columns
@@ -5,18 +7,27 @@ module SwaggerApi
5
7
 
6
8
  included do
7
9
  def columns
10
+ filter_except_columns(filter_only_columns(model.columns))
11
+ end
12
+
13
+ def filter_only_columns(columns)
8
14
  if controller.columns.try(:only).present?
9
- model.columns.select! do |column|
15
+ columns.select do |column|
10
16
  controller.columns.only.include?(column.name)
11
17
  end
18
+ else
19
+ columns
12
20
  end
21
+ end
13
22
 
23
+ def filter_except_columns(columns)
14
24
  if controller.columns.try(:except).present?
15
- model.columns.reject! do |column|
25
+ columns.reject do |column|
16
26
  controller.columns.except.include?(column.name)
17
27
  end
28
+ else
29
+ columns
18
30
  end
19
- model.columns
20
31
  end
21
32
  end
22
33
  end
@@ -1,18 +1,20 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwaggerApi
2
4
  module Concerns
3
5
  module StiSchema
4
6
  extend ActiveSupport::Concern
5
7
 
6
8
  included do
7
- def is_sti?(model)
8
- model_klass = model&.safe_constantize || model
9
- return false if model_klass.is_a? String
9
+ def sti?(model)
10
+ model_klass = model.try(:safe_constantize) || model
11
+ return false if model_klass.is_a?(String) || model_klass.nil?
10
12
  Rails.application.eager_load!
11
13
  model_klass.descendants.count != 0
12
14
  end
13
15
 
14
16
  def schema(model)
15
- if is_sti?(model)
17
+ if sti?(model)
16
18
  {
17
19
  oneOf: model.descendants.map do |klass|
18
20
  { '$ref' => "#/components/schemas/#{klass.name}" }
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwaggerApi
2
4
  module Operations
3
5
  class Base
@@ -13,7 +15,7 @@ module SwaggerApi
13
15
  description: "#{readable_action} #{model_name.downcase}'s information",
14
16
  parameters: parameters,
15
17
  responses: responses,
16
- tags: [model_name],
18
+ tags: [model_name]
17
19
  }
18
20
  end
19
21
 
@@ -28,7 +30,7 @@ module SwaggerApi
28
30
  type: :integer,
29
31
  format: :int64,
30
32
  minimum: 1
31
- },
33
+ }
32
34
  }
33
35
  ]
34
36
  end
@@ -88,4 +90,4 @@ module SwaggerApi
88
90
  end
89
91
  end
90
92
  end
91
- end
93
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwaggerApi
2
4
  module Operations
3
5
  class Create < Base
@@ -25,9 +27,9 @@ module SwaggerApi
25
27
 
26
28
  def error_responses
27
29
  super.reject do |error_response|
28
- %w(404).include?(error_response.keys.first)
30
+ %w[404].include?(error_response.keys.first)
29
31
  end
30
32
  end
31
33
  end
32
34
  end
33
- end
35
+ end
@@ -1,20 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwaggerApi
2
4
  module Operations
3
5
  class Delete < Base
4
6
  def success_response
5
7
  {
6
8
  '204' => {
7
- description: "#{readable_action} #{model_name.downcase}'s information",
9
+ description: "#{readable_action} #{model_name.downcase}'s information"
8
10
  }
9
11
  }
10
12
  end
11
13
 
12
-
13
14
  def error_responses
14
15
  super.reject do |error_response|
15
- %w(422).include?(error_response.keys.first)
16
+ %w[422].include?(error_response.keys.first)
16
17
  end
17
18
  end
18
19
  end
19
20
  end
20
- end
21
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwaggerApi
2
4
  module Operations
3
5
  class Index < Base
@@ -7,14 +9,14 @@ module SwaggerApi
7
9
  schema: {
8
10
  type: :integer
9
11
  },
10
- description: 'total results available',
12
+ description: 'total results available'
11
13
  },
12
14
  'x-link-next' => {
13
15
  schema: {
14
16
  type: :string
15
17
  },
16
- description: 'uri for next page of results',
17
- },
18
+ description: 'uri for next page of results'
19
+ }
18
20
  }
19
21
  end
20
22
 
@@ -25,7 +27,7 @@ module SwaggerApi
25
27
  in: 'query',
26
28
  required: false,
27
29
  description: "#{column.name} of #{model.name}",
28
- schema: SwaggerApi::ColumnSchema.new(column: column).create,
30
+ schema: SwaggerApi::ColumnSchema.new(column: column).create
29
31
  }
30
32
  end
31
33
  end
@@ -42,9 +44,9 @@ module SwaggerApi
42
44
 
43
45
  def error_responses
44
46
  super.reject do |error_response|
45
- %w(404 422).include?(error_response.keys.first)
47
+ %w[404 422].include?(error_response.keys.first)
46
48
  end
47
49
  end
48
50
  end
49
51
  end
50
- end
52
+ end