tiki 0.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.
data/lib/tiki/spec.rb ADDED
@@ -0,0 +1,159 @@
1
+ require 'json'
2
+ require_relative './props'
3
+ require_relative './path-item'
4
+ require_relative './components'
5
+ require_relative './server'
6
+
7
+ using Props
8
+
9
+ class License
10
+ props :name, :url
11
+
12
+ def initialize(name = nil, url = nil)
13
+ @name = name
14
+ @url = url
15
+ end
16
+
17
+ def to_spec
18
+ props = { name: @name }
19
+ props[:url] = @url if @url
20
+ props
21
+ end
22
+ end
23
+
24
+ class Contact
25
+ props :name, :url, :email
26
+ named_props :url, :email
27
+ scalar_props :name, :url, :email
28
+
29
+ def initialize(name = nil, **named)
30
+ @name = name
31
+ named_props named
32
+ end
33
+
34
+ def to_spec
35
+ scalar_props
36
+ end
37
+ end
38
+
39
+ class Info
40
+ props :title, :version, :description, :terms_of_service
41
+ scalar_props :title, :version, :description, :terms_of_service
42
+ object_props :license, :contact
43
+
44
+ def initialize(title = nil, version = nil, license: nil)
45
+ @title = title
46
+ @version = version
47
+ @license = License.new license if license
48
+ end
49
+
50
+ def license(name = nil, url = nil, &block)
51
+ @license = License.new name, url
52
+ @license.instance_eval(&block) if block
53
+ end
54
+
55
+ def contact(name = nil, **named, &block)
56
+ @contact = Contact.new name, **named
57
+ @contact.instance_eval(&block) if block
58
+ end
59
+
60
+ def to_spec
61
+ props = {}
62
+ scalar_props props
63
+ object_props props
64
+ end
65
+
66
+ alias terms terms_of_service
67
+ end
68
+
69
+ class Spec
70
+ include ServerMethods
71
+
72
+ object_props :info, :components
73
+ hash_props :paths
74
+ array_props :servers
75
+
76
+ def initialize
77
+ @paths = []
78
+ end
79
+
80
+ def version(spec_version)
81
+ @spec_version = spec_version
82
+ end
83
+
84
+ def openapi(spec_version = '3.0.3', &block)
85
+ @spec_version = spec_version
86
+ instance_eval(&block)
87
+ end
88
+
89
+ def info(title = nil, version = nil, &block)
90
+ @info = Info.new title, version
91
+ @info.instance_eval(&block) if block
92
+ end
93
+
94
+ def path(url, summary = nil, **named, &block)
95
+ root = PathItemRoot.new @paths
96
+ parent = root.child url
97
+ path = PathItem.new parent, summary, **named
98
+ path.instance_eval(&block) if block
99
+ parent.add path
100
+ end
101
+
102
+ def components(&block)
103
+ return unless block
104
+
105
+ @components ||= Components.new
106
+ @components.instance_eval(&block)
107
+ end
108
+
109
+ def to_spec
110
+ @paths.each do |(url, path_item)|
111
+ path_item.check_parameters url
112
+ end
113
+ props = { openapi: @spec_version }
114
+ object_props props
115
+ hash_props props
116
+ array_props props
117
+ props
118
+ end
119
+ end
120
+
121
+ class PathItemRoot
122
+ attr_reader :paths
123
+
124
+ def initialize(paths)
125
+ @paths = paths
126
+ end
127
+
128
+ def child(url)
129
+ PathItemParent.new url, self, []
130
+ end
131
+
132
+ def parameters
133
+ []
134
+ end
135
+ end
136
+
137
+ class PathItemParent
138
+ def initialize(url, parent, parameters)
139
+ @url = url
140
+ @parent = parent
141
+ @parameters = parameters
142
+ end
143
+
144
+ def child(url, parameters)
145
+ PathItemParent.new @url + url, self, parameters
146
+ end
147
+
148
+ def add(path_item)
149
+ @parent.paths.push [@url, path_item]
150
+ end
151
+
152
+ def paths
153
+ @parent.paths
154
+ end
155
+
156
+ def parameters
157
+ @parent.parameters + @parameters
158
+ end
159
+ end
@@ -0,0 +1,65 @@
1
+ require_relative './spec'
2
+
3
+ describe do
4
+ describe 'openapi' do
5
+ describe 'an minimum openapi' do
6
+ let(:spec) { Spec.new }
7
+
8
+ it 'should return a correct spec' do
9
+ spec.openapi { info 'An OpenAPI', '1.0.0' }
10
+ expect(spec.to_spec).to eq(
11
+ {
12
+ openapi: '3.0.3',
13
+ info: {
14
+ title: 'An OpenAPI',
15
+ version: '1.0.0'
16
+ },
17
+ paths: {}
18
+ }
19
+ )
20
+ end
21
+ end
22
+ end
23
+
24
+ describe 'Info Object' do
25
+ let(:info) { Info.new }
26
+
27
+ describe 'a minimum Info object' do
28
+ before do
29
+ info.version '1.0.0'
30
+ info.title 'An OpenAPI'
31
+ end
32
+
33
+ it 'should return a correct spec' do
34
+ expect(info.to_spec).to eq(
35
+ {
36
+ title: 'An OpenAPI',
37
+ version: '1.0.0'
38
+ }
39
+ )
40
+ end
41
+ end
42
+ end
43
+
44
+ describe 'Contact Object' do
45
+ describe 'a minimum object' do
46
+ let(:contact) { Contact.new }
47
+
48
+ it 'should return a correct spec' do
49
+ expect(contact.to_spec).to eq({})
50
+ end
51
+ end
52
+
53
+ describe 'a fully initialized object' do
54
+ let(:contact) do
55
+ Contact.new 'Foo Bar', url: 'foo.bar', email: 'foo@bar'
56
+ end
57
+
58
+ it 'should return a correct spec' do
59
+ expect(contact.to_spec).to eq(
60
+ { name: 'Foo Bar', url: 'foo.bar', email: 'foo@bar' }
61
+ )
62
+ end
63
+ end
64
+ end
65
+ end
data/lib/tiki.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'optimist'
2
+ require_relative './tiki/spec'
3
+ require_relative './tiki/list-helpers'
4
+
5
+ using ListHelpers
6
+
7
+ def tiki
8
+ opts = Optimist.options do
9
+ opt :indir, 'Input directory', default: 'specs'
10
+ opt :outdir, 'Output directory', type: :string
11
+ opt :ext, 'Extension of the input files', default: 'oas.rb'
12
+ end
13
+
14
+ indir = opts[:indir]
15
+ outdir = opts[:outdir] || indir
16
+ ext = opts[:ext]
17
+
18
+ infiles = Dir.glob File.join(indir, "*.#{ext}")
19
+ puts "Found #{infiles.size} input #{infiles.size == 1 ? 'file' : 'files'}"
20
+ infiles.each do |infile|
21
+ outfile = File.join outdir, "#{File.basename(infile, '.rb')}.json"
22
+ puts "#{infile} -> #{outfile}"
23
+ spec = Spec.new
24
+ spec.instance_eval File.read(infile)
25
+ json = JSON.pretty_generate spec.to_spec
26
+ File.write outfile, json
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiki
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kim Dalsgaard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-11-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: optimist
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A tool for creating and using OpenAPI specs
28
+ email: kim@kimdalsgaard.com
29
+ executables:
30
+ - tiki
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - bin/tiki
36
+ - lib/tiki.rb
37
+ - lib/tiki/components.rb
38
+ - lib/tiki/content.rb
39
+ - lib/tiki/external-documentation.rb
40
+ - lib/tiki/list-helpers.rb
41
+ - lib/tiki/media-type.rb
42
+ - lib/tiki/operation.rb
43
+ - lib/tiki/parameter.rb
44
+ - lib/tiki/path-item.rb
45
+ - lib/tiki/props.rb
46
+ - lib/tiki/reason.rb
47
+ - lib/tiki/reference.rb
48
+ - lib/tiki/request-body.rb
49
+ - lib/tiki/response.rb
50
+ - lib/tiki/response.spec.rb
51
+ - lib/tiki/schema.rb
52
+ - lib/tiki/schema.spec.rb
53
+ - lib/tiki/server.rb
54
+ - lib/tiki/server.spec.rb
55
+ - lib/tiki/spec.rb
56
+ - lib/tiki/spec.spec.rb
57
+ homepage: https://rubygems.org/gems/tiki
58
+ licenses:
59
+ - MIT
60
+ metadata:
61
+ rubygems_mfa_required: 'true'
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.7.0
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 3.3.11
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Tiki
81
+ test_files: []