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.
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwaggerApi
2
4
  module Operations
3
5
  class Show < Base
4
6
  def error_responses
5
7
  super.reject do |error_response|
6
- %w(422).include?(error_response.keys.first)
8
+ %w[422].include?(error_response.keys.first)
7
9
  end
8
10
  end
9
11
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwaggerApi
2
4
  module Operations
3
5
  class Update < Base
@@ -23,4 +25,4 @@ module SwaggerApi
23
25
  end
24
26
  end
25
27
  end
26
- end
28
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwaggerApi
2
4
  class Paths
3
5
  include ActiveAttr::Model
@@ -20,16 +22,40 @@ module SwaggerApi
20
22
 
21
23
  def paths(controller)
22
24
  paths = {}
23
- paths["#{route(controller)}"] = {}
25
+ paths[route(controller)] = {}
24
26
  paths["#{route(controller)}{id}/"] = {}
25
- paths["#{route(controller)}"][:get] = SwaggerApi::Operations::Index.new(controller: controller).create if action?(controller, 'index')
26
- paths["#{route(controller)}"][:post] = SwaggerApi::Operations::Create.new(controller: controller).create if action?(controller, 'create')
27
+ collection_paths(paths, controller)
28
+ member_paths(paths, controller)
29
+ paths
30
+ end
31
+
32
+ def collection_paths(paths, controller)
33
+ paths[route(controller)][:get] = SwaggerApi::Operations::Index.new(controller: controller).create if action?(controller, 'index')
34
+ paths[route(controller)][:post] = SwaggerApi::Operations::Create.new(controller: controller).create if action?(controller, 'create')
35
+ end
36
+
37
+ def member_paths(paths, controller)
38
+ show_path(paths, controller)
39
+ update_path(paths, controller)
40
+ delete_path(paths, controller)
41
+ clean_out_delete_path(paths, controller)
42
+ end
43
+
44
+ def show_path(paths, controller)
27
45
  paths["#{route(controller)}{id}/"][:get] = SwaggerApi::Operations::Show.new(controller: controller).create if action?(controller, 'show')
46
+ end
47
+
48
+ def update_path(paths, controller)
28
49
  paths["#{route(controller)}{id}/"][:put] = SwaggerApi::Operations::Update.new(controller: controller).create if action?(controller, 'update')
50
+ end
51
+
52
+ def delete_path(paths, controller)
29
53
  paths["#{route(controller)}{id}/"][:delete] = SwaggerApi::Operations::Delete.new(controller: controller).create if action?(controller, 'delete')
30
- paths["#{route(controller)}"].delete if paths["#{route(controller)}"].blank?
54
+ end
55
+
56
+ def clean_out_delete_path(paths, controller)
57
+ paths[route(controller)].delete if paths[route(controller)].blank?
31
58
  paths["#{route(controller)}{id}/"].delete if paths["#{route(controller)}{id}/"].blank?
32
- paths
33
59
  end
34
60
 
35
61
  def route(controller)
@@ -1,6 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails'
2
- class SwaggerApi::Railtie < Rails::Railtie
3
- rake_tasks do
4
- load 'swagger_api/tasks/swagger.rake'
4
+
5
+ #:nocov:#
6
+ module SwaggerApi
7
+ class Railtie < Rails::Railtie
8
+ rake_tasks do
9
+ load 'swagger_api/tasks/swagger.rake'
10
+ end
5
11
  end
6
- end
12
+ end
13
+ #:nocov:#
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwaggerApi
2
4
  class RequestBodies
3
5
  include ActiveAttr::Model
