tomograph 2.5.1 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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, 3 or 4. Default: use drafter v.3.')
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. 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,36 +27,14 @@ 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
- when '3'
56
- :drafter_3
57
34
  when '4'
58
35
  :drafter_4
59
36
  when nil
60
- :drafter_3
37
+ :drafter_4
61
38
  else
62
39
  raise 'Unsupported drafter version!'
63
40
  end
@@ -77,35 +54,19 @@ def write_split_json(actions, output)
77
54
  end
78
55
 
79
56
  def write_json(obj, path)
80
- json = MultiJson.dump(obj, pretty: true)
57
+ json = JSON.pretty_generate(obj)
81
58
  File.open(path, 'w') do |file|
82
59
  file.write(json)
83
60
  end
84
61
  end
85
62
 
86
63
  main do |input, output|
87
- format = guess_format(options['format'], input)
88
64
  version = choose_drafter(options['drafter'])
89
- format_key = case format
90
- when :apib
91
- if version == :drafter_3
92
- :apib_path
93
- elsif version == :crafter
94
- :crafter_apib_path
95
- else
96
- :drafter_4_apib_path
97
- end
98
- when :yaml
99
- if version == :drafter_3
100
- :drafter_yaml_path
101
- elsif version == :crafter
102
- :crafter_yaml_path
103
- else
104
- :drafter_4_yaml_path
105
- end
106
- else
107
- fail NotImplementedError
108
- end
65
+ format_key = {
66
+ crafter: :crafter_yaml_path,
67
+ drafter_4: :drafter_yaml_path
68
+ }[version]
69
+
109
70
  tomogram = Tomograph::Tomogram.new(format_key => input)
110
71
  actions = tomogram.to_a.map(&:to_hash)
111
72
 
@@ -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
@@ -29,7 +25,7 @@ module Tomograph
29
25
  def group?(group)
30
26
  return false if group['element'] == 'resource'
31
27
  group['element'] != 'copy' && # Element is a human readable text
32
- group['meta']['classes'][0] == 'resourceGroup' # skip Data Structures
28
+ group['meta']['classes']['content'][0]['content'] == 'resourceGroup' # skip Data Structures
33
29
  end
34
30
 
35
31
  def resources
@@ -101,15 +97,15 @@ module Tomograph
101
97
  path: "#{@prefix}#{related_actions.first.path}",
102
98
  method: related_actions.first.method,
103
99
  content_type: related_actions.first.content_type,
104
- request: related_actions.first.request,
105
- responses: related_actions.map(&:responses).flatten,
100
+ requests: related_actions.map(&:request).flatten.uniq,
101
+ responses: related_actions.map(&:responses).flatten.uniq,
106
102
  resource: related_actions.first.resource
107
103
  }
108
104
  end
109
105
 
110
106
  def to_tomogram
111
107
  @tomogram ||= actions.inject([]) do |result, action|
112
- result.push(Tomograph::Tomogram::Action.new(action))
108
+ result.push(Tomograph::Tomogram::Action.new(**action))
113
109
  end
114
110
  end
115
111
 
@@ -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
@@ -51,7 +51,7 @@ module Tomograph
51
51
  end
52
52
  @responses = @responses.map do |response|
53
53
  {
54
- 'status' => response['attributes']['statusCode']['content'],
54
+ 'status' => response['attributes']['statusCode']['content'].to_s,
55
55
  'body' => json_schema(response['content']),
56
56
  'content-type' => response['attributes'].has_key?('headers') ?
57
57
  response['attributes']['headers']['content'][0]['content']['value']['content'] : nil
@@ -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
@@ -101,15 +97,15 @@ module Tomograph
101
97
  path: "#{@prefix}#{related_actions.first.path}",
102
98
  method: related_actions.first.method,
103
99
  content_type: related_actions.first.content_type,
104
- request: related_actions.first.request,
105
- responses: related_actions.map(&:responses).flatten,
100
+ requests: related_actions.map(&:request).flatten.uniq,
101
+ responses: related_actions.map(&:responses).flatten.uniq,
106
102
  resource: related_actions.first.resource
107
103
  }
108
104
  end
109
105
 
110
106
  def to_tomogram
111
107
  @tomogram ||= actions.inject([]) do |result, action|
