tomograph 3.0.0 → 3.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c457270d6d9ab0b7d23c4af643248636f3fe9b3f673858700505ab55ee31716f
4
- data.tar.gz: d089a1f21d1ab2e739854f962eff9e0e7fcdc3cdb631a595f84a328afc016823
3
+ metadata.gz: ade39b28aaf05781e5c1de146d692f960f607136d2853b9a3d74f87907bd39e0
4
+ data.tar.gz: 2045eece41858ee1345f9e13221ca8021806086764565b3c60bd5c5c41435742
5
5
  SHA512:
6
- metadata.gz: 4f31296ba8532f93e0f89f881e0c0aa07a3a2a2752d2f6764be9ac6bf0ee5a8e1103b4f5bc2eb9b8ef0472f70432acaec379d2b914338fefed6f8e7c2842f3d3
7
- data.tar.gz: b54f9a273905b2e7d3bd2d91777f46fc5c626dec43124abf6a3b5fed85a63e3ec63e21568498e6f072ab48020a58e6c833ab89a2ef7bf2692acc184c614cb6d4
6
+ metadata.gz: 48161844767112cc64665c512ad893a2734f069c03d22a7b1c340e9dca52d48ab6a2ae32b6ebe5ef8c5b31612cc1649473273770fc33b3f12c5f03061a54dc33
7
+ data.tar.gz: 509b370fdc936e5e3e11a8e8b4f970bee98855b0d40710fa236e62061c36a681428a4dc39ca099e0d644947f40498a84dc6314f77eb90b68ac8dd21fe9f0d717
@@ -0,0 +1,33 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+
21
+ steps:
22
+ - uses: actions/checkout@v2
23
+ - name: Set up Ruby
24
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
25
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
26
+ uses: ruby/setup-ruby@v1
27
+ # uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0
28
+ with:
29
+ ruby-version: 2.7
30
+ - name: Install dependencies
31
+ run: bundle install
32
+ - name: Run tests
33
+ run: bundle exec rake
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.7.1
1
+ 3.0.2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,32 @@
1
1
  # Change log
2
2
 
