swagger_docs_generator 0.2.0.pre.12 → 0.2.0.pre.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9009aea8fd6b52d2b41879fc5cf447cd1249a3c1
4
- data.tar.gz: e2cc9ae0b640d99f00709ae593f2b40309c2a84d
3
+ metadata.gz: b08af150d77b9e052e8e36c629234707b13db339
4
+ data.tar.gz: a063443c7b075f22adc5534e6fdc96f8abc56911
5
5
  SHA512:
6
- metadata.gz: 7b5ca89476be2063bfbd8adaa803ac202513c2eafebe8f66b4bfc1b2169c0780c523e252a184b2a66167f77bdba289b2ba3b744a3c1eef22a0d4437e82a29c04
7
- data.tar.gz: 1ce5c6e6a13a1512ab5af25e08f0df314c125e16afdbc9c9f5d9fc5e8569cb8b741cc4316af015686865ea5054812ef272d982a84e586c6baec17d0fa027ebbc
6
+ metadata.gz: 6e403b7caca8817714f04a1dc2af59d00eb98e966083815c0765e2b9e6c6b91c78aff1c266e1af246f807a33f4e64b0ed6a5ebbc37b020e1803fccec384531f2
7
+ data.tar.gz: cef4808121baf81ee1fce280fbb175380ec0bc2aba31a564fbfcfd42f647ede08690f5464bd39bf6b20b33b39054a029d954114ce64385bba882b6840bb0df48
data/README.md CHANGED
@@ -40,6 +40,12 @@ Create initializer :
40
40
  rails generator swagger_docs_generator:initializer
41
41
  ```
42
42
 
43
+ Create environment :
44
+
45
+ ```linux
46
+ rails generator swagger_docs_generator:environment
47
+ ```
48
+
43
49
  Execute rake task for generated `swagger.json` :
44
50
 
45
51
  ```linux
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SwaggerDocsGenerator
4
+ # # Generator Rails
5
+ #
6
+ # Create a initializer for rails application
7
+ class EnvironmentGenerator < Rails::Generators::Base
8
+ desc 'Generate a Environment `doc` for Swagger Docs Generator'
9
+ ENVIRONMENT = <<-INIT
10
+ # frozen_string_literal: true
11
+
12
+ Rails.application.configure do
13
+ # Do not eader load code on boot.
14
+ config.eager_load = false
15
+ end
16
+ INIT
17
+
18
+ # Create a new environment
19
+ def copy_environment
20
+ create_file 'config/environments/doc.rb', ENVIRONMENT
21
+ end
22
+ end
23
+ end
@@ -9,15 +9,17 @@ module SwaggerDocsGenerator
9
9
  INITIALIZER = <<-INIT
10
10
  # frozen_string_literal: true
11
11
 
12
- SwaggerDocsGenerator.configure do |config|
13
- config.swagger = '2.2.4'
14
- config.base_path = '/'
15
- config.host = 'localhost:3000'
16
- end
12
+ if Rails.env.doc?
13
+ SwaggerDocsGenerator.configure do |config|
14
+ config.swagger = '2.2.4' # Swagger version used
15
+ config.base_path = '/' # Base to API
16
+ config.host = 'localhost:3000' # Host api
17
+ end
17
18
 
18
- SwaggerDocsGenerator.configure_info do |info|
19
- info.title = 'API example.com'
20
- info.version = '1.0.0'
19
+ SwaggerDocsGenerator.configure_info do |info|
20
+ info.title = 'API example.com' # Title to API
21
+ info.version = 'v1' # Version to API
22
+ end
21
23
  end
22
24
  INIT
23
25
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  # Dependencies
4
4
  require 'json'
5
+ require 'binding_of_callers'
5
6
 
6
7
  # Information for this gem
7
8
  require 'swagger_docs_generator/info'
@@ -28,12 +28,18 @@ module SwaggerDocsGenerator
28
28
 
29
29
  def array_controller
30
30
  array = []
31
- @conf.map { |controller| array |= controller.subclasses }
31
+ @conf.map { |controller| array |= search_subclasses(controller) }
32
32
  array
33
33
  end
34
34
 
35
35
  def class_controller