112
- result.push(Tomograph::Tomogram::Action.new(action))
108
+ result.push(Tomograph::Tomogram::Action.new(**action))
113
109
  end
114
110
  end
115
111
 
@@ -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
@@ -51,7 +51,7 @@ module Tomograph
51
51
  end
52
52
  @responses = @responses.map do |response|
53
53
  {
54
- 'status' => response['attributes']['statusCode']['content'],
54
+ 'status' => response['attributes']['statusCode']['content'].to_s,
55
55
  'body' => json_schema(response['content']),
56
56
  'content-type' => response['attributes'].has_key?('headers') ?
57
57
  response['attributes']['headers']['content'][0]['content']['value']['content'] : nil
@@ -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
@@ -14,7 +14,7 @@ module Tomograph
14
14
  path: "#{@prefix}#{action['path']}",
15
15
  method: action['method'],
16
16
  content_type: action['content-type'],
17
- request: action['request'],
17
+ requests: action['requests'],
18
18
  responses: action['responses'],
19
19
  resource: action['resource']))
20
20
  end
@@ -1,7 +1,6 @@
1
- require 'multi_json'
1
+ require 'json'
2
2
  require 'tomograph/path'
3
3
  require 'tomograph/api_blueprint/json_schema'
4
- require 'tomograph/api_blueprint/drafter_3/yaml'
5
4
  require 'tomograph/api_blueprint/drafter_4/yaml'
6
5
  require 'tomograph/api_blueprint/crafter/yaml'
7
6
 
@@ -9,15 +8,13 @@ module Tomograph
9
8
  class Tomogram
10
9
  extend Gem::Deprecate
11
10
 
12
- 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)
11
+ def initialize(prefix: '', drafter_yaml_path: nil, tomogram_json_path: nil, crafter_yaml_path: nil)
13
12
  @documentation = if tomogram_json_path
14
13
  Tomograph::ApiBlueprint::JsonSchema.new(prefix, tomogram_json_path)
15
- elsif drafter_4_yaml_path || drafter_4_apib_path
16
- Tomograph::ApiBlueprint::Drafter4::Yaml.new(prefix, drafter_4_apib_path, drafter_4_yaml_path)
17
- elsif crafter_yaml_path || crafter_apib_path
18
- Tomograph::ApiBlueprint::Crafter::Yaml.new(prefix, crafter_apib_path, crafter_yaml_path)
14
+ elsif crafter_yaml_path
15
+ Tomograph::ApiBlueprint::Crafter::Yaml.new(prefix, crafter_yaml_path)
19
16
  else
20
- Tomograph::ApiBlueprint::Drafter3::Yaml.new(prefix, apib_path, drafter_yaml_path)
17
+ Tomograph::ApiBlueprint::Drafter4::Yaml.new(prefix, drafter_yaml_path)
21
18
  end
22
19
  @prefix = prefix
23
20
  end
@@ -32,7 +29,7 @@ module Tomograph
32
29
  deprecate :to_hash, 'to_a with method access', 2018, 8
33
30
 
34
31
  def to_json
35
- MultiJson.dump(to_a.map(&:to_hash), pretty: true)
32
+ JSON.pretty_generate(to_a.map(&:to_hash))
36
33
  end
37
34
 
38
35
  def find_request(method:, path:)
@@ -3,13 +3,13 @@ require 'tomograph/path'
3
3
  module Tomograph
4
4
  class Tomogram
5
5
  class Action
6
- attr_reader :path, :method, :content_type, :request, :responses, :resource
6
+ attr_reader :path, :method, :content_type, :requests, :responses, :resource
7
7
 
8
- def initialize(path:, method:, content_type:, request:, responses:, resource:)
8
+ def initialize(path:, method:, content_type:, requests:, responses:, resource:)
9
9
  @path ||= Tomograph::Path.new(path)
10
10
  @method ||= method
11
11
  @content_type ||= content_type
12
- @request ||= request
12
+ @requests ||= requests
13
13
  @responses ||= responses
14
14
  @resource ||= resource
15
15
  end
@@ -25,7 +25,7 @@ module Tomograph
25
25
  'path' => path,
26
26
  'method' => method,
27
27
  'content-type' => content_type,
28
- 'request' => request,
28
+ 'requests' => requests,
29
29
  'responses' => responses,
