zilla 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: 3cdd0d45c3286dd333971aaea9f860912c2808a636c922111ad501357102a1bd
4
+ data.tar.gz: 44b18060657c6cb42944dc1ad20d249263ba8acfa176b1ef1d070bea3a5351da
5
+ SHA512:
6
+ metadata.gz: 04c69f835148c7ad68fdc90471bdf67e816fbf50d1313720d273f66f72ea916970d8a5a5099028a716609390c12665deeb1fc5de12146775e1deaab566387584
7
+ data.tar.gz: 3bd57eb44508103712642dbd054a69d6f56926fcc01a3c4eec576556aadc899e152b79ec5c50366f25e8d80d572741927b9b1547f278108bdd5ec03a67ff7bb8
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 progress
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,47 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.2
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+
6
+ require:
7
+ - rubocop-performance
8
+ - rubocop-rspec
9
+
10
+ Layout/LineLength:
11
+ Max: 120
12
+ Exclude:
13
+ - spec/**/*_spec.rb
14
+
15
+ Metrics/BlockLength:
16
+ Exclude:
17
+ - spec/**/*_spec.rb
18
+ - "*.gemspec"
19
+
20
+ Metrics/MethodLength:
21
+ Max: 10
22
+
23
+ RSpec/NamedSubject:
24
+ Enabled: false
25
+
26
+ Style/StringLiterals:
27
+ Enabled: true
28
+ EnforcedStyle: double_quotes
29
+
30
+ Style/StringLiteralsInInterpolation:
31
+ Enabled: true
32
+ EnforcedStyle: double_quotes
33
+
34
+ Style/Documentation:
35
+ Enabled: false
36
+
37
+ Style/SymbolArray:
38
+ EnforcedStyle: brackets
39
+
40
+ Style/WordArray:
41
+ EnforcedStyle: brackets
42
+
43
+ Style/ClassAndModuleChildren:
44
+ EnforcedStyle: compact
45
+
46
+ RSpec/NestedGroups:
47
+ Max: 5
@@ -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,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in async-openapi.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+ gem "rubocop-performance"
14
+ gem "rubocop-rspec"
15
+
16
+ gem "async-rspec"
17
+ gem "simplecov"
18
+ gem "webmock"
19
+
20
+ gem "overcommit"
21
+ gem "solargraph"
data/Gemfile.lock ADDED
@@ -0,0 +1,193 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ zilla (0.1.0)
5
+ async (~> 2.3.0)
6
+ async-http-faraday (~> 0.11)
7
+ faraday (~> 2.7.0)
8
+ json_schemer (~> 0.2.0)
9
+ memery (~> 1.4.0)
10
+ oj (~> 3.13.0)
11
+ zeitwerk (~> 2.6.0)
12
+
13
+ GEM
14
+ remote: https://rubygems.org/
15
+ specs:
16
+ addressable (2.8.1)
17
+ public_suffix (>= 2.0.2, < 6.0)
18
+ ast (2.4.2)
19
+ async (2.3.1)
20
+ console (~> 1.10)
21
+ io-event (~> 1.1)
22
+ timers (~> 4.1)
23
+ async-http (0.59.4)
24
+ async (>= 1.25)
25
+ async-io (>= 1.28)
26
+ async-pool (>= 0.2)
27
+ protocol-http (~> 0.23.1)
28
+ protocol-http1 (~> 0.14.0)
29
+ protocol-http2 (~> 0.14.0)
30
+ traces (>= 0.8.0)
31
+ async-http-faraday (0.11.0)
32
+ async-http (~> 0.42)
33
+ faraday
34
+ async-io (1.34.1)
35
+ async
36
+ async-pool (0.3.12)
37
+ async (>= 1.25)
38
+ async-rspec (1.16.1)
39
+ rspec (~> 3.0)
40
+ rspec-files (~> 1.0)
41
+ rspec-memory (~> 1.0)
42
+ backport (1.2.0)
43
+ benchmark (0.2.1)
44
+ childprocess (4.1.0)
45
+ console (1.16.2)
46
+ fiber-local
47
+ crack (0.4.5)
48
+ rexml
49
+ diff-lcs (1.5.0)
50
+ docile (1.4.0)
51
+ e2mmap (0.1.0)
52
+ ecma-re-validator (0.4.0)
53
+ regexp_parser (~> 2.2)
54
+ faraday (2.7.4)
55
+ faraday-net_http (>= 2.0, < 3.1)
56
+ ruby2_keywords (>= 0.0.4)
57
+ faraday-net_http (3.0.2)
58
+ fiber-local (1.0.0)
59
+ hana (1.3.7)
60
+ hashdiff (1.0.1)
61
+ iniparse (1.5.0)
62
+ io-event (1.1.6)
63
+ jaro_winkler (1.5.4)
64
+ json (2.6.3)
65
+ json_schemer (0.2.24)
66
+ ecma-re-validator (~> 0.3)
67
+ hana (~> 1.3)
68
+ regexp_parser (~> 2.0)
69
+ uri_template (~> 0.7)
70
+ kramdown (2.4.0)
71
+ rexml
72
+ kramdown-parser-gfm (1.1.0)
73
+ kramdown (~> 2.0)
74
+ memery (1.4.1)
75
+ ruby2_keywords (~> 0.0.2)
76
+ nokogiri (1.14.0-x86_64-linux)
77
+ racc (~> 1.4)
78
+ oj (3.13.23)
79
+ overcommit (0.60.0)
80
+ childprocess (>= 0.6.3, < 5)
81
+ iniparse (~> 1.4)
82
+ rexml (~> 3.2)
83
+ parallel (1.22.1)
84
+ parser (3.2.0.0)
85
+ ast (~> 2.4.1)
86
+ protocol-hpack (1.4.2)
87
+ protocol-http (0.23.12)
88
+ protocol-http1 (0.14.6)
89
+ protocol-http (~> 0.22)
90
+ protocol-http2 (0.14.2)
91
+ protocol-hpack (~> 1.4)
92
+ protocol-http (~> 0.18)
93
+ public_suffix (5.0.1)
94
+ racc (1.6.2)
95
+ rainbow (3.1.1)
96
+ rake (13.0.6)
97
+ regexp_parser (2.6.2)
98
+ reverse_markdown (2.1.1)
99
+ nokogiri
100
+ rexml (3.2.5)
101
+ rspec (3.12.0)
102
+ rspec-core (~> 3.12.0)
103
+ rspec-expectations (~> 3.12.0)
104
+ rspec-mocks (~> 3.12.0)
105
+ rspec-core (3.12.0)
106
+ rspec-support (~> 3.12.0)
107
+ rspec-expectations (3.12.2)
108
+ diff-lcs (>= 1.2.0, < 2.0)
109
+ rspec-support (~> 3.12.0)
110
+ rspec-files (1.1.3)
111
+ rspec (~> 3.0)
112
+ rspec-memory (1.0.3)
113
+ rspec (~> 3.0)
114
+ rspec-mocks (3.12.3)
115
+ diff-lcs (>= 1.2.0, < 2.0)
116
+ rspec-support (~> 3.12.0)
117
+ rspec-support (3.12.0)
118
+ rubocop (1.44.1)
119
+ json (~> 2.3)
120
+ parallel (~> 1.10)
121
+ parser (>= 3.2.0.0)
122
+ rainbow (>= 2.2.2, < 4.0)
123
+ regexp_parser (>= 1.8, < 3.0)
124
+ rexml (>= 3.2.5, < 4.0)
125
+ rubocop-ast (>= 1.24.1, < 2.0)
126
+ ruby-progressbar (~> 1.7)
127
+ unicode-display_width (>= 2.4.0, < 3.0)
128
+ rubocop-ast (1.24.1)
129
+ parser (>= 3.1.1.0)
130
+ rubocop-capybara (2.17.0)
131
+ rubocop (~> 1.41)
132
+ rubocop-performance (1.15.2)
133
+ rubocop (>= 1.7.0, < 2.0)
134
+ rubocop-ast (>= 0.4.0)
135
+ rubocop-rspec (2.18.1)
136
+ rubocop (~> 1.33)
137
+ rubocop-capybara (~> 2.17)
138
+ ruby-progressbar (1.11.0)
139
+ ruby2_keywords (0.0.5)
140
+ simplecov (0.22.0)
141
+ docile (~> 1.1)
142
+ simplecov-html (~> 0.11)
143
+ simplecov_json_formatter (~> 0.1)
144
+ simplecov-html (0.12.3)
145
+ simplecov_json_formatter (0.1.4)
146
+ solargraph (0.48.0)
147
+ backport (~> 1.2)
148
+ benchmark
149
+ bundler (>= 1.17.2)
150
+ diff-lcs (~> 1.4)
151
+ e2mmap
152
+ jaro_winkler (~> 1.5)
153
+ kramdown (~> 2.3)
154
+ kramdown-parser-gfm (~> 1.1)
155
+ parser (~> 3.0)
156
+ reverse_markdown (>= 1.0.5, < 3)
157
+ rubocop (>= 0.52)
158
+ thor (~> 1.0)
159
+ tilt (~> 2.0)
160
+ yard (~> 0.9, >= 0.9.24)
161
+ thor (1.2.1)
162
+ tilt (2.0.11)
163
+ timers (4.3.5)
164
+ traces (0.8.0)
165
+ unicode-display_width (2.4.2)
166
+ uri_template (0.7.0)
167
+ webmock (3.18.1)
168
+ addressable (>= 2.8.0)
169
+ crack (>= 0.3.2)
170
+ hashdiff (>= 0.4.0, < 2.0.0)
171
+ webrick (1.7.0)
172
+ yard (0.9.28)
173
+ webrick (~> 1.7.0)
174
+ zeitwerk (2.6.6)
175
+
176
+ PLATFORMS
177
+ x86_64-linux
178
+
179
+ DEPENDENCIES
180
+ async-rspec
181
+ overcommit
182
+ rake (~> 13.0)
183
+ rspec (~> 3.0)
184
+ rubocop (~> 1.21)
185
+ rubocop-performance
186
+ rubocop-rspec
187
+ simplecov
188
+ solargraph
189
+ webmock
190
+ zilla!
191
+
192
+ BUNDLED WITH
193
+ 2.4.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 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
+ # Zilla
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/zilla`. 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/zhulik/zilla. 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/zhulik/zilla/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 Zilla project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/zhulik/zilla/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: [:spec, :rubocop]
data/exe/zilla ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "zilla"
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zilla::ClientFactory
4
+ class UnsupportedVersion < Zilla::Error; end
5
+
6
+ CLIENTS = {
7
+ "2.0" => Zilla::Versions::V20::Client
8
+ }.freeze
9
+
10
+ class << self
11
+ def build(input)
12
+ definition = Zilla::LoaderFactory.build(input).load
13
+
14
+ version = definition["swagger"]
15
+
16
+ raise ArgumentError, "#{input.inspect} is not an OpenAPI definition" if version.nil?
17
+ raise UnsupportedVersion, "usupported version #{version.inspect}" if version && CLIENTS[version].nil?
18
+
19
+ CLIENTS[version].new(definition)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zilla::LoaderFactory
4
+ class UnknownLoader < Zilla::Error; end
5
+
6
+ class << self
7
+ def build(input)
8
+ case input
9
+ when Hash
10
+ Zilla::Loaders::Hash
11
+ when String
12
+ for_string(input)
13
+ end.new(input)
14
+ rescue NoMethodError
15
+ raise UnknownLoader, "cannot find loader for #{input.inspect}"
16
+ end
17
+
18
+ private
19
+
20
+ def for_string(string)
21
+ if File.exist?(string)
22
+ Zilla::Loaders::File
23
+ elsif ["http", "https"].any? { string.start_with?(_1) }
24
+ Zilla::Loaders::HTTP
25
+ elsif json?(string)
26
+ Zilla::Loaders::String
27
+ end
28
+ end
29
+
30
+ def json?(string)
31
+ Oj.load(string)
32
+ true
33
+ rescue Oj::ParseError
34
+ false
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Zilla::Loaders::File < Zilla::Loaders::Loader
4
+ def load_string = File.read(input)
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Zilla::Loaders::Hash < Zilla::Loaders::Loader
4
+ def load = input
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Zilla::Loaders::HTTP < Zilla::Loaders::Loader
4
+ def load_string
5
+ Faraday.new(input) do |f|
6
+ f.adapter :async_http
7
+ f.response :raise_error
8
+ end.get.body
9
+ end
10
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Zilla::Loaders::Loader
4
+ class LoaderError < Zilla::Error; end
5
+
6
+ attr_reader :input
7
+
8
+ def initialize(input)
9
+ @input = input
10
+ end
11
+
12
+ def load
13
+ Oj.load(load_string)
14
+ rescue StandardError => e
15
+ raise LoaderError, e.message
16
+ end
17
+
18
+ private
19
+
20
+ def load_string = raise NotImplementedError
21
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Zilla::Loaders::String < Zilla::Loaders::Loader
4
+ def load_string = input
5
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zilla
4
+ module Version
5
+ end
6
+
7
+ VERSION = "0.1.0"
8
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Zilla::Versions::V20::API
4
+ include Zilla::Versions::V20
5
+
6
+ include Memery
7
+
8
+ attr_reader :json
9
+
10
+ def initialize(json)
11
+ @json = json
12
+ end
13
+
14
+ [:host, :schemes].each do |name|
15
+ define_method(name) do
16
+ json[name.to_s]
17
+ end
18
+ end
19
+
20
+ memoize def info = OpenStruct.new(json["info"]) # rubocop:disable Style/OpenStructUse
21
+
22
+ memoize def definitions
23
+ json.fetch("definitions", {}).each_with_object({}) do |(k, v), acc|
24
+ acc[k] = Definition.new(k, v, definitions: acc)
25
+ end
26
+ end
27
+
28
+ memoize def paths
29
+ json.fetch("paths", {}).each_with_object({}) do |(k, v), acc|
30
+ acc[k] = Path.new(k, v, definitions:)
31
+ end
32
+ end
33
+
34
+ memoize def endpoints = paths.values.flat_map { _1.endpoints.values }
35
+
36
+ # TODO: :security, :securityDefinitions
37
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Zilla::Versions::V20::Client
4
+ include Zilla::Versions::V20
5
+
6
+ include Memery
7
+
8
+ attr_reader :json, :host, :scheme
9
+
10
+ def initialize(json, host: nil, scheme: :https)
11
+ @json = json
12
+ @host = host || api.host || raise(ArgumentError, ":host must be specified")
13
+ @scheme = scheme.to_s
14
+
15
+ unless api.schemes.include?(@scheme)
16
+ raise ArgumentError, "unsupported scheme #{@scheme.inspect}. Supported: #{api.schemes}"
17
+ end
18
+
19
+ define_operations!
20
+ end
21
+
22
+ memoize def api = API.new(json)
23
+
24
+ memoize def executor = Executor.new(scheme, host)
25
+
26
+ private
27
+
28
+ def define_operations!
29
+ api.endpoints.each do |endpoint|
30
+ define_singleton_method(endpoint.operationId) do |*args, **params|
31
+ executor.call(endpoint, *args, **params)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Zilla::Versions::V20::Definition
4
+ include Memery
5
+
6
+ attr_reader :name, :json, :definitions
7
+
8
+ def initialize(name, json, definitions: {})
9
+ @name = name
10
+ @json = json
11
+ @definitions = definitions
12
+ end
13
+
14
+ def valid?(*, **) = schema.valid?(*, **)
15
+
16
+ memoize def schema = JSONSchemer.schema(json.merge("definitions" => definitions), insert_property_defaults: true)
17
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Zilla::Versions::V20::Endpoint
4
+ include Zilla::Versions::V20
5
+
6
+ include Memery
7
+
8
+ attr_reader :path, :method, :json, :definitions
9
+
10
+ def initialize(path, method, json, definitions: {})
11
+ @path = path
12
+ @method = method
13
+ @json = json
14
+ @definitions = definitions
15
+ end
16
+
17
+ [:summary, :description, :tags, :operationId].each do |name|
18
+ define_method(name) do
19
+ json[name.to_s]
20
+ end
21
+ end
22
+
23
+ memoize def path_template = PathTemplate.new(path)
24
+
25
+ memoize def parameters = Parameters.new(json["parameters"], definitions:)
26
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Zilla::Versions::V20::Executor
4
+ include Memery
5
+
6
+ attr_reader :scheme, :host
7
+
8
+ def initialize(scheme, host)
9
+ @scheme = scheme
10
+ @host = host
11
+ end
12
+
13
+ def call(endpoint, *args, **params)
14
+ path_parameters = endpoint.path_template.path_parameters(*args, **params)
15
+ validate_path_params!(endpoint, path_parameters)
16
+
17
+ path = endpoint.path_template.render(*args, **path_parameters)
18
+ connection.public_send(endpoint.method, path)
19
+ end
20
+
21
+ private
22
+
23
+ memoize def connection
24
+ Faraday.new("#{scheme}://#{host}") do |f|
25
+ f.adapter :async_http
26
+ f.response :raise_error
27
+ end
28
+ end
29
+
30
+ def validate_path_params!(endpoint, params)
31
+ params.each do |k, v|
32
+ endpoint.parameters[k].validate!(v)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Zilla::Versions::V20::Parameter
4
+ include Memery
5
+
6
+ attr_reader :json, :definitions
7
+
8
+ def initialize(json, definitions: {})
9
+ @json = json
10
+ @definitions = definitions
11
+ end
12
+
13
+ [:name, :in, :description, :required, :type].each do |name|
14
+ define_method(name) do
15
+ json[name.to_s]
16
+ end
17
+ end
18
+
19
+ def validate!(value)
20
+ errors = schema.validate(value)
21
+
22
+ raise ArgumentError, errors.to_a.to_s if errors.any?
23
+ end
24
+
25
+ memoize def schema = JSONSchemer.schema(json.merge("definitions" => definitions), insert_property_defaults: true)
26
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Zilla::Versions::V20::Parameters
4
+ include Zilla::Versions::V20
5
+
6
+ include Memery
7
+
8
+ attr_reader :json, :definitions
9
+
10
+ def initialize(json, definitions: {})
11
+ @json = json
12
+ @definitions = definitions
13
+ end
14
+
15
+ memoize def parameters
16
+ json.map { Parameter.new(_1, definitions:) }
17
+ .group_by(&:name)
18
+ .transform_values(&:first)
19
+ end
20
+
21
+ def empty? = parameters.empty?
22
+ def names = parameters.keys
23
+ def [](name) = parameters[name]
24
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Zilla::Versions::V20::Path
4
+ include Zilla::Versions::V20
5
+
6
+ include Memery
7
+
8
+ attr_reader :path, :json, :definitions
9
+
10
+ def initialize(path, json, definitions: {})
11
+ @path = path
12
+ @json = json
13
+ @definitions = definitions
14
+ end
15
+
16
+ memoize def endpoints
17
+ json.each_with_object({}) do |(k, v), acc|
18
+ acc[k] = Endpoint.new(path, k, v, definitions:)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Zilla::Versions::V20::PathTemplate
4
+ include Memery
5
+
6
+ attr_reader :template
7
+
8
+ RE = /(\{\s*(.+?)\s*\})/
9
+
10
+ def initialize(template)
11
+ @template = template
12
+ @parameters = []
13
+
14
+ @parameters = template.scan(RE).map(&:reverse).to_h
15
+ end
16
+
17
+ memoize def parameters = @parameters.keys.map(&:to_sym)
18
+
19
+ def render(*args, **params) = render_params(path_parameters(*args, **params))
20
+
21
+ def path_parameters(*args, **params) # rubocop:disable Metrics/AbcSize
22
+ params = params.slice(*parameters)
23
+
24
+ raise ArgumentError, "path parameters must only be passed as args OR params" if args.any? && params.any?
25
+
26
+ count = args.count + params.count
27
+ raise ArgumentError, "#{@parameters.count} path parameters expected, given: #{count}" if count != @parameters.count
28
+
29
+ return validate!(params.transform_keys(&:to_s)) if params.any?
30
+
31
+ @parameters.keys.each.with_index.with_object({}) do |(k, i), acc|
32
+ acc[k] = args[i]
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def validate!(params)
39
+ params.each_key do |k|
40
+ raise ArgumentError, "unknown path parameter: #{k.inspect}, known: #{parameters}" unless @parameters.key?(k.to_s)
41
+ end
42
+ end
43
+
44
+ def render_params(params)
45
+ params.reduce(@template) do |acc, (k, v)|
46
+ acc.sub(@parameters[k.to_s], v.to_s)
47
+ end
48
+ end
49
+ end
data/lib/zilla.rb ADDED
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "async"
4
+ require "async/http/faraday"
5
+ require "faraday"
6
+ require "oj"
7
+ require "zeitwerk"
8
+ require "json_schemer"
9
+ require "memery"
10
+
11
+ loader = Zeitwerk::Loader.for_gem
12
+
13
+ loader.inflector.inflect(
14
+ "http" => "HTTP",
15
+ "api" => "API"
16
+ )
17
+
18
+ # Your code goes here...
19
+ module Zilla # rubocop:disable Style/ClassAndModuleChildren
20
+ class Error < StandardError; end
21
+ end
22
+
23
+ loader.setup
24
+ loader.eager_load
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zilla
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: 2023-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: async
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.3.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: async-http-faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.11'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.7.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.7.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: json_schemer
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.2.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: memery
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.4.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.4.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: oj
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 3.13.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 3.13.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: zeitwerk
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.6.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.6.0
111
+ description: OpenAPI client for ruby
112
+ email:
113
+ - zhulik.gleb@gmail.com
114
+ executables:
115
+ - zilla
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".overcommit.yml"
120
+ - ".rspec"
121
+ - ".rubocop.yml"
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - exe/zilla
129
+ - lib/zilla.rb
130
+ - lib/zilla/client_factory.rb
131
+ - lib/zilla/loader_factory.rb
132
+ - lib/zilla/loaders/file.rb
133
+ - lib/zilla/loaders/hash.rb
134
+ - lib/zilla/loaders/http.rb
135
+ - lib/zilla/loaders/loader.rb
136
+ - lib/zilla/loaders/string.rb
137
+ - lib/zilla/version.rb
138
+ - lib/zilla/versions/v20/api.rb
139
+ - lib/zilla/versions/v20/client.rb
140
+ - lib/zilla/versions/v20/definition.rb
141
+ - lib/zilla/versions/v20/endpoint.rb
142
+ - lib/zilla/versions/v20/executor.rb
143
+ - lib/zilla/versions/v20/parameter.rb
144
+ - lib/zilla/versions/v20/parameters.rb
145
+ - lib/zilla/versions/v20/path.rb
146
+ - lib/zilla/versions/v20/path_template.rb
147
+ homepage: https://github.com/zhulik/zilla
148
+ licenses:
149
+ - MIT
150
+ metadata:
151
+ homepage_uri: https://github.com/zhulik/zilla
152
+ source_code_uri: https://github.com/zhulik/zilla
153
+ changelog_uri: https://github.com/zhulik/zilla/releases
154
+ rubygems_mfa_required: 'true'
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: 3.2.0
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubygems_version: 3.4.1
171
+ signing_key:
172
+ specification_version: 4
173
+ summary: OpenAPI client for ruby
174
+ test_files: []