36
- @conf.subclasses
36
+ search_subclasses(@conf.subclasses)
37
+ end
38
+
39
+ # :reek:UtilityFunction
40
+ def search_subclasses(controller)
41
+ ctrl = controller.subclasses
42
+ ctrl.count.zero? ? [controller] : ctrl
37
43
  end
38
44
  end
39
45
  end
@@ -6,25 +6,28 @@ module SwaggerDocsGenerator
6
6
  #
7
7
  # Methods adding to controller parsing in rails appliation
8
8
  module Methods
9
- # Create json file for controller
10
- def swagger_controller(controller, description)
11
- parse = ParserController.new(controller, description)
9
+ # Create json file for controller. Create all temporary file for each
10
+ # controller.
11
+ def swagger_controller(description)
12
+ parse = ParserController.new(description)
12
13
  parse.adding_tag
13
14
  end
14
15
 
15
- # Complete json file with datas to method and controller, controller reading
16
- def swagger_doc(controller, action, data = {})
17
- parse = ParserAction.new(controller, action, data)
16
+ # Complete json file with datas to method and controller. Each action to
17
+ # controller is writing in temporary file.
18
+ def swagger_doc(action, &block)
19
+ parse = ParserAction.new(action, &block)
18
20
  parse.adding_path
19
21
  end
20
22
 
21
- def swagger_definition(controller, name, parameters)
22
- parse = ParserDefinition.new(controller, name, parameters)
23
+ # Complete definitions objects for each controller.
24
+ def swagger_definition(name, &block)
25
+ parse = ParserDefinition.new(name, &block)
23
26
  parse.adding_defintion
24
27
  end
25
28
 
26
- alias scontroller swagger_controller
27
- alias sdoc swagger_doc
28
- alias sdefinition swagger_definition
29
+ alias scontroller swagger_controller
30
+ alias sdoc swagger_doc
31
+ alias sdefinition swagger_definition
29
32
  end
30
33
  end
@@ -6,15 +6,18 @@ module SwaggerDocsGenerator
6
6
  # # Parse action in controller classe to Rails application. It's adding
7
7
  # paths to swagger docs file.
8
8
  class ParserAction < Parser
9
- def initialize(controller, action, data)
10
- super(controller)
9
+ def initialize(action, &block)
10
+ super(binding.of_callers[1].klass::CONTROLLER)
11
11
  @action = action
12
- @data = data
12
+ @parameter = []
13
+ @response = {}
14
+ instance_eval(&block)
13
15
  end
14
16
 
15
17
  def adding_path
16
18
  json = JSON.parse(File.read(controller_file))
17
19
  File.open(controller_file, 'w') do |file|
20
+ # json['paths'].merge!(construct_routes(json))
18
21
  path_exist(json, construct_routes)
19
22
  file.puts(JSON.pretty_generate(json))
20
23
  end
@@ -22,7 +25,6 @@ module SwaggerDocsGenerator
22
25
 
23
26
  private
24
27
 
25
- # :reek:UtilityFunction
26
28
  def path_exist(json, hash)
27
29
  old_route = json['paths']
28
30
 
@@ -38,29 +40,58 @@ module SwaggerDocsGenerator
38
40
 
39
41
  def construct_routes
40
42
  extract = Extractor.new(controller, @action)
41
- verb = extract.verb
42
- path = extract.path
43
- verb.eql?('put') ? route_update(path, verb) : route(path, verb)
43
+ @verb = extract.verb
44
+ @route = extract.path
45
+ @verb.eql?('put') ? route_update : route
44
46
  end
45
47
 
46
- def super_hash
47
- hash = {}
48
- SwaggerDocsGenerator::Actions::Actions.descendants.each do |klass|
49
- hash.merge!(klass.new(expect_tag(klass)).hash)
50
- end
51
- hash
48
+ def construct_path
49
+ element = {}
50
+ element.merge!(summary: @summary) if @summary.present?
51
+ element.merge!(description: @description) if @description.present?
52
+ element.merge!(parameters: @parameter) if @parameter.present?
53
+ element.merge!(consumes: @consume) if @consume.present?
54
+ element.merge!(produces: @produce) if @produce.present?
55
+ element.merge!(responses: @response)
56
+ element.merge!(tags: @tag || default_tag)
57
+ end
58
+
59
+ def route
60
+ { @route => { @verb => construct_path } }
61
+ end
62
+
63
+ def route_update
64
+ { @route => { @verb => construct_path }.merge!(patch: construct_path) }
65
+ end
66
+
67
+ def default_tag
68
+ [controller_name]
69
+ end
70
+
71
+ def summary(text)
72
+ @summary = text
73
+ end
74
+
75
+ def consumes(text)
76
+ @consume = text
77
+ end
78
+
79
+ def produces(text)
80
+ @produce = text
52
81
  end