@@ -17,7 +19,7 @@ module SwaggerApi
17
19
  {
18
20
  content: {
19
21
  'application/json' => {
20
- schema: schema(controller.model.safe_constantize || controller.model)
22
+ schema: schema(controller.model.try(:safe_constantize) || controller.model)
21
23
  }
22
24
  },
23
25
  description: "#{controller.model} attribute",
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  namespace :swagger do
3
4
  desc 'Create a local version of the swagger api json file'
@@ -10,4 +11,4 @@ namespace :swagger do
10
11
  file.write(generator.prettify)
11
12
  end
12
13
  end
13
- end
14
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwaggerApi
2
- VERSION = "0.1.48"
4
+ VERSION = '0.2.1'
3
5
  end
data/lib/swagger_api.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'swagger_api/version'
2
4
  require 'rails'
3
5
 
@@ -14,21 +16,25 @@ module SwaggerApi
14
16
  def create
15
17
  @config ||= {
16
18
  openapi: '3.0.0',
17
- security: [{api_key: []}],
19
+ security: [{ api_key: [] }],
18
20
  info: info,
19
- servers: [{url: server_url}],
21
+ servers: [{ url: server_url }],
20
22
  paths: Paths.new(controllers: config.controllers).create,
21
- components: {
22
- responses: responses,
23
- schemas: Components.new(controllers: config.controllers).create,
24
- requestBodies: RequestBodies.new(controllers: config.controllers).create,
25
- securitySchemes: security_schemes,
26
- },
23
+ components: components
24
+ }
25
+ end
26
+
27
+ def components
28
+ {
29
+ responses: responses,
30
+ schemas: Components.new(controllers: config.controllers).create,
31
+ requestBodies: RequestBodies.new(controllers: config.controllers).create,
32
+ securitySchemes: security_schemes
27
33
  }
28
34
  end
29
35
 
30
36
  def config
31
- @yaml_config ||= JSON.parse(YAML.load_file("#{Rails.root.to_s}/config/swagger.yml").to_json, object_class: OpenStruct)
37
+ @yaml_config ||= JSON.parse(YAML.load_file("#{Rails.root}/config/swagger.yml").to_json, object_class: OpenStruct)
32
38
  end
33
39
 
34
40
  def security_schemes
@@ -42,19 +48,17 @@ module SwaggerApi
42
48
  end
43
49
 
44
50
  def responses
45
- @responses ||= custom_responses || default_responses
51
+ @responses ||= default_responses
46
52
  end
47
53
 
48
- def custom_responses
49
- custom_responses_path = "#{Rails.root.to_s}/config/swagger_custom_responses.json"
50
- return unless File.exist?(custom_responses_path)
51
- @custom_responses ||= JSON.parse(File.read(custom_responses_path), object_class: OpenStruct)
54
+ def default_responses
55
+ {}.merge(default_not_found_response).merge(default_unauthorized_response).merge(default_bad_request_response)
52
56
  end
53
57
 
54
- def default_responses
58
+ def default_not_found_response
55
59
  {
56
60
  NotFound: {
57
- description: "The specified resource was not found",
61
+ description: 'The specified resource was not found',
58
62
  content: {
59
63
  'application/json; charset=utf-8' => {
60
64
  schema: {
@@ -63,9 +67,14 @@ module SwaggerApi
63
67
  }
64
68
  }
65
69
  }
66
- },
70
+ }
71
+ }
72
+ end
73
+
74
+ def default_unauthorized_response
75
+ {
67
76
  Unauthorized: {
68
- description: "Unauthorized",
77
+ description: 'Unauthorized',
69
78
  content: {
70
79
  'application/json; charset=utf-8' => {
71
80
  schema: {
@@ -74,9 +83,14 @@ module SwaggerApi
74
83
  }
75
84
  }
76
85
  }
77
- },
86
+ }
87
+ }
88
+ end
89
+
90
+ def default_bad_request_response
91
+ {
78
92
  BadRequest: {
79
- description: "Bad Request",
93
+ description: 'Bad Request',
80
94
  content: {
81
95
  'application/json; charset=utf-8' => {
82
96
  schema: {
@@ -96,12 +110,12 @@ module SwaggerApi
96
110
  {
97
111
  version: config.info.version,
98
112
  title: config.info.title,
99
- description: config.info.description,
113
+ description: config.info.description
100
114
  }
101
115
  end
102
116
 
103
117
  def server_url
104
- return unless config.server.respond_to?(Rails.env)
118
+ return unless config.servers.respond_to?(Rails.env)
105
119
  config.servers.send(Rails.env).url
106
120
  end
107
121
  end
data/swagger_api.gemspec CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  lib = File.expand_path('../lib', __FILE__)
2
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
5
  require 'swagger_api/version'
@@ -8,8 +10,8 @@ Gem::Specification.new do |spec|
8
10
  spec.authors = ['Full Measure Education']
9
11
  spec.email = ['devops@fullmeasureed.com']
10
12
 
11
- spec.summary = %q{A simple library for autocreating RESTful swagger api docs from Rails controllers.}
12
- spec.description = %q{A simple library for autocreating RESTful swagger api docs from Rails controllers.}
13
+ spec.summary = 'A simple library for autocreating RESTful swagger api docs from Rails controllers.'
14
+ spec.description = 'A simple library for autocreating RESTful swagger api docs from Rails controllers.'
13
15
  spec.homepage = 'https://gitlab.com/fullmeasure/public/swagger-api/swagger_api'
14
16
  spec.license = 'MIT'
15
17
 
@@ -18,8 +20,7 @@ Gem::Specification.new do |spec|
18
20
  if spec.respond_to?(:metadata)
19
21
  spec.metadata['allowed_push_host'] = 'https://rubygems.org'
20
22
  else
21
- raise 'RubyGems 2.0 or newer is required to protect against ' \
22
- 'public gem pushes.'
23
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
23
24
  end
24
25
 
25
26
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagger_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.48
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Full Measure Education
@@ -106,6 +106,7 @@ files:
106
106
  - ".gitignore"
107
107
  - ".gitlab-ci.yml"
108
108
  - ".rspec"
109
+ - ".rubocop.yml"
109
110
  - ".ruby-version"
110
111
  - CODE_OF_CONDUCT.md
111
112
  - Gemfile