interpol_schema_2_openapi_schema 0.1.0

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: c66000ad88b22628fbdb3575cf380b15ae3254e525336564b8d36fbc1e8a1ede
4
+ data.tar.gz: f1df4a032f7951304f2b2ab7133477259fafba620cd6418265e11b05c7f93096
5
+ SHA512:
6
+ metadata.gz: '028d16c280eef252132972b73cd79c7c2f0655f90825af70af8cb21ed77002de0bebc1408678f7bdfcee610b027a43299cd579052c293982e5479e13ab3b4524'
7
+ data.tar.gz: 8e9aea032b574d65236c2d830e7630a7dc453b6e659e0296d88f7f4d3aedd19e5405921f03299a03822647cb2adaa02dbd470f8f22d5bba47ebcf2cdece7b1f7
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in interpol_schema_2_openapi_schema.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # InterpolSchema2OpenapiSchema
2
+
3
+ This gem provides the CLI to convert [Interpol](https://github.com/seomoz/interpol) endpoint definition to [OpenAPI](https://github.com/OAI/OpenAPI-Specification) definition.
4
+
5
+ ## Supported OpenAPI Version
6
+
7
+ - OpenAPI 2.0
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'interpol_schema_2_openapi_schema'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install interpol_schema_2_openapi_schema
24
+
25
+ ## Usage
26
+
27
+ ### Convert to Path Objects
28
+
29
+ Give an endpoint definition yaml.
30
+
31
+ ```
32
+ $ istos ./path/to/index.yaml
33
+
34
+ # dump to stdout
35
+ ```
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/interpol_schema_2_openapi_schema.
46
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "interpol_schema_2_openapi_schema"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/istos ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "interpol_schema_2_openapi_schema"
4
+
5
+ InterpolSchema2OpenapiSchema::Cli.start(ARGV)
@@ -0,0 +1,32 @@
1
+ require_relative 'lib/interpol_schema_2_openapi_schema/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "interpol_schema_2_openapi_schema"
5
+ spec.version = InterpolSchema2OpenapiSchema::VERSION
6
+ spec.authors = ["ku00"]
7
+ spec.email = ["skentaro36@gmail.com"]
8
+
9
+ spec.summary = %q{Convert Interpol definition to OpenAPI definition.}
10
+ spec.description = %q{Convert Interpol definition to OpenAPI definition.}
11
+ spec.homepage = "https://github.com/ku00/interpol_schema_2_openapi_schema"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7.1")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/ku00/interpol_schema_2_openapi_schema"
16
+ spec.metadata["changelog_uri"] = "https://github.com/ku00/interpol_schema_2_openapi_schema"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency "thor", "~> 1.0.0"
28
+
29
+ spec.add_development_dependency "bundler", "~> 2.1.0"
30
+ spec.add_development_dependency "rake", "~> 12.0"
31
+ spec.add_development_dependency "minitest", "~> 5.0"
32
+ end
@@ -0,0 +1,31 @@
1
+ require 'yaml'
2
+
3
+ module InterpolSchema
4
+ class Parser
5
+ attr_reader :schema
6
+
7
+ def initialize(path)
8
+ @schema = YAML.load_file path
9
+ end
10
+
11
+ def http_method
12
+ schema.dig("method")
13
+ end
14
+
15
+ def request_params_in_path
16
+ schema.dig("definitions", 0, "path_params")
17
+ end
18
+
19
+ def request_params_in_query
20
+ schema.dig("definitions", 0, "query_params")
21
+ end
22
+
23
+ def response_code
24
+ schema.dig("definitions", 1, "status_codes", 0)
25
+ end
26
+
27
+ def response_params
28
+ schema.dig("definitions", 1, "schema")
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,29 @@
1
+ require 'thor'
2
+
3
+ require "interpol_schema_2_openapi_schema/version"
4
+ require "interpol_schema/parser"
5
+ require "openapi_schema/path_object/builder"
6
+
7
+ module InterpolSchema2OpenapiSchema
8
+ class Error < StandardError; end
9
+
10
+ class Cli < Thor
11
+ default_command :build_path_object
12
+
13
+ desc "build_path_object YAML_FILE_PATH", "Build OpenAPI path object"
14
+ def build_path_object(file_path)
15
+ puts OpenapiSchema::PathObject::Builder.new(
16
+ InterpolSchema::Parser.new(file_path)
17
+ ).build
18
+ end
19
+
20
+ map %w[--version -v] => :version
21
+ desc "--version, -v", "Print the version"
22
+ def version
23
+ puts InterpolSchema2OpenapiSchema::VERSION
24
+ end
25
+
26
+ # To make build-command available in no calling itself
27
+ ARGV.unshift(self.default_command) unless self.all_tasks.has_key?(ARGV[0]) || self.instance_variable_get(:@map).has_key?(ARGV[0])
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module InterpolSchema2OpenapiSchema
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,88 @@
1
+ require 'yaml'
2
+
3
+ module OpenapiSchema
4
+ module PathObject
5
+ class Builder
6
+ attr_reader :interpol_schema
7
+
8
+ def initialize(interpol_schema)
9
+ @interpol_schema = interpol_schema
10
+ end
11
+
12
+ def build
13
+ base_template.to_yaml
14
+ end
15
+
16
+ def base_template
17
+ {
18
+ "#{interpol_schema.http_method.downcase}" => {
19
+ "summary" => "summary",
20
+ "description" => "description",
21
+ "parameters" => request_params,
22
+ "responses" => {
23
+ "#{interpol_schema.response_code}" => {
24
+ "description" => "response description",
25
+ "schema" => interpol_schema.response_params
26
+ }
27
+ }
28
+ }
29
+ }
30
+ end
31
+
32
+ def request_params
33
+ [
34
+ request_params_in_path_template,
35
+ request_params_in_query_template,
36
+ ].flatten.compact
37
+ end
38
+
39
+ def request_params_in_path_template
40
+ return if empty_param?(interpol_schema.request_params_in_path)
41
+
42
+ interpol_schema.request_params_in_path["properties"].map do |name, detail|
43
+ {
44
+ "in" => "path",
45
+ "name" => name,
46
+ "type" => detail["type"],
47
+ "required" => true,
48
+ }.merge(
49
+ enum_params_template(detail)
50
+ )
51
+ end
52
+ end
53
+
54
+ def request_params_in_query_template
55
+ return if empty_param?(interpol_schema.request_params_in_query)
56
+
57
+ interpol_schema.request_params_in_query["properties"].map do |name, detail|
58
+ {
59
+ "in" => "query",
60
+ "name" => name,
61
+ "type" => detail["type"],
62
+ }.merge(enum_params_template(detail))
63
+ .merge(required_param_template(detail))
64
+ end
65
+ end
66
+
67
+ def enum_params_template(target)
68
+ return {} if target["enum"].nil?
69
+
70
+ {
71
+ "enum" => target["enum"],
72
+ }
73
+ end
74
+
75
+ def required_param_template(target)
76
+ return {} if target["optional"].nil? || target["optional"] === true
77
+
78
+ {
79
+ "required" => true
80
+ }
81
+ end
82
+
83
+ def empty_param?(target)
84
+ target.nil? || target === {}
85
+ end
86
+ end
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: interpol_schema_2_openapi_schema
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ku00
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.1.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description: Convert Interpol definition to OpenAPI definition.
70
+ email:
71
+ - skentaro36@gmail.com
72
+ executables:
73
+ - istos
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - exe/istos
85
+ - interpol_schema_2_openapi_schema.gemspec
86
+ - lib/interpol_schema/parser.rb
87
+ - lib/interpol_schema_2_openapi_schema.rb
88
+ - lib/interpol_schema_2_openapi_schema/version.rb
89
+ - lib/openapi_schema/path_object/builder.rb
90
+ homepage: https://github.com/ku00/interpol_schema_2_openapi_schema
91
+ licenses: []
92
+ metadata:
93
+ homepage_uri: https://github.com/ku00/interpol_schema_2_openapi_schema
94
+ source_code_uri: https://github.com/ku00/interpol_schema_2_openapi_schema
95
+ changelog_uri: https://github.com/ku00/interpol_schema_2_openapi_schema
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 2.7.1
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubygems_version: 3.1.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Convert Interpol definition to OpenAPI definition.
115
+ test_files: []