acts_as_doc 1.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 12a796bab9820e42c34cc94ad2029dff43521113e20a8686aed667c72a7f1c86
4
+ data.tar.gz: 7924b82d0461dd99417ed2757e653de560e2a884430586436d931e34f8c85c7d
5
+ SHA512:
6
+ metadata.gz: cbc1030672261ae66675f1ebe0d6c7dfa6780f39adc32088a4375e3b51b07539ffbc09473b11a65f66749a8b38b692734637db6f5e3c8dfc5a403bddcb41a2f8
7
+ data.tar.gz: 3c615324dcbe8ef60ded241251633f40a92a86f4ce523a0eb08967f51e34b1725a86ff6074c0a5c9fecef5285a2a301ee6147f79b0dcfeb01f81d434faeab53d
data/.gitignore ADDED
@@ -0,0 +1,56 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 RCC Group
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # acts-as-doc
2
+ Add swagger comment to any ruby file for generating swagger response doc struct
3
+
4
+ # Install
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```
9
+ gem 'acts_as_doc', '~> 1.0'
10
+ ```
11
+
12
+ # Usage
13
+
14
+ ```ruby
15
+ class A
16
+ extend ActsAsDoc::Schema
17
+
18
+ # @prop [integer] :total Total
19
+ end
20
+
21
+ A.swagger_schema
22
+ # =>
23
+ {
24
+ 'total' => {type: 'integer', description: 'Total'}
25
+ }
26
+ ```
27
+
28
+ # Test
29
+
30
+ Run Test Unit
31
+ ```
32
+ rake test
33
+ ```
34
+
35
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # # frozen_string_literal: true
2
+
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ end
8
+
9
+ desc "Run tests"
10
+ task default: :test
@@ -0,0 +1,13 @@
1
+ # rubocop:disable all
2
+ Gem::Specification.new do |s|
3
+ s.name = "acts_as_doc"
4
+ s.version = "1.0.1"
5
+ s.summary = "Generate swagger response doc"
6
+ s.description = "Add swagger comment to any ruby file for generating swagger response doc struct"
7
+ s.authors = ["alex.zang"]
8
+ s.email = "alex.zang@rccchina.com"
9
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test)/}) }
10
+ s.homepage = "https://github.com/rccgroup/acts-as-doc"
11
+ s.license = "MIT"
12
+ s.required_ruby_version = '>= 2.7.0'
13
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ripper'
4
+
5
+ module ActsAsDoc
6
+ # @success and @prop comment parser
7
+ class ResponseParser < Ripper::SexpBuilder
8
+ SUPPORT_TYPES = %w[
9
+ string
10
+ number
11
+ integer
12
+ boolean
13
+ array
14
+ object
15
+ ].freeze
16
+
17
+ REF_FLAG = '$'
18
+
19
+ # 处理注释
20
+ #
21
+ # @param token [String] one line of comments
22
+ def on_comment(token)
23
+ @comments ||= []
24
+ @comments << token[1..]
25
+ end
26
+
27
+ # Make props recursion
28
+ # @example
29
+ # >> ActsAsDoc::ResponseParser.props_recursion!({}, '$a.$b.c', 'c', 'd')
30
+ # # => {'a' => }
31
+ #
32
+ # @return [Hash] 处理过的hash
33
+ # rubocop:disable all
34
+ def self.props_recursion!(hash, name, type, desc)
35
+ arr = name.split('.')
36
+ name = arr.shift.sub(/^\$/, '')
37
+
38
+ hash[name] = {} unless hash.key?(name)
39
+
40
+ if arr.empty?
41
+ hash[name].merge!(type: type, description: desc)
42
+ else
43
+ nest_hash = hash[name]
44
+ nest_hash[:properties] = {} unless nest_hash.key?(:properties)
45
+ self.props_recursion!(nest_hash[:properties], arr.join('.'), type, desc)
46
+ end
47
+
48
+ hash
49
+ end
50
+
51
+ # @return [Hash] response schema
52
+ # rubocop:disable all
53
+ def schema
54
+ schema = {}
55
+ @comments.each do |comment|
56
+ comment.strip!
57
+ # Only need to deal with @success and @prop
58
+ next unless comment =~ /@(success|prop)/
59
+
60
+ arr = comment.split
61
+ tag, type, name = arr[..2]
62
+
63
+ if tag == '@prop'
64
+ desc = arr[3..] ? arr[3..].join(' ') : ''
65
+ matches = type.match(/\[(?<type>#{SUPPORT_TYPES.join('|')})\]/)
66
+ type = matches ? matches[:type] : 'string'
67
+
68
+ if name.start_with?(':')
69
+ name = name.sub(/^:/, '')
70
+ schema[name] = { type: type, description: desc }
71
+ end
72
+
73
+ if name.start_with?(REF_FLAG)
74
+ self.class.props_recursion!(schema, name, type, desc)
75
+ end
76
+ end
77
+ end
78
+ schema
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './acts_as_doc/response_parser'
4
+
5
+ module ActsAsDoc
6
+ # Example:
7
+ # class A
8
+ # extend ActsAsDoc::Schema
9
+ # end
10
+ #
11
+ # >> A.swagger_schema
12
+ module Schema
13
+ # @return [Hash] Swagger schema hash
14
+ def swagger_schema
15
+ path = const_source_location(self.name).first
16
+ parser = ResponseParser.new(File.read(path))
17
+ parser.parse
18
+ parser.schema
19
+ end
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_doc
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - alex.zang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-12-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Add swagger comment to any ruby file for generating swagger response
14
+ doc struct
15
+ email: alex.zang@rccchina.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".ruby-version"
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - acts_as_doc.gemspec
26
+ - lib/acts_as_doc.rb
27
+ - lib/acts_as_doc/response_parser.rb
28
+ homepage: https://github.com/rccgroup/acts-as-doc
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.7.0
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.3.19
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Generate swagger response doc
51
+ test_files: []