30
30
  'resource' => resource
31
31
  }
@@ -1,3 +1,3 @@
1
1
  module Tomograph
2
- VERSION = '2.5.1'.freeze
2
+ VERSION = '3.0.1'.freeze
3
3
  end
@@ -18,12 +18,11 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_runtime_dependency 'methadone', '~> 1.9', '>= 1.9.5'
22
- spec.add_runtime_dependency 'multi_json', '~> 1.11', '>= 1.11.1'
23
- spec.add_development_dependency 'bundler', '~> 1.12'
24
- spec.add_development_dependency 'byebug', '~> 8.2', '>= 8.2.1'
25
- spec.add_development_dependency 'rake', '~> 10.0'
26
- spec.add_development_dependency 'rspec', '~> 3.4', '>= 3.4.0'
27
- spec.add_development_dependency 'rubocop', '~> 0.52.1', '>= 0.52.1'
28
- spec.add_development_dependency 'simplecov', '~> 0.11', '>= 0.11.2'
21
+ spec.add_runtime_dependency 'methadone', '~> 2', '>= 2.0.2'
22
+ spec.add_development_dependency 'byebug', '~> 11.1', '>= 11.1.1'
23
+ spec.add_development_dependency 'rake', '>= 13.0.1'
24
+ spec.add_development_dependency 'rspec', '~> 3.9', '>= 3.9.0'
25
+ spec.add_development_dependency 'rubocop', '~> 0.81', '>= 0.81.0'
26
+ spec.add_development_dependency 'simplecov', '~> 0.18', '>= 0.18.5'
27
+ spec.required_ruby_version = '>= 2.4.0'
29
28
  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: 2.5.1
4
+ version: 3.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - d.efimov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-10 00:00:00.000000000 Z
11
+ date: 2020-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: methadone
@@ -16,148 +16,114 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.9'
19
+ version: '2'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 1.9.5
22
+ version: 2.0.2
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: '1.9'
29
+ version: '2'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 1.9.5
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.11'
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- version: 1.11.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.11'
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- version: 1.11.1
53
- - !ruby/object:Gem::Dependency
54
- name: bundler
55
- requirement: !ruby/object:Gem::Requirement
56
- requirements:
57
- - - "~>"
58
- - !ruby/object:Gem::Version
59
- version: '1.12'
60
- type: :development
61
- prerelease: false
62
- version_requirements: !ruby/object:Gem::Requirement
63
- requirements:
64
- - - "~>"
65
- - !ruby/object:Gem::Version
66
- version: '1.12'
32
+ version: 2.0.2
67
33
  - !ruby/object:Gem::Dependency
68
34
  name: byebug
69
35
  requirement: !ruby/object:Gem::Requirement
70
36
  requirements:
71
37
  - - "~>"
72
38
  - !ruby/object:Gem::Version
73
- version: '8.2'
39
+ version: '11.1'
74
40
  - - ">="
75
41
  - !ruby/object:Gem::Version
76
- version: 8.2.1
42
+ version: 11.1.1
77
43
  type: :development
78
44
  prerelease: false
79
45
  version_requirements: !ruby/object:Gem::Requirement
80
46
  requirements:
81
47
  - - "~>"
82
48
  - !ruby/object:Gem::Version
83
- version: '8.2'
49
+ version: '11.1'
84
50
  - - ">="
85
51
  - !ruby/object:Gem::Version
86
- version: 8.2.1
52
+ version: 11.1.1
87
53
  - !ruby/object:Gem::Dependency
88
54
  name: rake
89
55
  requirement: !ruby/object:Gem::Requirement
90
56
  requirements:
91
- - - "~>"
57
+ - - ">="
92
58
  - !ruby/object:Gem::Version
93
- version: '10.0'
59
+ version: 13.0.1
94
60
  type: :development
95
61
  prerelease: false
96
62
  version_requirements: !ruby/object:Gem::Requirement
97
63
  requirements:
98
- - - "~>"
64
+ - - ">="
99
65
  - !ruby/object:Gem::Version
100
- version: '10.0'
66
+ version: 13.0.1
101
67
  - !ruby/object:Gem::Dependency
102
68
  name: rspec
103
69
  requirement: !ruby/object:Gem::Requirement
104
70
  requirements:
