tomograph 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f6bd7ef6a03950f113a0492c08e4d9aa17f26eb
4
- data.tar.gz: bd068832137f783a9d50b5d42b73824f7355f0a2
3
+ metadata.gz: 9b621ab5616dfc37ca938313d8b47e53ef40ca37
4
+ data.tar.gz: 848d61604096bddf13e6248d4adbe5eeeab9f205
5
5
  SHA512:
6
- metadata.gz: 580304b7f2e90c5ed4f34e016549277025fef310ddb97deb758787e73376243da3e96fa58abd4d48adabcdb4751c4facaf47c02ccc2d97f5050ca2718fd6dd9a
7
- data.tar.gz: fef696a261c995d5fa8c44cc8b553742271a99486738a8a70bca989f3b95341cf4b4f71fe3ed81ebe16dfb6d90f01eacba7b3fe7d840391729cfc7b6575646fc
6
+ metadata.gz: d01849bf00acd6939d1a2681083d22d846eed46cf2fca03948cc30de8bf6c83f7eec7eba0f4abba331df4c6abf37772bba6c1a0658243a5661b5edcab07b658a
7
+ data.tar.gz: e9c176086865d22445808b337111dd80a831418dfdb555a6535cd5191bf46898586cb51bd5187e089fff896af9da34411b4045be799b8d6f450dabe7557f3d47
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  .idea/
2
+ /pkg/
2
3
  .byebug_history
3
4
  Gemfile.lock
4
5
  coverage
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Change log
2
2
 
3
+ ### 2.2.0 - 2018-07-05
4
+
5
+ * API changes
6
+ * Tomogram: add to_a method
7
+ * deprecations
8
+ * Tomogram: to_hash method
9
+ * features
10
+ * add command-line tool
11
+ * include Action's "resource" in serialization to JSON
12
+ * add support for loading Tomogram JSON
13
+
3
14
  ### 2.1.0 - 2018-03-16
4
15
 
5
16
  * features
data/README.md CHANGED
@@ -35,6 +35,18 @@ require 'tomograph'
35
35
  tomogram = Tomograph::Tomogram.new(apib_path: '/path/to/doc.apib')
36
36
  ```
37
37
 
38
+ ### Command line tool
39
+
40
+ The command line tool allows you to convert files from API Blueprint or API Elements to JSON Schema.
41
+ ```
42
+ tomograph doc.apib doc.json
43
+ ```
44
+ See
45
+ ```
46
+ tomograph -h
47
+ ```
48
+ for details on usage.
49
+
38
50
  ## Convert
39
51
 
40
52
  ```ruby
@@ -205,6 +217,12 @@ or
205
217
  Tomograph::Tomogram.new(prefix: '/api/v2', drafter_yaml_path: '/path/to/doc.yaml')