3
+ ### 3.1.2 - 2021-09-28
4
+
5
+ * bug fixes
6
+ * fix parser last crafter version
7
+
8
+ ### 3.1.1 - 2021-02-10
9
+
10
+ * setting
11
+ * improve gemspec summary and description
12
+
13
+ ### 3.1.0 - 2021-02-09
14
+
15
+ * features
16
+ * support Swagger/OpenAPI 2.0 [#47](https://github.com/funbox/tomograph/issues/47)
17
+ * support OpenAPI 3.0 [#50](https://github.com/funbox/tomograph/issues/50)
18
+ * removed
19
+ * delete travis [#45](https://github.com/funbox/tomograph/issues/45)
20
+
21
+ ### 3.0.1 - 2020-10-14
22
+
23
+ * removed
24
+ * delete apib_path [#31](https://github.com/funbox/tomograph/issues/31)
25
+ * delete drafter_4_apib_path [#32](https://github.com/funbox/tomograph/issues/32)
26
+ * replace drafter_yaml_path with drafter_4_yaml_path [#33](https://github.com/funbox/tomograph/issues/33)
27
+ * delete crafter_apib_path [#34](https://github.com/funbox/tomograph/issues/34)
28
+ * delete multi_json [#35](https://github.com/funbox/tomograph/issues/35)
29
+
3
30
  ### 3.0.0 - 2020-08-27
4
31
 
5
32
  * features
data/README.md CHANGED
@@ -1,12 +1,86 @@
1
1
  # Tomograph
2
2
 
3
- [![Build Status](https://travis-ci.org/funbox/tomograph.svg?branch=master)](https://travis-ci.org/funbox/tomograph) [![Gem Version](https://badge.fury.io/rb/tomograph.svg)](https://badge.fury.io/rb/tomograph)
3
+ Convert API Blueprint, Swagger and OpenAPI to minimal routes with JSON Schema. For ease of use and creation of new tools.
4
4
 
5
- Convert API Blueprint to JSON Schema and search.
5
+ Will look like
6
6
 
7
- ## Installation
7
+ ```json
8
+ [
9
+ {
10
+ "path": "/sessions",
11
+ "method": "POST",
12
+ "content-type": "application/json",
13
+ "requests": [{
14
+ "$schema": "http://json-schema.org/draft-04/schema#",
15
+ "type": "object",
16
+ "properties": {
17
+ "login": {
18
+ "type": "string"
19
+ },
20
+ "password": {
21
+ "type": "string"
22
+ },
23
+ "captcha": {
24
+ "type": "string"
25
+ }
26
+ },
27
+ "required": [
28
+ "login",
29
+ "password"
30
+ ]
31
+ }],
32
+ "responses": [
33
+ {
34
+ "status": "401",
35
+ "content-type": "application/json",
36
+ "body": {}
37
+ },
38
+ {
39
+ "status": "429",
40
+ "content-type": "application/json",
41
+ "body": {}
42
+ },
43
+ {
44
+ "status": "201",
45
+ "content-type": "application/json",
46
+ "body": {
47
+ "$schema": "http://json-schema.org/draft-04/schema#",
48
+ "type": "object",
49
+ "properties": {
50
+ "confirmation": {
51
+ "type": "object",
52
+ "properties": {
53
+ "id": {
54
+ "type": "string"
55
+ },
56
+ "type": {
57
+ "type": "string"
58
+ },
59
+ "operation": {
60
+ "type": "string"
61
+ }
62
+ },
63
+ "required": [
64
+ "id",
65
+ "type",
66
+ "operation"
67
+ ]
68
+ },
69
+ "captcha": {
70
+ "type": "string"
71
+ },
72
+ "captcha_does_not_match": {
73
+ "type": "boolean"
74
+ }
75
+ }
76
+ }
77
+ }
78
+ ]
79
+ }
80
+ ]
81
+ ```
8
82
 
9
- First you need to install [drafter](https://github.com/apiaryio/drafter).
83
+ ## Installation
10
84
 
11
85
  Then add this line to your application's Gemfile:
12
86
 
@@ -28,36 +102,72 @@ $ gem install tomograph
28
102
 
29
103
  ## Usage
30
104
 
105
+ ### In code
106
+
107
+ #### OpenAPI 2.0
108
+
109
+ Also Swagger
110
+
31
111
  ```ruby
32
112
  require 'tomograph'
33
113
 
34
- tomogram = Tomograph::Tomogram.new(apib_path: '/path/to/doc.apib')
114
+ tomogram = Tomograph::Tomogram.new(openapi2_json_path: '/path/to/doc.json')
35
115
  ```
36
116
 
37
- ### Command line tool
117
+ #### OpenAPI 3.0
38
118
 
39
- CLI allows you to convert files from API Blueprint or API Elements to JSON Schema.
119
+ Also OpenAPI
40
120
 
41
- ```bash
42
- tomograph doc.apib doc.json
121
+ ```ruby
122
+ require 'tomograph'
123
+
124
+ tomogram = Tomograph::Tomogram.new(openapi3_yaml_path: '/path/to/doc.yaml')
43
125
  ```
44
126
 
45
- There is also support for documents pre-parsed by [drafter](https://github.com/apiaryio/drafter) versions 3 and 4, or `crafter`.
46
- To specify the handler version use the `-d` flag:
127
+ #### API Blueprint
128
+
129
+ First you need to install [drafter](https://github.com/apiaryio/drafter).
130
+ Works after conversion from API Blueprint to API Elements (in YAML file) with Drafter.
131
+
132
+ That is, I mean that you first need to do this
47
133
 
48
134
  ```bash
49
- tomograph -d 4 doc_by_drafter4.yaml doc.json
135
+ drafter doc.apib -o doc.yaml
50
136
  ```
51
137
 
52
- Run CLI with `-h` to get detailed help:
138
+ and then
53
139
 
54
- ```bash
55
- tomograph -h
140
+ ```ruby
141
+ require 'tomograph'
142
+
143
+ tomogram = Tomograph::Tomogram.new(drafter_yaml_path: '/path/to/doc.yaml')
56
144
  ```
57
145
 
58
- ## Convert
146
+ #### Tomograph
59
147
 
60
- Use `to_json` for converting APIB to JSON:
148
+ To use additional features of the pre-converted
149
+
150
+ ```ruby
151
+ require 'tomograph'
152
+
153
+ tomogram = Tomograph::Tomogram.new(tomogram_json_path: '/path/to/doc.json')
154
+ ```
155
+
156
+ #### prefix
157
+ Default: `''`
158
+
159
+ You can specify API prefix and path to the spec using one of the possible formats:
160
+
161
+ ```ruby
162
+ Tomograph::Tomogram.new(prefix: '/api/v2', drafter_yaml_path: '/path/to/doc.yaml')
163
+ ```
164
+
165
+ ```ruby
166
+ Tomograph::Tomogram.new(prefix: '/api/v2', tomogram_json_path: '/path/to/doc.json')
167
+ ```
168
+
169
+ #### to_json
170
+ Use `to_json` for converting to JSON, example from API Blueprint:
61
171
 
62
172
  ```ruby
63
173
  tomogram.to_json
@@ -65,7 +175,7 @@ tomogram.to_json
65
175
 
66
176
  <details>
67
177
  <summary>Example input</summary>
68
-
178
+
69
179
  ```apib
70
180
  FORMAT: 1A
71
181
  HOST: http://test.local
@@ -110,7 +220,7 @@ tomogram.to_json
110
220
 
111
221
  <details>
112
222
  <summary>Example output</summary>
113
-
223
+
114
224
  ```json
115
225
  [
116
226
  {
@@ -188,41 +298,35 @@ tomogram.to_json
188
298
  ```
189
299
  </details>
190
300
 
191
- ## Search
192
-
193
- Use these methods to search through parsed API Blueprint spec to get request => responses hash maps.
194
-
195
- ### `find_request`
301
+ #### to_a
302
+ ```ruby
303
+ tomogram.to_a
304
+ ```
196
305
 
306
+ #### find_request
197
307
  ```ruby
198
308
  request = tomogram.find_request(method: 'GET', path: '/status/1?qwe=rty')
199
309
  ```
200
310
 
201
- ### `find_request_with_content_type`
202
-
311
+ #### find_request_with_content_type
203
312
  ```ruby
204
313
  request = tomogram.find_request_with_content_type(method: 'GET', path: '/status/1?qwe=rty', content_type: 'application/json')
205
314
  ```
206
315
 
207
- ### `find_responses`
208
-
316
+ #### `find_responses`
209
317
  ```ruby
210
318
  responses = request.find_responses(status: '200')
211
319
  ```
212
320
 
213
- ## Other methods
214
-
215
- ### `prefix_match?`
216
-
321
+ #### prefix_match?
217
322
  This may be useful if you specify a prefix.
218
323
 
219
324
  ```ruby
220
325
  tomogram.prefix_match?('http://local/api/v2/users')
221
326
  ```
222
327
 
223
- ### `to_resources`
224
-
225
- Maps resources with possible requests.
328
+ #### to_resources
329
+ Maps resources for API Blueprint with possible requests.
226
330
 
227
331
  Example output:
228
332
 
@@ -232,47 +336,48 @@ Example output:
232
336
  }
233
337
  ```
234
338
 
235
- ## Constructor params
236
-
237
- You can specify API prefix and path to the spec using one of the possible formats:
339
+ ### Command line tool
238
340
 
239
- ```ruby
240
- Tomograph::Tomogram.new(prefix: '/api/v2', apib_path: '/path/to/doc.apib')
241
- ```
341
+ CLI allows you to convert files from API Blueprint (API Elements), Swagger and OpenAPI to JSON Schema.
242
342
 
243
- ```ruby
244
- Tomograph::Tomogram.new(prefix: '/api/v2', drafter_yaml_path: '/path/to/doc.yaml')
245
- ```
343
+ Run CLI with `-h` to get detailed help:
246
344
 
247
- ```ruby
248
- Tomograph::Tomogram.new(prefix: '/api/v2', tomogram_json_path: '/path/to/doc.json')
345
+ ```bash
346
+ tomograph -h
249
347
  ```
250
348
 
251
- ### `apib_path`
252
-
253
- Path to API Blueprint documentation. There must be an installed [drafter](https://github.com/apiaryio/drafter) to parse it.
254
-
255
- ### `drafter_yaml_path`
256
-
257
- Path to API Blueprint documentation pre-parsed with `drafter` and saved to a YAML file.
349
+ To specify the handler version use the `-d` flag:
258
350
 
259
- ### Drafter v4 & Crafter support
351
+ #### OpenAPI 2.0
352
+ ```bash
353
+ tomograph -d openapi2 openapi2.json tomogram.json
354
+ ```
260
355
 
261
- If you are using a `drafter v4`, you should use `drafter_4_apib_path` and `drafter_4_yaml_path` respectively.
356
+ #### OpenAPI 3.0
357
+ ```bash
358
+ tomograph -d openapi3 openapi3.yaml doc.json
359
+ ```
262
360
 
263
- In case when you want to use `сrafter`, then you should pass these params: `crafter_apib_path`, `crafter_yaml_path`.
361
+ #### API Blueprint
362
+ ```bash
363
+ tomograph -d 4 apielemetns.yaml doc.json
364
+ ```
264
365
 
265
- ### `tomogram_json_path`
366
+ #### exclude-description
266
367
 
267
- Path to API Blueprint documentation converted with `tomograph` to a JSON file.
368
+ Exclude "description" keys from json-schemas.
268
369
 
269
- ### `prefix`
370
+ ```bash
371
+ tomograph -d 4 apielemetns.yaml doc.json --exclude-description
372
+ ```
270
373
 
271
- Default: `''`
374
+ #### split
272
375
 
273
- Prefix for API requests.
376
+ Split output into files by method. Output in dir path.
274
377
 
275
- Example: `'/api'`.
378
+ ```bash
379
+ tomograph -d 4 --split apielemetns.yaml jsons/
380
+ ```
276
381
 
277
382
  ## License
278
383
 
data/exe/tomograph CHANGED
@@ -12,11 +12,10 @@ include Methadone::CLILogging
12
12
 
13
13
  version Tomograph::VERSION
14
14
  description 'Converts API Blueprint to JSON Schema'
15
- on('-d DRAFTER_VERSION', '--drafter', 'Choose drafter version: crafter or 4. Default: use drafter v.4.')
16
- on('-f INPUT_FORMAT', '--format', 'Force input format: "apib" or "yaml". Default: detect by file extension.')
15
+ on('-d DRAFTER_VERSION', '--drafter', 'Choose drafter version: crafter or 4, or OpenAPI version: openapi2 or openapi3. Default: use drafter v.4.')
17
16
  on('--exclude-description', 'Exclude "description" keys.')
18
17
  on('--split', 'Split output into files by method.')
19
- arg :input, 'path/to/doc.apib (API Blueprint) or path/to/doc.yaml (API Elements)'
18
+ arg :input, 'path/to/doc.yaml (API Elements)'
20
19
  arg :output, 'path/to/doc.json or path/to/dir if --split is used.'
21
20
 
22
21
  def prune!(obj, unwanted_key)
@@ -28,32 +27,16 @@ def prune!(obj, unwanted_key)
28
27
  end
29
28
  end
30
29
 
31
- def guess_format(opt_format, input)
32
- case opt_format && opt_format.downcase
33
- when 'apib'
34
- :apib
35
- when 'yaml'
36
- :yaml
37
- when nil
38
- case File.extname(input).downcase
39
- when '.apib'
40
- :apib
41
- when '.yaml', '.yml'
42
- :yaml
43
- else
44
- fail 'Unsupported input file extension!'
45
- end
46
- else
47
- fail 'Unsupported input format!'
48
- end
49
- end
50
-
51
30
  def choose_drafter(opt_parser)
52
31
  case opt_parser
53
32
  when 'crafter'
54
33
  :crafter
55
34
  when '4'
56
35
  :drafter_4
36
+ when 'openapi2'
37
+ :openapi2
38
+ when 'openapi3'
39
+ :openapi3
57
40
  when nil
58
41
  :drafter_4
59
42
  else
@@ -75,31 +58,21 @@ def write_split_json(actions, output)
75
58
  end
76
59
 
77
60
  def write_json(obj, path)
78
- json = MultiJson.dump(obj, pretty: true)
61
+ json = JSON.pretty_generate(obj)
79
62
  File.open(path, 'w') do |file|
80
63
  file.write(json)
81
64
  end
82
65
  end
83
66
 
84
67
  main do |input, output|
85
- format = guess_format(options['format'], input)
86
68
  version = choose_drafter(options['drafter'])
87
- format_key = case format
88
- when :apib
89
- if version == :crafter
90
- :crafter_apib_path
91
- else
92
- :drafter_4_apib_path
93
- end
94
- when :yaml
95
- if version == :crafter
96
- :crafter_yaml_path
97
- else
98
- :drafter_4_yaml_path
99
- end
100
- else
101
- fail NotImplementedError
102
- end
69
+ format_key = {
70
+ crafter: :crafter_yaml_path,
71
+ drafter_4: :drafter_yaml_path,
72
+ openapi2: :openapi2_json_path,
73
+ openapi3: :openapi3_yaml_path
74
+ }[version]
75
+
103
76
  tomogram = Tomograph::Tomogram.new(format_key => input)
104
77
  actions = tomogram.to_a.map(&:to_hash)
105
78
 
@@ -33,12 +33,12 @@ module Tomograph
33
33
 
34
34
  def json_schema(actions)
35
35
  schema_node = actions.find do |action|
36
- action && action['element'] == 'asset' && action['attributes']['contentType']['content'] == 'application/schema+json'
36
+ action && action.fetch('element', nil) == 'asset' && action.fetch('attributes', {}).fetch('contentType', {}).fetch('content', nil) == 'application/schema+json'
37
37
  end
38
38
  return {} unless schema_node
39
39
 
40
- MultiJson.load(schema_node['content'])
41
- rescue MultiJson::ParseError => e
40
+ JSON.parse(schema_node['content'])
41
+ rescue JSON::ParserError => e
42
42
  puts "[Tomograph] Error while parsing #{e}. skipping..."
43
43
  {}
44
44
  end
@@ -6,13 +6,9 @@ module Tomograph
6
6
  module ApiBlueprint
7
7
  class Crafter
8
8
  class Yaml
9
- def initialize(prefix, apib_path, drafter_yaml_path)
9
+ def initialize(prefix, drafter_yaml_path)
10
10
  @prefix = prefix
11
- @documentation = if apib_path
12
- YAML.safe_load(`drafter #{apib_path}`)
13
- elsif drafter_yaml_path
14
- YAML.safe_load(File.read(drafter_yaml_path))
15
- end
11
+ @documentation = YAML.safe_load(File.read(drafter_yaml_path))
16
12
  end
17
13
 
18
14
  def groups
@@ -37,8 +37,8 @@ module Tomograph
37
37
  end
38
38
  return {} unless schema_node
39
39
 
40
- MultiJson.load(schema_node['content'])
41
- rescue MultiJson::ParseError => e
40
+ JSON.parse(schema_node['content'])
41
+ rescue JSON::ParserError => e
42
42
  puts "[Tomograph] Error while parsing #{e}. skipping..."
43
43
  {}
44
44
  end
@@ -6,13 +6,9 @@ module Tomograph
6
6
  module ApiBlueprint
7
7
  class Drafter4
8
8
  class Yaml
9
- def initialize(prefix, apib_path, drafter_yaml_path)
9
+ def initialize(prefix, drafter_yaml_path)
10
10
  @prefix = prefix
11
- @documentation = if apib_path
12
- YAML.safe_load(`drafter #{apib_path}`)
13
- elsif drafter_yaml_path
14
- YAML.safe_load(File.read(drafter_yaml_path))
15
- end
11
+ @documentation = YAML.safe_load(File.read(drafter_yaml_path))
16
12
  end
17
13
 
18
14
  def groups
@@ -5,7 +5,7 @@ module Tomograph
5
5
  class JsonSchema
6
6
  def initialize(prefix, json_schema_path)
7
7
  @prefix = prefix
8
- @documentation = MultiJson.load(File.read(json_schema_path))
8
+ @documentation = JSON.parse(File.read(json_schema_path))
9
9
  end
10
10
 
11
11
  def to_tomogram
@@ -0,0 +1,91 @@
1
+ require 'tomograph/tomogram/action'
2
+
3
+ module Tomograph
4
+ module OpenApi
5
+ class OpenApi2
6
+ def initialize(prefix, json_schema_path)
7
+ @prefix = prefix
8
+ @documentation = JSON.parse(File.read(json_schema_path))
9
+ end
10
+
11
+ def to_tomogram
12
+ @tomogram ||= @documentation['paths'].inject([]) do |result, action|
13
+ action[1].keys.each do |method|
14
+ result.push(Tomograph::Tomogram::Action.new(
15
+ path: "#{@prefix}#{action[0]}",
16
+ method: method.upcase,
17
+ content_type: '',
18
+ requests: [],
19
+ responses: responses(action[1][method]['responses'], @documentation['definitions']),
20
+ resource: ''))
21
+ end
22
+ result
23
+ end
24
+ end
25
+
26
+ def responses(resp, defi)
27
+ resp.inject([]) do |result, reponse|
28
+ if reponse[1]['schema']
29
+ result.push(
30
+ status: reponse[0],
31
+ body: schema(reponse[1]['schema'], defi),
32
+ 'content-type': ''
33
+ )
34
+ else
35
+ result.push(
36
+ status: reponse[0],
37
+ body: {},
38
+ 'content-type': ''
39
+ )
40
+ end
41
+ end
42
+ end
43
+
44
+ def schema(sche, defi)
45
+ if sche.keys.include?('$ref')
46
+ res = sche.merge('definitions' => {sche["$ref"][14..-1] => defi[sche["$ref"][14..-1]]})
47
+ if defi[sche["$ref"][14..-1]].to_s.include?('$ref')
48
+ keys = defi[sche["$ref"][14..-1]].to_s.split('"').find_all{|word| word.include?('definitions') }
49
+ keys.each do |key|
50
+ res["definitions"].merge!({key[14..-1] => defi[key[14..-1]]})
51
+ end
52
+ end
53
+ res
54
+ else
55
+ if sche.to_s.include?('$ref')
56
+ res = sche.merge('definitions' => {})
57
+ keys = sche.to_s.split('"').find_all{|word| word.include?('definitions') }
58
+ keys.each do |key|
59
+ res["definitions"].merge!({key[14..-1] => defi[key[14..-1]]})
60
+ end
61
+ res
62
+ else
63
+ sche
64
+ end
65
+ end
66
+ end
67
+
68
+ def search_hash(hash, key)
69
+ return hash[key] if hash.assoc(key)
70
+ hash.delete_if{|key, value| value.class != Hash}
71
+ new_hash = Hash.new
72
+ hash.each_value {|values| new_hash.merge!(values)}
73
+ unless new_hash.empty?
74
+ search_hash(new_hash, key)
75
+ end
76
+ end
77
+
78
+ def to_resources
79
+ return @to_resources if @to_resources
80
+
81
+ @to_resources = @documentation.group_by { |action| action['resource'] }
82
+ @to_resources = @to_resources.each_with_object({}) do |(resource, actions), resource_map|
83
+ requests = actions.map do |action|
84
+ "#{action['method']} #{@prefix}#{action['path']}"
85
+ end
86
+ resource_map[resource] = requests
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,118 @@
1
+ require 'tomograph/tomogram/action'
2
+
3
+ module Tomograph
4
+ module OpenApi
5
+ class OpenApi3
6
+ def initialize(prefix, openapi3_yaml_path)
7
+ @prefix = prefix
8
+ @documentation = YAML.load(File.read(openapi3_yaml_path))
9
+ end
10
+
11
+ def to_tomogram
12
+ @tomogram ||= @documentation['paths'].inject([]) do |result, action|
13
+ action[1].keys.each do |method|
14
+ result.push(Tomograph::Tomogram::Action.new(
15
+ path: "#{@prefix}#{action[0]}",
16
+ method: method.upcase,
17
+ content_type: '',
18
+ requests: [],
19
+ responses: responses(action[1][method]['responses'], @documentation['components']['schemas']),
20
+ resource: ''))
21
+ end
22
+ result
23
+ end
24
+ end
25
+
26
+ def responses(resp, defi)
27
+ resp.inject([]) do |result, response|
28
+ if response[1]['content'] == nil
29
+ #TODO 403Forbidden
30
+ result.push(
31
+ status: response[0],
32
+ body: {},
33
+ 'content-type': ''
34
+ )
35
+ elsif response[1]['content'].values[0]['schema']
36
+ result.push(
37
+ status: response[0],
38
+ body: schema(response[1]['content'].values[0]['schema'], defi),
39
+ 'content-type': ''
40
+ )
41
+ else
42
+ result.push(
43
+ status: response[0],
44
+ body: {},
45
+ 'content-type': ''
46
+ )
47
+ end
48
+ end
49
+ end
50
+
51
+ def schema(sche, defi)
52
+ if sche.keys.include?('$ref')
53
+ sche.merge!('components' => {})
54
+ sche['components'].merge!('schemas' => {})
55
+ sche['components']['schemas'].merge!({sche["$ref"][21..-1] => defi[sche["$ref"][21..-1]]})
56
+
57
+ if defi[sche["$ref"][21..-1]].to_s.include?('$ref')
58
+ keys = defi[sche["$ref"][21..-1]].to_s.split('"').find_all{|word| word.include?('#/components/schemas/') }
59
+ keys.each do |key|
60
+ sche['components']['schemas'].merge!({key[21..-1] => defi[key[21..-1]]})
61
+
62
+ if defi[key[21..-1]].to_s.include?('$ref')
63
+ keys2 = defi[key[21..-1]].to_s.split('"').find_all{|word| word.include?('#/components/schemas/') }
64
+ keys2.each do |key2|
65
+ sche['components']['schemas'].merge!({key2[21..-1] => defi[key2[21..-1]]})
66
+
67
+ if defi[key2[21..-1]].to_s.include?('$ref')
68
+ keys3 = defi[key2[21..-1]].to_s.split('"').find_all { |word| word.include?('#/components/schemas/') }.uniq
69
+ keys3.each do |key3|
70
+ sche['components']['schemas'].merge!({ key3[21..-1] => defi[key3[21..-1]] })
71
+ end
72
+ end
73
+
74
+ end
75
+ end
76
+
77
+ end
78
+ end
79
+ sche
80
+
81
+ else
82
+ if sche.to_s.include?('$ref')
83
+ res = sche.merge('definitions' => {})
84
+ keys = sche.to_s.split('"').find_all{|word| word.include?('definitions') }
85
+ keys.each do |key|
86
+ res["definitions"].merge!({key[21..-1] => defi[key[21..-1]]})
87
+ end
88
+ res
89
+ else
90
+ sche
91
+ end
92
+ end
93
+ end
94
+
95
+ def search_hash(hash, key)
96
+ return hash[key] if hash.assoc(key)
97
+ hash.delete_if{|key, value| value.class != Hash}
98
+ new_hash = Hash.new
99
+ hash.each_value {|values| new_hash.merge!(values)}
100
+ unless new_hash.empty?
101
+ search_hash(new_hash, key)
102
+ end
103
+ end
104
+
105
+ def to_resources
106
+ return @to_resources if @to_resources
107
+
108
+ @to_resources = @documentation.group_by { |action| action['resource'] }
109
+ @to_resources = @to_resources.each_with_object({}) do |(resource, actions), resource_map|
110
+ requests = actions.map do |action|
111
+ "#{action['method']} #{@prefix}#{action['path']}"
112
+ end
113
+ resource_map[resource] = requests
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -1,20 +1,26 @@
1
- require 'multi_json'
1
+ require 'json'
2
2
  require 'tomograph/path'
3
3
  require 'tomograph/api_blueprint/json_schema'
4
4
  require 'tomograph/api_blueprint/drafter_4/yaml'
5
5
  require 'tomograph/api_blueprint/crafter/yaml'
6
+ require 'tomograph/openapi/openapi2'
7
+ require 'tomograph/openapi/openapi3'
6
8
 
7
9
  module Tomograph
8
10
  class Tomogram
9
11
  extend Gem::Deprecate
10
12
 
11
- def initialize(prefix: '', apib_path: nil, drafter_yaml_path: nil, tomogram_json_path: nil, drafter_4_apib_path: nil, drafter_4_yaml_path: nil, crafter_apib_path: nil, crafter_yaml_path: nil)
13
+ def initialize(prefix: '', drafter_yaml_path: nil, tomogram_json_path: nil, crafter_yaml_path: nil, openapi2_json_path: nil, openapi3_yaml_path: nil)
12
14
  @documentation = if tomogram_json_path
13
15
  Tomograph::ApiBlueprint::JsonSchema.new(prefix, tomogram_json_path)
14
- elsif crafter_yaml_path || crafter_apib_path
15
- Tomograph::ApiBlueprint::Crafter::Yaml.new(prefix, crafter_apib_path, crafter_yaml_path)
16
+ elsif crafter_yaml_path
17
+ Tomograph::ApiBlueprint::Crafter::Yaml.new(prefix, crafter_yaml_path)
18
+ elsif openapi2_json_path
19
+ Tomograph::OpenApi::OpenApi2.new(prefix, openapi2_json_path)
20
+ elsif openapi3_yaml_path
21
+ Tomograph::OpenApi::OpenApi3.new(prefix, openapi3_yaml_path)
16
22
  else
17
- Tomograph::ApiBlueprint::Drafter4::Yaml.new(prefix, drafter_4_apib_path, drafter_4_yaml_path)
23
+ Tomograph::ApiBlueprint::Drafter4::Yaml.new(prefix, drafter_yaml_path)
18
24
  end
19
25
  @prefix = prefix
20
26
  end
@@ -23,13 +29,8 @@ module Tomograph
23
29
  @actions ||= @documentation.to_tomogram
24
30
  end
25
31
 
26
- def to_hash
27
- to_a.map(&:to_hash)
28
- end
29
- deprecate :to_hash, 'to_a with method access', 2018, 8
30
-
31
32
  def to_json
32
- MultiJson.dump(to_a.map(&:to_hash), pretty: true)
33
+ JSON.pretty_generate(to_a.map(&:to_hash))
33
34
  end
34
35
 
35
36
  def find_request(method:, path:)
@@ -1,3 +1,3 @@
1
1
  module Tomograph
2
- VERSION = '3.0.0'.freeze
2
+ VERSION = '3.1.2'.freeze
3
3
  end
data/tomograph.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ['d.efimov']
9
9
  spec.email = ['d.efimov@fun-box.ru']
10
10
 
11
- spec.summary = 'Convert API Blueprint to Tomogram'
12
- spec.description = 'Convert API Blueprint to routes and JSON-Schemas'
11
+ spec.summary = 'Convert API Blueprint, Swagger and OpenAPI to Tomogram'
12
+ spec.description = 'Convert API Blueprint, Swagger and OpenAPI to routes and JSON-Schemas'
13
13
  spec.homepage = 'https://github.com/funbox/tomograph'
14
14
  spec.license = 'MIT'
15
15
 
@@ -19,11 +19,11 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_runtime_dependency 'methadone', '~> 2', '>= 2.0.2'
22
- spec.add_runtime_dependency 'multi_json', '~> 1.14', '>= 1.14.1'
23
22
  spec.add_development_dependency 'byebug', '~> 11.1', '>= 11.1.1'
24
23
  spec.add_development_dependency 'rake', '>= 13.0.1'
25
- spec.add_development_dependency 'rspec', '~> 3.9', '>= 3.9.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.10', '>= 3.10.0'
26
25
  spec.add_development_dependency 'rubocop', '~> 0.81', '>= 0.81.0'
27
- spec.add_development_dependency 'simplecov', '~> 0.18', '>= 0.18.5'
26
+ spec.add_development_dependency 'simplecov', '~> 0.21', '>= 0.21.2'
27
+ spec.add_development_dependency 'json-schema', '~> 2.8', '>= 2.8.1'
28
28
  spec.required_ruby_version = '>= 2.4.0'
29
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tomograph
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - d.efimov
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-27 00:00:00.000000000 Z
11
+ date: 2021-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: methadone
@@ -30,26 +30,6 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 2.0.2
33
- - !ruby/object:Gem::Dependency
34
- name: multi_json
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '1.14'
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- version: 1.14.1
43
- type: :runtime
44
- prerelease: false
45
- version_requirements: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - "~>"
48
- - !ruby/object:Gem::Version
49
- version: '1.14'
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- version: 1.14.1
53
33
  - !ruby/object:Gem::Dependency
54
34
  name: byebug
55
35
  requirement: !ruby/object:Gem::Requirement
@@ -90,20 +70,20 @@ dependencies:
90
70
  requirements:
91
71
  - - "~>"
92
72
  - !ruby/object:Gem::Version
93
- version: '3.9'
73
+ version: '3.10'
94
74
  - - ">="
95
75
  - !ruby/object:Gem::Version
96
- version: 3.9.0
76
+ version: 3.10.0
97
77
  type: :development
98
78
  prerelease: false
99
79
  version_requirements: !ruby/object:Gem::Requirement
100
80
  requirements:
101
81
  - - "~>"
102
82
  - !ruby/object:Gem::Version
103
- version: '3.9'
83
+ version: '3.10'
104
84
  - - ">="
105
85
  - !ruby/object:Gem::Version
106
- version: 3.9.0
86
+ version: 3.10.0
107
87
  - !ruby/object:Gem::Dependency
108
88
  name: rubocop
109
89
  requirement: !ruby/object:Gem::Requirement
@@ -130,21 +110,41 @@ dependencies:
130
110
  requirements:
131
111
  - - "~>"
132
112
  - !ruby/object:Gem::Version
133
- version: '0.18'
113
+ version: '0.21'
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 0.21.2
117
+ type: :development
118
+ prerelease: false
119
+ version_requirements: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '0.21'
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: 0.21.2
127
+ - !ruby/object:Gem::Dependency
128
+ name: json-schema
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - "~>"
132
+ - !ruby/object:Gem::Version
133
+ version: '2.8'
134
134
  - - ">="
135
135
  - !ruby/object:Gem::Version
136
- version: 0.18.5
136
+ version: 2.8.1
137
137
  type: :development
138
138
  prerelease: false
139
139
  version_requirements: !ruby/object:Gem::Requirement
140
140
  requirements:
141
141
  - - "~>"
142
142
  - !ruby/object:Gem::Version
143
- version: '0.18'
143
+ version: '2.8'
144
144
  - - ">="
145
145
  - !ruby/object:Gem::Version
146
- version: 0.18.5
147
- description: Convert API Blueprint to routes and JSON-Schemas
146
+ version: 2.8.1
147
+ description: Convert API Blueprint, Swagger and OpenAPI to routes and JSON-Schemas
148
148
  email:
149
149
  - d.efimov@fun-box.ru
150
150
  executables:
@@ -152,12 +152,11 @@ executables:
152
152
  extensions: []
153
153
  extra_rdoc_files: []
154
154
  files:
155
- - ".github/workflows/gempush.yml"
155
+ - ".github/workflows/ruby.yml"
156
156
  - ".gitignore"
157
157
  - ".rubocop.yml"
158
158
  - ".ruby-version"
159
159
  - ".tool-versions"
160
- - ".travis.yml"
161
160
  - CHANGELOG.md
162
161
  - CODE_OF_CONDUCT.md
163
162
  - Gemfile
@@ -173,6 +172,8 @@ files:
173
172
  - lib/tomograph/api_blueprint/drafter_4/yaml.rb
174
173
  - lib/tomograph/api_blueprint/drafter_4/yaml/action.rb
175
174
  - lib/tomograph/api_blueprint/json_schema.rb
175
+ - lib/tomograph/openapi/openapi2.rb
176
+ - lib/tomograph/openapi/openapi3.rb
176
177
  - lib/tomograph/path.rb
177
178
  - lib/tomograph/tomogram.rb
178
179
  - lib/tomograph/tomogram/action.rb
@@ -183,7 +184,7 @@ homepage: https://github.com/funbox/tomograph
183
184
  licenses:
184
185
  - MIT
185
186
  metadata: {}
186
- post_install_message:
187
+ post_install_message:
187
188
  rdoc_options: []
188
189
  require_paths:
189
190
  - lib
@@ -199,7 +200,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
199
200
  version: '0'
200
201
  requirements: []
201
202
  rubygems_version: 3.1.2
202
- signing_key:
203
+ signing_key:
203
204
  specification_version: 4
204
- summary: Convert API Blueprint to Tomogram
205
+ summary: Convert API Blueprint, Swagger and OpenAPI to Tomogram
205
206
  test_files: []
@@ -1,28 +0,0 @@
1
- name: Ruby Gem
2
-
3
- on:
4
- push:
5
- branches: [ master ]
6
- pull_request:
7
- branches: [ master ]
8
-
9
- jobs:
10
- build:
11
- name: Build + Publish
12
- runs-on: ubuntu-latest
13
-
14
- steps:
15
- - uses: actions/checkout@v2
16
- - name: Set up Ruby
17
- uses: actions/setup-ruby@v1
18
-
19
- - name: Publish to RubyGems
20
- run: |
21
- mkdir -p $HOME/.gem
22
- touch $HOME/.gem/credentials
23
- chmod 0600 $HOME/.gem/credentials
24
- printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
25
- gem build *.gemspec
26
- gem push *.gem
27
- env:
28
- GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.4
4
- - 2.5
5
- - 2.6
6
- - 2.7