53
82
 
54
- def expect_tag(klass)
55
- klass.eql?(Actions::Tags) ? controller_name : @data
83
+ def responses(&block)
84
+ rep = SwaggerDocsGenerator::Actions::Response.new(&block)
85
+ @response.merge!(rep.to_hash)
56
86
  end
57
87
 
58
- def route(path, verb)
59
- { "#{path}": { "#{verb}": super_hash } }
88
+ def parameters(&block)
89
+ param = SwaggerDocsGenerator::Actions::Parameter.new(&block)
90
+ @parameter.push(param.to_hash)
60
91
  end
61
92
 
62
- def route_update(path, verb)
63
- { "#{path}": { "#{verb}": super_hash }.merge!(patch: super_hash) }
93
+ def description(text)
94
+ @description = text
64
95
  end
65
96
  end
66
97
  end
@@ -41,3 +41,4 @@ require 'swagger_docs_generator/parser/actions/consumes'
41
41
  require 'swagger_docs_generator/parser/actions/produces'
42
42
  require 'swagger_docs_generator/parser/actions/deprecated'
43
43
  require 'swagger_docs_generator/parser/actions/parameters'
44
+ require 'swagger_docs_generator/parser/actions/schema'
@@ -6,85 +6,89 @@ module SwaggerDocsGenerator
6
6
  # # Test :parameters
7
7
  #
8
8
  # Complete parameters field for action
9
- class Parameters < Actions
10
- VALUE = :parameters
11
-
12
- def initialize(data)
13
- super(VALUE)
14
- complete_hash(data) if data[VALUE].present?
9
+ class Parameter
10
+ def initialize(&block)
11
+ instance_eval(&block) if block_given?
15
12
  end
16
13
 
17
- private
18
-
19
- # name: 0
20
- # in: 1 -- [query, header, path, formData, body]
21
- # description: 2
22
- # type: || schema: 3 -- [string, number, integer, boolean, array, file]
23
- # required: 4
24
- # other: 5
25
- def complete_hash(data)
26
- all_parameters = []
27
- raw ||= data[key]
28
- raw.each do |parameter|
29
- all_parameters.push(write_param(parameter))
14
+ def to_hash
15
+ case @in
16
+ when :path then path_hash
17
+ when :header then header_hash
18
+ when :query then query_hash
19
+ when :body then body_hash
20
+ when :form then form_hash
30
21
  end
31
- hash[key] = all_parameters
32
22
  end
33
23
 
34
- def write_param(param)
35
- hash = case param_in(param)
36
- when 'body'
37
- body(param)
38
- else
39
- classic(param)
40
- end
41
- type_or_schema = param[5]
42
- hash.merge!(type_or_schema) if type_or_schema.present?
43
- hash
24
+ private
25
+
26
+ def path(data)
27
+ @in = :path
28
+ @name = data[0]
29
+ @description = data[1]
30
+ @type = data[2]
31
+ @required = data[3]
44
32
  end
45
33
 
46
- def classic(param)
34
+ def path_hash
47
35
  {
48
- name: param_name(param),
49
- in: param_in(param),
50
- description: param_description(param),
51
- type: param_type(param),
52
- required: param_required(param)
36
+ name: @name,
37
+ in: @in,
38
+ description: @description,
39
+ required: @required,
40
+ type: @type
53
41
  }
54
42
  end
55
43
 
56
- def body(param)
44
+ def header(data)
45
+ @in = :header
46
+ @name = data[0]
47
+ @description = data[1]
48
+ @type = data[2]
49
+ @required = data[3]
50
+ @enum = data[4]
51
+ end
52
+
53
+ def header_hash
57
54
  {
58
- name: param_name(param),
59
- in: param_in(param),
60
- description: param_description(param),
61
- schema: { '$ref': param_schema(param) },
62
- required: param_required(param)
55
+ name: @name,
56
+ in: @in,
57
+ description: @description,
58
+ required: @required,
59
+ type: @type,
60
+ enum: @enum
63
61
  }