105
71
  - - "~>"
106
72
  - !ruby/object:Gem::Version
107
- version: '3.4'
73
+ version: '3.9'
108
74
  - - ">="
109
75
  - !ruby/object:Gem::Version
110
- version: 3.4.0
76
+ version: 3.9.0
111
77
  type: :development
112
78
  prerelease: false
113
79
  version_requirements: !ruby/object:Gem::Requirement
114
80
  requirements:
115
81
  - - "~>"
116
82
  - !ruby/object:Gem::Version
117
- version: '3.4'
83
+ version: '3.9'
118
84
  - - ">="
119
85
  - !ruby/object:Gem::Version
120
- version: 3.4.0
86
+ version: 3.9.0
121
87
  - !ruby/object:Gem::Dependency
122
88
  name: rubocop
123
89
  requirement: !ruby/object:Gem::Requirement
124
90
  requirements:
125
91
  - - "~>"
126
92
  - !ruby/object:Gem::Version
127
- version: 0.52.1
93
+ version: '0.81'
128
94
  - - ">="
129
95
  - !ruby/object:Gem::Version
130
- version: 0.52.1
96
+ version: 0.81.0
131
97
  type: :development
132
98
  prerelease: false
133
99
  version_requirements: !ruby/object:Gem::Requirement
134
100
  requirements:
135
101
  - - "~>"
136
102
  - !ruby/object:Gem::Version
137
- version: 0.52.1
103
+ version: '0.81'
138
104
  - - ">="
139
105
  - !ruby/object:Gem::Version
140
- version: 0.52.1
106
+ version: 0.81.0
141
107
  - !ruby/object:Gem::Dependency
142
108
  name: simplecov
143
109
  requirement: !ruby/object:Gem::Requirement
144
110
  requirements:
145
111
  - - "~>"
146
112
  - !ruby/object:Gem::Version
147
- version: '0.11'
113
+ version: '0.18'
148
114
  - - ">="
149
115
  - !ruby/object:Gem::Version
150
- version: 0.11.2
116
+ version: 0.18.5
151
117
  type: :development
152
118
  prerelease: false
153
119
  version_requirements: !ruby/object:Gem::Requirement
154
120
  requirements:
155
121
  - - "~>"
156
122
  - !ruby/object:Gem::Version
157
- version: '0.11'
123
+ version: '0.18'
158
124
  - - ">="
159
125
  - !ruby/object:Gem::Version
160
- version: 0.11.2
126
+ version: 0.18.5
161
127
  description: Convert API Blueprint to routes and JSON-Schemas
162
128
  email:
163
129
  - d.efimov@fun-box.ru
@@ -166,6 +132,7 @@ executables:
166
132
  extensions: []
167
133
  extra_rdoc_files: []
168
134
  files:
135
+ - ".github/workflows/ruby.yml"
169
136
  - ".gitignore"
170
137
  - ".rubocop.yml"
171
138
  - ".ruby-version"
@@ -183,8 +150,6 @@ files:
183
150
  - lib/tomograph.rb
184
151
  - lib/tomograph/api_blueprint/crafter/yaml.rb
185
152
  - lib/tomograph/api_blueprint/crafter/yaml/action.rb
186
- - lib/tomograph/api_blueprint/drafter_3/yaml.rb
187
- - lib/tomograph/api_blueprint/drafter_3/yaml/action.rb
188
153
  - lib/tomograph/api_blueprint/drafter_4/yaml.rb
189
154
  - lib/tomograph/api_blueprint/drafter_4/yaml/action.rb
190
155
  - lib/tomograph/api_blueprint/json_schema.rb
@@ -206,15 +171,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
206
171
  requirements:
207
172
  - - ">="
208
173
  - !ruby/object:Gem::Version
209
- version: '0'
174
+ version: 2.4.0
210
175
  required_rubygems_version: !ruby/object:Gem::Requirement
211
176
  requirements:
212
177
  - - ">="
213
178
  - !ruby/object:Gem::Version
214
179
  version: '0'
215
180
  requirements: []
216
- rubyforge_project:
217
- rubygems_version: 2.4.5
181
+ rubygems_version: 3.1.2
218
182
  signing_key:
219
183
  specification_version: 4
220
184
  summary: Convert API Blueprint to Tomogram