206
218
  ```
207
219
 
220
+ or
221
+
222
+ ```ruby
223
+ Tomograph::Tomogram.new(prefix: '/api/v2', tomogram_json_path: '/path/to/doc.json')
224
+ ```
225
+
208
226
  ### apib_path
209
227
 
210
228
  Path to API Blueprint documentation. There must be an installed [drafter](https://github.com/apiaryio/drafter) to parse it.
@@ -213,6 +231,10 @@ Path to API Blueprint documentation. There must be an installed [drafter](https:
213
231
 
214
232
  Path to API Blueprint documentation pre-parsed with `drafter` and saved to a YAML file.
215
233
 
234
+ ### tomogram_json_path
235
+
236
+ Path to API Blueprint documentation converted with `tomograph` to a JSON file.
237
+
216
238
  ### prefix
217
239
 
218
240
  Default empty String. Prefix of API requests. Example: `'/api'`.
data/exe/tomograph ADDED
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'methadone'
4
+
5
+ require 'tomograph'
6
+ require 'tomograph/version'
7
+
8
+
9
+ include Methadone::Main
10
+ include Methadone::CLILogging
11
+
12
+ version Tomograph::VERSION
13
+ description 'Converts API Blueprint to JSON Schema'
14
+ on('-f INPUT_FORMAT', '--format', 'Force input format: "apib" or "yaml". Default: detect by file extension.')
15
+ arg :input, 'path/to/doc.apib (API Blueprint) or path/to/doc.yaml (API Elements)'
16
+ arg :output, 'path/to/doc.json'
17
+
18
+ main do |input, output|
19
+ opt_format = options['format']
20
+ format = case opt_format && opt_format.downcase
21
+ when 'apib'
22
+ :apib
23
+ when 'yaml'
24
+ :yaml
25
+ when nil
26
+ case File.extname(input).downcase
27
+ when '.apib'
28
+ :apib
29
+ when '.yaml'
30
+ :yaml
31
+ else
32
+ fail 'Unsupported input file extension!'
33
+ end
34
+ else
35
+ fail 'Unsupported input format!'
36
+ end
37
+
38
+ format_key = case format
39
+ when :apib
40
+ :apib_path
41
+ when :yaml
42
+ :drafter_yaml_path
43
+ else
44
+ fail 'Unimplemented!'
45
+ end
46
+ tomogram = Tomograph::Tomogram.new({format_key => input})
47
+ File.open(output, 'w') do |f|
48
+ f.write(tomogram.to_json)
49
+ end
50
+ 0
51
+ end
52
+
53
+ go!
@@ -0,0 +1,36 @@
1
+ require 'tomograph/tomogram/action'
2
+
3
+ module Tomograph
4
+ module ApiBlueprint
5
+ class JsonSchema
6
+ def initialize(prefix, json_schema_path)
7
+ @prefix = prefix
8
+ @documentation = MultiJson.load(File.read(json_schema_path))
9
+ end
10
+
11
+ def to_tomogram
12
+ @tomogram ||= @documentation.inject([]) do |result, action|
13
+ result.push(Tomograph::Tomogram::Action.new(
14
+ path: "#{@prefix}#{action['path']}",
15
+ method: action['method'],
16
+ content_type: action['content-type'],
17
+ request: action['request'],
18
+ responses: action['responses'],
19
+ resource: action['resource']))
20
+ end
21
+ end
22
+
23
+ def to_resources
24
+ return @to_resources if @to_resources
25
+
26
+ @to_resources = @documentation.group_by { |action| action['resource'] }
27
+ @to_resources = @to_resources.each_with_object({}) do |(resource, actions), resource_map|
28
+ requests = actions.map do |action|
29
+ "#{action['method']} #{action['path']}"
30
+ end
31
+ resource_map[resource] = requests
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -9,7 +9,7 @@ module Tomograph
9
9
  @prefix = prefix
10
10
  @documentation = if apib_path
11
11
  YAML.safe_load(`drafter #{apib_path}`)
12
- else
12
+ elsif drafter_yaml_path
13
13
  YAML.safe_load(File.read(drafter_yaml_path))
14
14
  end
15
15
  end
@@ -1,26 +1,38 @@
1
1
  require 'multi_json'
2
2
  require 'tomograph/path'
3
+ require 'tomograph/api_blueprint/json_schema'
3
4
  require 'tomograph/api_blueprint/yaml'
4
5
 
5
6
  module Tomograph
6
7
  class Tomogram
7
- def initialize(prefix: '', apib_path: nil, drafter_yaml_path: nil)
8
- @documentation = Tomograph::ApiBlueprint::Yaml.new(prefix, apib_path, drafter_yaml_path)
8
+ extend Gem::Deprecate
9
+
10
+ def initialize(prefix: '', apib_path: nil, drafter_yaml_path: nil, tomogram_json_path: nil)
11
+ @documentation = if tomogram_json_path
12
+ Tomograph::ApiBlueprint::JsonSchema.new(prefix, tomogram_json_path)
13
+ else
14
+ Tomograph::ApiBlueprint::Yaml.new(prefix, apib_path, drafter_yaml_path)
15
+ end
9
16
  @prefix = prefix
10
17
  end
11
18
 
19
+ def to_a
20
+ @actions ||= @documentation.to_tomogram
21
+ end
22
+
12
23
  def to_hash
13
- @documentation.to_tomogram.map(&:to_hash)
24
+ to_a.map(&:to_hash)
14
25
  end
26
+ deprecate :to_hash, 'to_a with method access', 2018, 8
15
27
 
16
28
  def to_json
17
- MultiJson.dump(to_hash)
29
+ MultiJson.dump(to_a.map(&:to_hash), pretty: true)
18
30
  end
19
31
 
20
32
  def find_request(method:, path:)
21
33
  path = Tomograph::Path.new(path).to_s
22
34
 
23
- @documentation.to_tomogram.find do |action|
35
+ to_a.find do |action|
24
36
  action.method == method && action.path.match(path)
25
37
  end
26
38
  end
@@ -28,13 +40,13 @@ module Tomograph
28
40
  def find_request_with_content_type(method:, path:, content_type:)
29
41
  path = Tomograph::Path.new(path).to_s
30
42
 
31
- @documentation.to_tomogram.find do |action|
43
+ to_a.find do |action|
32
44
  action.method == method && action.path.match(path) && action.content_type == content_type
