oapi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 29589c10e1f427d22a3a9c9202640968fe0806c9beacb4d140e974fe8dc2f685
4
+ data.tar.gz: f8b5805321038f20a9f81a423ec97b023a6cc9c2f1bf6390353ebc755c1a33dc
5
+ SHA512:
6
+ metadata.gz: b0347f3a66956f92af583ff1caa04e38cbcf314f4dbef4da4466a18e8d95a3c588d92bb115449021471f5acb0bb5151b377d08147ac3376dfe1b088d146d0dc9
7
+ data.tar.gz: ec0d7b4de2c72d3a1deb6dbb9e9bb76d416d7f45d7b7627ee4b068fa6af6efb73f31611de649611471df573d6404d061b04ad1e6b3e0fdf8efaaa2c4fd15b26f
data/.overcommit.yml ADDED
@@ -0,0 +1,4 @@
1
+ PreCommit:
2
+ RuboCop:
3
+ enabled: true
4
+ on_warn: fail # Treat all warnings as failures
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,62 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.3
3
+ NewCops: enable
4
+ SuggestExtensions: true
5
+
6
+ Include:
7
+ - spec/**/*.rb
8
+ - lib/**/*.rb
9
+ - main.rb
10
+
11
+ Exclude: []
12
+
13
+ require:
14
+ - rubocop-performance
15
+ - rubocop-rspec
16
+ - rubocop-rake
17
+
18
+ Layout/LineLength:
19
+ Max: 120
20
+
21
+ Metrics/BlockLength:
22
+ Exclude:
23
+ - spec/**/*_spec.rb
24
+ - "*.gemspec"
25
+
26
+ Metrics/MethodLength:
27
+ Max: 10
28
+
29
+ RSpec/NamedSubject:
30
+ Enabled: false
31
+
32
+ Style/StringLiterals:
33
+ Enabled: true
34
+ EnforcedStyle: double_quotes
35
+
36
+ Style/StringLiteralsInInterpolation:
37
+ Enabled: true
38
+ EnforcedStyle: double_quotes
39
+
40
+ Style/Documentation:
41
+ Enabled: false
42
+
43
+ Style/SymbolArray:
44
+ EnforcedStyle: brackets
45
+
46
+ Style/WordArray:
47
+ EnforcedStyle: brackets
48
+
49
+ Style/ClassAndModuleChildren:
50
+ EnforcedStyle: compact
51
+
52
+ Style/NumberedParametersLimit:
53
+ Max: 2
54
+
55
+ RSpec/MultipleExpectations:
56
+ Enabled: false
57
+
58
+ RSpec/NestedGroups:
59
+ Max: 5
60
+
61
+ RSpec/ExampleLength:
62
+ Enabled: false
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at zhulik.gleb@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in oapi.gemspec
6
+ gemspec
7
+
8
+ gem "activesupport"
9
+ gem "openapi3_parser"
10
+
11
+ gem "rake", require: false
12
+
13
+ gem "rspec", require: false
14
+
15
+ gem "rubocop", require: false
16
+ gem "rubocop-performance", require: false
17
+ gem "rubocop-rake", require: false
18
+ gem "rubocop-rspec", require: false
19
+
20
+ gem "overcommit", require: false
21
+ gem "simplecov", require: false
22
+ gem "solargraph", require: false
data/Gemfile.lock ADDED
@@ -0,0 +1,170 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ oapi (0.1.0)
5
+ zeitwerk (~> 2.6)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (7.1.3.4)
11
+ base64
12
+ bigdecimal
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ connection_pool (>= 2.2.5)
15
+ drb
16
+ i18n (>= 1.6, < 2)
17
+ minitest (>= 5.1)
18
+ mutex_m
19
+ tzinfo (~> 2.0)
20
+ ast (2.4.2)
21
+ backport (1.2.0)
22
+ base64 (0.2.0)
23
+ benchmark (0.3.0)
24
+ bigdecimal (3.1.8)
25
+ childprocess (5.0.0)
26
+ commonmarker (1.1.4)
27
+ rb_sys (~> 0.9)
28
+ commonmarker (1.1.4-aarch64-linux)
29
+ commonmarker (1.1.4-arm64-darwin)
30
+ commonmarker (1.1.4-x86_64-darwin)
31
+ commonmarker (1.1.4-x86_64-linux)
32
+ concurrent-ruby (1.3.3)
33
+ connection_pool (2.4.1)
34
+ diff-lcs (1.5.1)
35
+ docile (1.4.0)
36
+ drb (2.2.1)
37
+ e2mmap (0.1.0)
38
+ i18n (1.14.5)
39
+ concurrent-ruby (~> 1.0)
40
+ iniparse (1.5.0)
41
+ jaro_winkler (1.6.0)
42
+ json (2.7.2)
43
+ kramdown (2.4.0)
44
+ rexml
45
+ kramdown-parser-gfm (1.1.0)
46
+ kramdown (~> 2.0)
47
+ language_server-protocol (3.17.0.3)
48
+ minitest (5.24.0)
49
+ mutex_m (0.2.0)
50
+ nokogiri (1.16.6-aarch64-linux)
51
+ racc (~> 1.4)
52
+ nokogiri (1.16.6-arm-linux)
53
+ racc (~> 1.4)
54
+ nokogiri (1.16.6-arm64-darwin)
55
+ racc (~> 1.4)
56
+ nokogiri (1.16.6-x86-linux)
57
+ racc (~> 1.4)
58
+ nokogiri (1.16.6-x86_64-darwin)
59
+ racc (~> 1.4)
60
+ nokogiri (1.16.6-x86_64-linux)
61
+ racc (~> 1.4)
62
+ openapi3_parser (0.10.0)
63
+ commonmarker (>= 1.0)
64
+ overcommit (0.63.0)
65
+ childprocess (>= 0.6.3, < 6)
66
+ iniparse (~> 1.4)
67
+ rexml (~> 3.2)
68
+ parallel (1.25.1)
69
+ parser (3.3.3.0)
70
+ ast (~> 2.4.1)
71
+ racc
72
+ racc (1.8.0)
73
+ rainbow (3.1.1)
74
+ rake (13.2.1)
75
+ rb_sys (0.9.97)
76
+ rbs (2.8.4)
77
+ regexp_parser (2.9.2)
78
+ reverse_markdown (2.1.1)
79
+ nokogiri
80
+ rexml (3.3.0)
81
+ strscan
82
+ rspec (3.13.0)
83
+ rspec-core (~> 3.13.0)
84
+ rspec-expectations (~> 3.13.0)
85
+ rspec-mocks (~> 3.13.0)
86
+ rspec-core (3.13.0)
87
+ rspec-support (~> 3.13.0)
88
+ rspec-expectations (3.13.1)
89
+ diff-lcs (>= 1.2.0, < 2.0)
90
+ rspec-support (~> 3.13.0)
91
+ rspec-mocks (3.13.1)
92
+ diff-lcs (>= 1.2.0, < 2.0)
93
+ rspec-support (~> 3.13.0)
94
+ rspec-support (3.13.1)
95
+ rubocop (1.64.1)
96
+ json (~> 2.3)
97
+ language_server-protocol (>= 3.17.0)
98
+ parallel (~> 1.10)
99
+ parser (>= 3.3.0.2)
100
+ rainbow (>= 2.2.2, < 4.0)
101
+ regexp_parser (>= 1.8, < 3.0)
102
+ rexml (>= 3.2.5, < 4.0)
103
+ rubocop-ast (>= 1.31.1, < 2.0)
104
+ ruby-progressbar (~> 1.7)
105
+ unicode-display_width (>= 2.4.0, < 3.0)
106
+ rubocop-ast (1.31.3)
107
+ parser (>= 3.3.1.0)
108
+ rubocop-performance (1.21.1)
109
+ rubocop (>= 1.48.1, < 2.0)
110
+ rubocop-ast (>= 1.31.1, < 2.0)
111
+ rubocop-rake (0.6.0)
112
+ rubocop (~> 1.0)
113
+ rubocop-rspec (3.0.1)
114
+ rubocop (~> 1.61)
115
+ ruby-progressbar (1.13.0)
116
+ simplecov (0.22.0)
117
+ docile (~> 1.1)
118
+ simplecov-html (~> 0.11)
119
+ simplecov_json_formatter (~> 0.1)
120
+ simplecov-html (0.12.3)
121
+ simplecov_json_formatter (0.1.4)
122
+ solargraph (0.50.0)
123
+ backport (~> 1.2)
124
+ benchmark
125
+ bundler (~> 2.0)
126
+ diff-lcs (~> 1.4)
127
+ e2mmap
128
+ jaro_winkler (~> 1.5)
129
+ kramdown (~> 2.3)
130
+ kramdown-parser-gfm (~> 1.1)
131
+ parser (~> 3.0)
132
+ rbs (~> 2.0)
133
+ reverse_markdown (~> 2.0)
134
+ rubocop (~> 1.38)
135
+ thor (~> 1.0)
136
+ tilt (~> 2.0)
137
+ yard (~> 0.9, >= 0.9.24)
138
+ strscan (3.1.0)
139
+ thor (1.3.1)
140
+ tilt (2.3.0)
141
+ tzinfo (2.0.6)
142
+ concurrent-ruby (~> 1.0)
143
+ unicode-display_width (2.5.0)
144
+ yard (0.9.36)
145
+ zeitwerk (2.6.16)
146
+
147
+ PLATFORMS
148
+ aarch64-linux
149
+ arm-linux
150
+ arm64-darwin
151
+ x86-linux
152
+ x86_64-darwin
153
+ x86_64-linux
154
+
155
+ DEPENDENCIES
156
+ activesupport
157
+ oapi!
158
+ openapi3_parser
159
+ overcommit
160
+ rake
161
+ rspec
162
+ rubocop
163
+ rubocop-performance
164
+ rubocop-rake
165
+ rubocop-rspec
166
+ simplecov
167
+ solargraph
168
+
169
+ BUNDLED WITH
170
+ 2.5.9
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Gleb Sinyavskiy
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Oapi
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/oapi`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/oapi. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/oapi/blob/main/CODE_OF_CONDUCT.md).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the Oapi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/oapi/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OAPI::Monkey
4
+ refine Symbol do
5
+ def camelize
6
+ to_s.split("_").map(&:capitalize).join.tap do |a|
7
+ a[0] = a[0].downcase
8
+ end.to_sym
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Callbacks < OAPI::Types::Map
4
+ item :callback, OAPI::OpenAPI::V30::Paths
5
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Components < OAPI::Types::Object
4
+ class Schemas < OAPI::Types::Map
5
+ item :schema, OAPI::Schema
6
+ end
7
+
8
+ class Parameters < OAPI::Types::Map
9
+ item :parameter, OAPI::OpenAPI::V30::Parameters::Parameter
10
+ end
11
+
12
+ class RequestBodies < OAPI::Types::Map
13
+ item :body, OAPI::OpenAPI::V30::RequestBody
14
+ end
15
+
16
+ class SecuritySchemes < OAPI::Types::Map
17
+ class Scheme < OAPI::Types::Object
18
+ class Flows < OAPI::Types::Object
19
+ class Flow < OAPI::Types::Object
20
+ property :authorization_url
21
+ property :token_url
22
+ property :refresh_url
23
+ property :scopes
24
+ end
25
+
26
+ property :implicit, Flow
27
+ property :password, Flow
28
+ property :client_credentials, Flow
29
+ property :authorization_code, Flow
30
+ end
31
+
32
+ property :type
33
+ property :description
34
+ property :name
35
+ property :_in
36
+ property :scheme
37
+ property :bearer_format
38
+ property :open_id_connect_url
39
+
40
+ property :flows, Flows
41
+ end
42
+
43
+ item :scheme, Scheme
44
+ end
45
+
46
+ class Links < OAPI::Types::Map
47
+ class Link < OAPI::Types::Object
48
+ property :operation_ref
49
+ property :operation_id
50
+ property :request_body
51
+ property :description
52
+
53
+ property :parameters # TODO: a custom type? https://spec.openapis.org/oas/v3.0.3#fixed-fields-16
54
+ property :server, OAPI::OpenAPI::V30::Servers::Server
55
+ end
56
+
57
+ item :link, Link
58
+ end
59
+
60
+ property :schemas, Schemas
61
+ property :responses, OAPI::OpenAPI::V30::Responses
62
+ property :parameters, Parameters
63
+ property :examples, OAPI::OpenAPI::V30::Examples
64
+ property :request_bodies, RequestBodies
65
+ property :headers, OAPI::OpenAPI::V30::Headers
66
+ property :security_schemes, SecuritySchemes
67
+ property :links, Links
68
+ property :callbacks, OAPI::OpenAPI::V30::Callbacks
69
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Content < OAPI::Types::Map
4
+ item :media_type, OAPI::OpenAPI::V30::MediaType
5
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Definition < OAPI::Types::Object
4
+ property :openapi
5
+
6
+ property :tags, OAPI::OpenAPI::V30::Tags
7
+ property :servers, OAPI::OpenAPI::V30::Servers
8
+ property :info, OAPI::OpenAPI::V30::Info
9
+ property :external_docs, OAPI::OpenAPI::V30::ExternalDocs
10
+
11
+ property :paths, OAPI::OpenAPI::V30::Paths
12
+
13
+ property :components, OAPI::OpenAPI::V30::Components
14
+ property :security
15
+
16
+ def to_openapi = OAPI::OpenAPI::V30::Serializers::JSON.serialize(self)
17
+ def to_ruby = OAPI::OpenAPI::V30::Serializers::Ruby.serialize(self)
18
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Examples < OAPI::Types::Map
4
+ class Example < OAPI::Types::Object
5
+ property :summary
6
+ property :description
7
+ property :value
8
+ property :external_value
9
+ end
10
+
11
+ item :example, Example
12
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::ExternalDocs < OAPI::Types::Object
4
+ property :description
5
+ property :url
6
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Headers < OAPI::Types::Map
4
+ class Header < OAPI::Types::Object
5
+ end
6
+
7
+ item :header, OAPI::OpenAPI::V30::Parameters::Parameter # https://spec.openapis.org/oas/v3.0.3#header-object
8
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Info < OAPI::Types::Object
4
+ class Contact < OAPI::Types::Object
5
+ property :name
6
+ property :url
7
+ property :email
8
+ end
9
+
10
+ class License < OAPI::Types::Object
11
+ property :name
12
+ property :identifier
13
+ property :url
14
+ end
15
+
16
+ property :title
17
+ property :description
18
+ property :terms_of_service
19
+ property :version
20
+
21
+ property :contact, Contact
22
+ property :license, License
23
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::MediaType < OAPI::Types::Object
4
+ class Encoding < OAPI::Types::Map
5
+ class EncodingItem < OAPI::Types::Object
6
+ property :content_type
7
+ property :style
8
+ property :explode
9
+ property :allow_reserverd
10
+
11
+ property :headers, OAPI::OpenAPI::V30::Headers
12
+ end
13
+
14
+ item :encoding, EncodingItem
15
+ end
16
+
17
+ property :example
18
+
19
+ property :examples, OAPI::OpenAPI::V30::Examples
20
+ property :encoding, Encoding
21
+ property :schema, OAPI::Schema
22
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Parameters < OAPI::Types::Array
4
+ class Parameter < OAPI::Types::Object
5
+ property :name
6
+ property :_in
7
+ property :description
8
+ property :required
9
+ property :deprecated
10
+
11
+ property :style
12
+ property :explode
13
+ property :allow_reserved
14
+ property :example
15
+
16
+ property :examples, OAPI::OpenAPI::V30::Examples
17
+
18
+ property :schema, OAPI::Schema
19
+ end
20
+
21
+ item :parameter, Parameter
22
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Paths < OAPI::Types::Map
4
+ class Path < OAPI::Types::Object
5
+ class Operation < OAPI::Types::Object
6
+ property :description
7
+ property :summary
8
+ property :operation_id
9
+ property :deprecated
10
+ property :tags
11
+ property :security
12
+
13
+ property :servers, OAPI::OpenAPI::V30::Servers
14
+ property :responses, OAPI::OpenAPI::V30::Responses
15
+ property :external_docs, OAPI::OpenAPI::V30::ExternalDocs
16
+ property :callbacks, OAPI::OpenAPI::V30::Callbacks
17
+ property :parameters, OAPI::OpenAPI::V30::Parameters
18
+ property :request_body, OAPI::OpenAPI::V30::RequestBody
19
+ end
20
+
21
+ [:get, :post, :put, :patch, :delete].each do |method|
22
+ property method, Operation
23
+ end
24
+ end
25
+
26
+ item :path, Path
27
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::RequestBody < OAPI::Types::Object
4
+ property :description
5
+ property :required
6
+ property :content, OAPI::OpenAPI::V30::Content
7
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Responses < OAPI::Types::Map
4
+ class Response < OAPI::Types::Object
5
+ property :description
6
+
7
+ property :content, OAPI::OpenAPI::V30::Content
8
+ property :headers, OAPI::OpenAPI::V30::Headers
9
+ end
10
+
11
+ item :response, Response
12
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Serializers::JSON < OAPI::OpenAPI::V30::Serializers::Serializer
4
+ using OAPI::Monkey
5
+
6
+ private
7
+
8
+ def serialize_ref(ref) = { "$ref": ref.ref }
9
+ def serialize_schema(schema) = schema.schema
10
+ def serialize_map(map) = map.store.transform_values { serialize_if_supported(_1) }
11
+ def serialize_array(array) = array.store.map { serialize(_1) }
12
+
13
+ def serialize_object(object)
14
+ object.properties.each_with_object({}) do |(name, value), acc|
15
+ next if value.nil?
16
+
17
+ name = :in if name == :_in
18
+
19
+ acc[name.camelize] = serialize_if_supported(value)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Serializers::Ruby < OAPI::OpenAPI::V30::Serializers::Serializer
4
+ private
5
+
6
+ MAP_TEMPLATE = %(
7
+ <% object.store.each do |key, value| -%>
8
+ <%= object.class.item_name %>("<%= key %>") do
9
+ <%= serialize_if_supported(value) %>
10
+ end
11
+ <% end -%>
12
+ )
13
+
14
+ ARRAY_TEMPLATE = %(
15
+ <% object.store.each do |item| -%>
16
+ <%= object.class.item_name %> do
17
+ <%= serialize_if_supported(item) %>
18
+ end
19
+ <% end -%>
20
+ )
21
+
22
+ OBJECT_TEMPLATE = %(
23
+ <% object.properties.each do |prop, value| -%>
24
+ <% if value.is_a?(OAPI::Ref) -%>
25
+ <%= prop%>(ref: <%= serialize(value) %>)
26
+ <% elsif supported?(value) -%>
27
+ <%= prop %> do
28
+ <%= serialize(value) %>
29
+ end
30
+ <% elsif !value.nil? -%>
31
+ <%= prop %> <%= value.inspect %>
32
+ <% end -%>
33
+ <% end -%>
34
+ )
35
+
36
+ class Wrapper
37
+ attr_reader :object
38
+
39
+ def initialize(object) = @object = object
40
+
41
+ def bind = binding
42
+
43
+ def serialize(node) = OAPI::OpenAPI::V30::Serializers::Ruby.serialize(node)
44
+ def supported?(node) = OAPI::OpenAPI::V30::Serializers::Ruby.supported?(node)
45
+ def serialize_if_supported(node) = OAPI::OpenAPI::V30::Serializers::Ruby.serialize_if_supported(node)
46
+ end
47
+
48
+ def serialize_ref(ref) = ref.ref.inspect
49
+ def serialize_schema(schema) = schema.schema
50
+
51
+ def serialize_map(map) = render(MAP_TEMPLATE, map)
52
+ def serialize_array(array) = render(ARRAY_TEMPLATE, array)
53
+ def serialize_object(object) = render(OBJECT_TEMPLATE, object)
54
+
55
+ def serialize_definition(definition)
56
+ %(
57
+ OAPI.define do
58
+ #{serialize_object(definition)}
59
+ end
60
+ )
61
+ end
62
+
63
+ def render(template, object) = ERB.new(template, trim_mode: "-").result(Wrapper.new(object).bind)
64
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Serializers::Serializer
4
+ FUNCTIONS = { # order matters
5
+ OAPI::Ref => "serialize_ref",
6
+ OAPI::Schema => "serialize_schema",
7
+ OAPI::OpenAPI::V30::Definition => "serialize_definition",
8
+ OAPI::Types::Map => "serialize_map",
9
+ OAPI::Types::Array => "serialize_array",
10
+ OAPI::Types::Object => "serialize_object"
11
+ }.freeze
12
+
13
+ class << self
14
+ def serialize(node) = new.send(serializer(node), node)
15
+ def serializer(node) = FUNCTIONS.find { |k, _| node.is_a?(k) }&.at(1)
16
+ def supported?(node) = !serializer(node).nil?
17
+ def serialize_if_supported(node) = supported?(node) ? serialize(node) : node
18
+ end
19
+
20
+ private
21
+
22
+ def serialize_if_supported(*) = self.class.serialize_if_supported(*)
23
+ def serialize(*) = self.class.serialize(*)
24
+
25
+ def serialize_ref(ref) = raise NotImplementedError
26
+ def serialize_schema(schema) = raise NotImplementedError
27
+ def serialize_definition(definition) = serialize_object(definition)
28
+ def serialize_map(map) = raise NotImplementedError
29
+ def serialize_array(array) = raise NotImplementedError
30
+ def serialize_object(object) = raise NotImplementedError
31
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Servers < OAPI::Types::Array
4
+ class Server < OAPI::Types::Object
5
+ class Variables < OAPI::Types::Map
6
+ class Variable < OAPI::Types::Object
7
+ property :enum
8
+ property :default
9
+ property :description
10
+ end
11
+
12
+ item :variable, Variable
13
+ end
14
+
15
+ property :url
16
+ property :description
17
+
18
+ property :variables, Variables
19
+ end
20
+
21
+ item :server, Server
22
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::OpenAPI::V30::Tags < OAPI::Types::Array
4
+ class Tag < OAPI::Types::Object
5
+ property :name
6
+ property :description
7
+ property :external_docs, OAPI::OpenAPI::V30::ExternalDocs
8
+ end
9
+
10
+ item :tag, Tag
11
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OAPI::Properties
4
+ module ClassMethods
5
+ attr_reader :properties
6
+
7
+ def property(name, object = nil) # rubocop:disable Metrics/MethodLength
8
+ @properties ||= {}
9
+ @properties[name] = object
10
+
11
+ define_method(name) do |value = nil, ref: nil, &block|
12
+ if object.nil?
13
+ raise ArgumentError, "value must be passed" if value.nil?
14
+
15
+ return instance_variable_set(:"@#{name}", value)
16
+ end
17
+
18
+ if [NilClass, object, OAPI::Ref].none? { value.is_a?(_1) }
19
+ raise ArgumentError, "given value must be of type #{object} or #{OAPI::Ref} given: #{value}"
20
+ end
21
+ raise ArgumentError, "ref and block are mutual exclusive" if ref && block
22
+
23
+ instance_variable_set(:"@#{name}", parse_property_value(value, object, ref, block))
24
+ end
25
+ end
26
+ end
27
+
28
+ class << self
29
+ def included(base)
30
+ base.extend(ClassMethods)
31
+ end
32
+ end
33
+
34
+ def initialize(&)
35
+ instance_exec(&) if block_given?
36
+ end
37
+
38
+ def properties
39
+ self.class.properties.keys.each_with_object({}) do |prop, acc|
40
+ acc[prop] = instance_variable_get(:"@#{prop}")
41
+ end
42
+ end
43
+
44
+ def parse_property_value(value, object, ref, block)
45
+ if ref
46
+ OAPI::Ref.new(ref)
47
+ elsif block
48
+ object.new(&block)
49
+ elsif !value.nil?
50
+ value
51
+ else
52
+ raise ArgumentError, "ref, block or value must be provided"
53
+ end
54
+ end
55
+ end
data/lib/oapi/ref.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::Ref
4
+ attr_reader :ref
5
+
6
+ def initialize(ref) = @ref = ref
7
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::Schema
4
+ attr_reader :schema
5
+
6
+ def initialize
7
+ @schema = yield if block_given?
8
+ end
9
+
10
+ class << self
11
+ def parse(json)
12
+ ref = json[:$ref]
13
+ return OAPI::Ref.new(ref) if ref
14
+
15
+ new { json }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::Types::Array < OAPI::Types::Object
4
+ attr_reader :store
5
+
6
+ def initialize(store = [], &)
7
+ @store = store
8
+ super(&)
9
+ end
10
+
11
+ class << self
12
+ attr_reader :item_name, :item_type
13
+
14
+ def item(name, type)
15
+ @item_name = name
16
+ @item_type = type
17
+
18
+ define_method(name) do |&block|
19
+ @store << type.new(&block)
20
+ end
21
+ end
22
+
23
+ def parse(json) = new(json.map { item_type.parse(_1) })
24
+ end
25
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::Types::Map < OAPI::Types::Object
4
+ attr_reader :store
5
+
6
+ def initialize(store = {}, &)
7
+ @store = store
8
+ super(&)
9
+ end
10
+
11
+ class << self
12
+ attr_reader :item_name, :item_type
13
+
14
+ def item(name, type = nil)
15
+ @item_name = name
16
+ @item_type = type
17
+
18
+ define_method(name) do |key, &block|
19
+ raise ArgumentError, "'#{key}' already exists" if @store.include?(key.to_s)
20
+
21
+ @store[key] = type.new(&block)
22
+ end
23
+ end
24
+
25
+ def parse(json)
26
+ store = json.transform_values do |value|
27
+ item_type ? item_type.parse(value) : value
28
+ end
29
+ new(store)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OAPI::Types::Object
4
+ using OAPI::Monkey
5
+
6
+ include OAPI::Properties
7
+
8
+ class << self
9
+ def parse(json)
10
+ new.tap do |obj|
11
+ (properties || []).each do |name, type|
12
+ value = json[name.camelize]
13
+ next if value.nil?
14
+
15
+ value = type.parse(value) if value.is_a?(Hash) || (value.is_a?(Array) && type)
16
+ obj.send(name, value)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OAPI
4
+ VERSION = "0.1.0"
5
+ end
data/lib/oapi.rb ADDED
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "oapi/version"
4
+
5
+ require "zeitwerk"
6
+
7
+ loader = Zeitwerk::Loader.for_gem
8
+
9
+ loader.inflector.inflect(
10
+ "oapi" => "OAPI",
11
+ "open_api" => "OpenAPI",
12
+ "json" => "JSON"
13
+ )
14
+
15
+ loader.setup
16
+
17
+ module OAPI
18
+ class Error < StandardError
19
+ # Your code goes here...
20
+ end
21
+
22
+ class UnsupportedSpecVersion < Error
23
+ end
24
+
25
+ class << self
26
+ def define(&)
27
+ # TODO: check version
28
+ # raise UnsupportedSpecVersion, "unsupported version #{version}" unless version.start_with?("3.0")
29
+
30
+ OAPI::OpenAPI::V30::Definition.new(&)
31
+ end
32
+
33
+ def parse(json)
34
+ version = json[:openapi]
35
+ raise UnsupportedSpecVersion, "unsupported version #{version}" unless version.start_with?("3.0")
36
+
37
+ OAPI::OpenAPI::V30::Definition.parse(json)
38
+ end
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gleb Sinyavskiy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: zeitwerk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.6'
27
+ description: A ruby DSL for building OpenAPI documents.
28
+ email:
29
+ - zhulik.gleb@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".overcommit.yml"
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - CODE_OF_CONDUCT.md
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/oapi.rb
44
+ - lib/oapi/monkey.rb
45
+ - lib/oapi/open_api/v30/callbacks.rb
46
+ - lib/oapi/open_api/v30/components.rb
47
+ - lib/oapi/open_api/v30/content.rb
48
+ - lib/oapi/open_api/v30/definition.rb
49
+ - lib/oapi/open_api/v30/examples.rb
50
+ - lib/oapi/open_api/v30/external_docs.rb
51
+ - lib/oapi/open_api/v30/headers.rb
52
+ - lib/oapi/open_api/v30/info.rb
53
+ - lib/oapi/open_api/v30/media_type.rb
54
+ - lib/oapi/open_api/v30/parameters.rb
55
+ - lib/oapi/open_api/v30/paths.rb
56
+ - lib/oapi/open_api/v30/request_body.rb
57
+ - lib/oapi/open_api/v30/responses.rb
58
+ - lib/oapi/open_api/v30/serializers/json.rb
59
+ - lib/oapi/open_api/v30/serializers/ruby.rb
60
+ - lib/oapi/open_api/v30/serializers/serializer.rb
61
+ - lib/oapi/open_api/v30/servers.rb
62
+ - lib/oapi/open_api/v30/tags.rb
63
+ - lib/oapi/properties.rb
64
+ - lib/oapi/ref.rb
65
+ - lib/oapi/schema.rb
66
+ - lib/oapi/types/array.rb
67
+ - lib/oapi/types/map.rb
68
+ - lib/oapi/types/object.rb
69
+ - lib/oapi/version.rb
70
+ homepage: https://github.com/zhulik/oapi
71
+ licenses:
72
+ - MIT
73
+ metadata:
74
+ homepage_uri: https://github.com/zhulik/oapi
75
+ source_code_uri: https://github.com/zhulik/oapi
76
+ rubygems_mfa_required: 'true'
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 3.3.0
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.5.9
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: A ruby DSL for building OpenAPI documents.
96
+ test_files: []