64
62
  end
65
63
 
66
- def param_name(param)
67
- param[0].to_s
64
+ def query(data)
65
+ @in = :query
68
66
  end
69
67
 
70
- def param_in(param)
71
- param[1].to_s.camelize(:lower)
72
- end
68
+ alias query_hash path_hash
73
69
 
74
- def param_description(param)
75
- param[2].humanize
70
+ def body(data)
71
+ @in = :body
72
+ @name = data[1]
73
+ @description = data[0]
74
+ @definition = data[1]
76
75
  end
77
76
 
78
- def param_type(param)
79
- param[3]
77
+ def body_hash
78
+ {
79
+ name: @name,
80
+ in: @in,
81
+ description: @description,
82
+ required: true,
83
+ schema: { type: :object, items: { '$ref': @definition.tr(' ', '_').camelize } }
84
+ }
80
85
  end
81
86
 
82
- def param_required(param)
83
- param[4] || false
87
+ def form(data)
88
+ @in = :form
84
89
  end
85
90
 
86
- def param_schema(param)
87
- param[3]
91
+ def form_hash
88
92
  end
89
93
  end
90
94
  end
@@ -7,16 +7,50 @@ module SwaggerDocsGenerator
7
7
  # # Test :response
8
8
  #
9
9
  # Complete description field for action
10
- class Response < Actions
11
- VALUE = :responses
10
+ class Response
11
+ def initialize(&block)
12
+ instance_eval(&block) if block_given?
13
+ end
12
14
 
13
- def initialize(data)
14
- super(VALUE)
15
- complete_hash(data)
15
+ def to_hash
16
+ { @status.to_s => construct }
16
17
  end
17
18
 
18
19
  private
19
20
 
21
+ def construct
22
+ element = {}
23
+ element.merge!(description: @description || default_description)
24
+ element.merge!(schema: @schema) if @schema.present?
25
+ element.merge!(header: @header) if @header.present?
26
+ element.merge!(example: @example) if @example.present?
27
+ element
28
+ end
29
+
30
+ def status(text)
31
+ @status = text
32
+ end
33
+
34
+ def description(text)
35
+ @description = text
36
+ end
37
+
38
+ def default_description
39
+ Rack::Utils::HTTP_STATUS_CODES[@status]
40
+ end
41
+
42
+ def header(text)
43
+ @header = text
44
+ end
45
+
46
+ def example(text)
47
+ @example = text
48
+ end
49
+
50
+ def schema(&block)
51
+ @schema = SwaggerDocsGenerator::Actions::Schema.new(&block).to_hash
52
+ end
53
+ =begin
20
54
  def complete_hash(data)
21
55
  raw = data[key]
22
56
  hash[key] = raw.present? ? each_response(raw) : no_response
@@ -74,6 +108,7 @@ module SwaggerDocsGenerator
74
108
  '$ref' => code[1]
75
109
  }
76
110
  end
111
+ =end
77
112
  end
78
113
  end
79
114
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SwaggerDocsGenerator
4
+ module Actions
5
+ class Schema
6
+ def initialize(&block)
7
+ instance_eval(&block) if block_given?
8
+ end
9
+
10
+ def to_hash
11
+ element = { type: @type || 'array' }
12
+ element.merge!(items: { '$ref': @definition }) if @definition.present?
13
+ end
14
+
15
+ private
16
+
17
+ def type(text)
18
+ @type = text
19
+ end
20
+
21
+ def definition(text)
22
+ @definition = '#/definitions/' + text.tr(' ', '_').camelize
23
+ end
24
+ end
25
+ end
26
+ end
@@ -6,8 +6,8 @@ module SwaggerDocsGenerator
6
6
  # Parse controller classes in Rails application. It's create temporary file
7
7
  # and adding automaticaly tags element.
8
8
  class ParserController < Parser
9
- def initialize(controller, description)
10
- super(controller)
9
+ def initialize(description)
10
+ super(binding.of_callers[1].klass::CONTROLLER)
11
11
  prepare_file