33
45
  end
34
46
  end
35
47
 
36
48
  def to_resources
37
- @documentation.to_resources
49
+ @resources ||= @documentation.to_resources
38
50
  end
39
51
 
40
52
  def prefix_match?(raw_path)
@@ -3,7 +3,7 @@ require 'tomograph/path'
3
3
  module Tomograph
4
4
  class Tomogram
5
5
  class Action
6
- attr_reader :path, :method, :content_type, :request, :responses
6
+ attr_reader :path, :method, :content_type, :request, :responses, :resource
7
7
 
8
8
  def initialize(path:, method:, content_type:, request:, responses:, resource:)
9
9
  @path ||= Tomograph::Path.new(path)
@@ -24,9 +24,10 @@ module Tomograph
24
24
  @action ||= {
25
25
  'path' => path,
26
26
  'method' => method,
27
- 'content-type': content_type,
27
+ 'content-type' => content_type,
28
28
  'request' => request,
29
- 'responses' => responses
29
+ 'responses' => responses,
30
+ 'resource' => resource
30
31
  }
31
32
  end
32
33
  end
@@ -1,3 +1,3 @@
1
1
  module Tomograph
2
- VERSION = '2.1.0'.freeze
2
+ VERSION = '2.2.0'.freeze
3
3
  end
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ def system!(*args)
4
+ puts "+ #{args.join(' ')}"
5
+ system(*args) || abort("\n== Command #{args} failed ==")
6
+ end
7
+
8
+ Dir.glob('spec/fixtures/*.yaml').each do |path|
9
+ path.delete_suffix!('.yaml')
10
+ system! "tomograph \"#{path}.yaml\" \"#{path}.json\""
11
+ end
data/tomograph.gemspec CHANGED
@@ -8,7 +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 JSON Schema and search'
11
+ spec.summary = 'Convert API Blueprint to Tomogram'
12
+ spec.description = 'Convert API Blueprint to routes and JSON-Schemas'
12
13
  spec.homepage = 'https://github.com/funbox/tomograph'
13
14
  spec.license = 'MIT'
14
15
 
@@ -17,6 +18,7 @@ Gem::Specification.new do |spec|
17
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
19
  spec.require_paths = ['lib']
19
20
 
21
+ spec.add_runtime_dependency 'methadone', '~> 1.9', '>= 1.9.5'
20
22
  spec.add_runtime_dependency 'multi_json', '~> 1.11', '>= 1.11.1'
21
23
  spec.add_development_dependency 'bundler', '~> 1.12'
22
24
  spec.add_development_dependency 'byebug', '~> 8.2', '>= 8.2.1'
metadata CHANGED
@@ -1,15 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tomograph
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - d.efimov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-16 00:00:00.000000000 Z
11
+ date: 2018-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: methadone
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.9.5
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.9'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.9.5
13
33
  - !ruby/object:Gem::Dependency
14
34
  name: multi_json
15
35
  requirement: !ruby/object:Gem::Requirement
@@ -138,10 +158,11 @@ dependencies:
138
158
  - - ">="
139
159
  - !ruby/object:Gem::Version
140
160
  version: 0.11.2
141
- description:
161
+ description: Convert API Blueprint to routes and JSON-Schemas
142
162
  email:
143
163
  - d.efimov@fun-box.ru
144
- executables: []
164
+ executables:
165
+ - tomograph
145
166
  extensions: []
146
167
  extra_rdoc_files: []
147
168
  files:
@@ -156,13 +177,16 @@ files:
156
177
  - Rakefile
157
178
  - bin/console
158
179
  - bin/setup
180
+ - exe/tomograph
159
181
  - lib/tomograph.rb
182
+ - lib/tomograph/api_blueprint/json_schema.rb
160
183
  - lib/tomograph/api_blueprint/yaml.rb
161
184
  - lib/tomograph/api_blueprint/yaml/action.rb
162
185
  - lib/tomograph/path.rb
163
186
  - lib/tomograph/tomogram.rb
164
187
  - lib/tomograph/tomogram/action.rb
165
188
  - lib/tomograph/version.rb
189
+ - script/update_json_fixtures
166
190
  - tomograph.gemspec
167
191
  homepage: https://github.com/funbox/tomograph
168
192
  licenses:
@@ -187,5 +211,5 @@ rubyforge_project:
187
211
  rubygems_version: 2.4.5
188
212
  signing_key:
189
213
  specification_version: 4
190
- summary: Convert API Blueprint to JSON Schema and search
214
+ summary: Convert API Blueprint to Tomogram
191
215
  test_files: []