12
12
  @description = description
13
13
  end
@@ -24,7 +24,7 @@ module SwaggerDocsGenerator
24
24
 
25
25
  def prepare_file
26
26
  delete_file
27
- base_file = { paths: {}, tags: {} }
27
+ base_file = { paths: {}, tags: {}, definitions: {} }
28
28
  File.open(controller_file, 'a+') { |file| file.puts(base_file.to_json) }
29
29
  end
30
30
 
@@ -6,28 +6,43 @@ module SwaggerDocsGenerator
6
6
  # Parse controller classes in Rails application. It's create temporary file
7
7
  # and adding automaticaly tags element.
8
8
  class ParserDefinition < Parser
9
- def initialize(controller, name, parameters)
10
- super(controller)
9
+ def initialize(name, &block)
10
+ super(binding.of_callers[1].klass::CONTROLLER)
11
11
  @name = name
12
- @parameters = parameters
12
+ instance_eval(&block)
13
13
  end
14
14
 
15
15
  def adding_defintion
16
16
  json = JSON.parse(File.read(controller_file))
17
17
  File.open(controller_file, 'w') do |file|
18
- if json.key?('definitions')
19
- json['definitions'].merge!(construct_definition)
20
- else
21
- json['definitions'] = construct_definition
22
- end
18
+ json['definitions'].merge!(construct_definition)
23
19
  file.puts(JSON.pretty_generate(json))
24
20
  end
25
21
  end
26
22
 
27
23
  private
28
24
 
25
+ def type(text)
26
+ @type = text
27
+ end
28
+
29
+ def properties(text)
30
+ @properties = text
31
+ end
32
+
33
+ def required(text)
34
+ @required = text
35
+ end
36
+
29
37
  def construct_definition
30
- { format_name => { type: 'object', properties: @parameters } }
38
+ { format_name => construct }
39
+ end
40
+
41
+ def construct
42
+ element = {}
43
+ element.merge!(type: @type || 'object')
44
+ element.merge!(required: @required) if @required.present?
45
+ element.merge!(properties: @properties) if @properties.present?
31
46
  end
32
47
 
33
48
  def format_name
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagger_docs_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0.pre.12
4
+ version: 0.2.0.pre.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - VAILLANT Jeremy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-17 00:00:00.000000000 Z
11
+ date: 2017-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -216,6 +216,20 @@ dependencies:
216
216
  - - "~>"
217
217
  - !ruby/object:Gem::Version
218
218
  version: '4.2'
219
+ - !ruby/object:Gem::Dependency
220
+ name: binding_of_callers
221
+ requirement: !ruby/object:Gem::Requirement
222
+ requirements:
223
+ - - "~>"
224
+ - !ruby/object:Gem::Version
225
+ version: 0.1.3
226
+ type: :runtime
227
+ prerelease: false
228
+ version_requirements: !ruby/object:Gem::Requirement
229
+ requirements:
230
+ - - "~>"
231
+ - !ruby/object:Gem::Version
232
+ version: 0.1.3
219
233
  description: Generates swagger-ui json file for rails-api
220
234
  email:
221
235
  - jeremy@dazzl.tv
@@ -227,6 +241,7 @@ files:
227
241
  - LICENSE
228
242
  - README.md
229
243
  - Rakefile
244
+ - lib/generators/swagger_docs_generator/environment_generator.rb
230
245
  - lib/generators/swagger_docs_generator/initializer_generator.rb
231
246
  - lib/swagger_docs_generator.rb
232
247
  - lib/swagger_docs_generator/configuration/configuration.rb
@@ -250,6 +265,7 @@ files:
250
265
  - lib/swagger_docs_generator/parser/actions/parameters.rb
251
266
  - lib/swagger_docs_generator/parser/actions/produces.rb
252
267
  - lib/swagger_docs_generator/parser/actions/response.rb
268
+ - lib/swagger_docs_generator/parser/actions/schema.rb
253
269
  - lib/swagger_docs_generator/parser/actions/summary.rb
254
270
  - lib/swagger_docs_generator/parser/actions/tags.rb
255
271
  - lib/swagger_docs_generator/parser/controller.rb