tomograph 2.5.4 → 3.0.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
  SHA256:
3
- metadata.gz: 07d63f3be0c4b209e6630a5956d7b4e57f473369dce3ae2e4e9c1d888433a9e3
4
- data.tar.gz: e5852bd99d4bce0f4d9e1509f42d05c18d36496eeff1527a5444372f7de697a8
3
+ metadata.gz: c457270d6d9ab0b7d23c4af643248636f3fe9b3f673858700505ab55ee31716f
4
+ data.tar.gz: d089a1f21d1ab2e739854f962eff9e0e7fcdc3cdb631a595f84a328afc016823
5
5
  SHA512:
6
- metadata.gz: e200c218b248783f50c4a60b3f2650441e2247412b11c2085a70c521d6a41c0fafbf00a6fc837740ba773b0dd17d803d575c8e551a2f758dc0b17ce406dcf69a
7
- data.tar.gz: 58f1be2822805c77a721697d518eeb4b8c17d881f3b659a953c8c7908634e51f95401d175516f491b63f117013939e8df215632973ed66977181309c0f1a10b1
6
+ metadata.gz: 4f31296ba8532f93e0f89f881e0c0aa07a3a2a2752d2f6764be9ac6bf0ee5a8e1103b4f5bc2eb9b8ef0472f70432acaec379d2b914338fefed6f8e7c2842f3d3
7
+ data.tar.gz: b54f9a273905b2e7d3bd2d91777f46fc5c626dec43124abf6a3b5fed85a63e3ec63e21568498e6f072ab48020a58e6c833ab89a2ef7bf2692acc184c614cb6d4
@@ -13,10 +13,8 @@ jobs:
13
13
 
14
14
  steps:
15
15
  - uses: actions/checkout@v2
16
- - name: Set up Ruby 2.6
16
+ - name: Set up Ruby
17
17
  uses: actions/setup-ruby@v1
18
- with:
19
- version: 2.6.x
20
18
 
21
19
  - name: Publish to RubyGems
22
20
  run: |
@@ -1,5 +1,12 @@
1
1
  # Change log
2
2
 
3
+ ### 3.0.0 - 2020-08-27
4
+
5
+ * features
6
+ * support multi request for one resource path [#27](https://github.com/funbox/tomograph/issues/27)
7
+ * removed
8
+ * delete support drafter 3 [#29](https://github.com/funbox/tomograph/issues/29)
9
+
3
10
  ### 2.5.4 - 2020-04-06
4
11
 
5
12
  * fixed warnings on ruby 2.7
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # Tomograph [![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)
1
+ # Tomograph
2
2
 
3
- <a href="https://funbox.ru">
4
- <img src="https://funbox.ru/badges/sponsored_by_funbox_compact.svg" alt="Sponsored by FunBox" width=250 />
5
- </a>
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)
6
4
 
7
5
  Convert API Blueprint to JSON Schema and search.
8
6
 
@@ -10,187 +8,203 @@ Convert API Blueprint to JSON Schema and search.
10
8
 
11
9
  First you need to install [drafter](https://github.com/apiaryio/drafter).
12
10
 
13
- Second add this line to your application's Gemfile:
11
+ Then add this line to your application's Gemfile:
14
12
 
15
13
  ```ruby
16
14
  gem 'tomograph'
17
15
  ```
18
16
 
19
- And then execute:
17
+ After that execute:
20
18
 
21
- $ bundle
19
+ ```bash
20
+ $ bundle
21
+ ```
22
22
 
23
- Or install it yourself as:
23
+ Or install the gem by yourself:
24
24
 
25
- $ gem install tomograph
25
+ ```bash
26
+ $ gem install tomograph
27
+ ```
26
28
 
27
29
  ## Usage
28
30
 
29
31
  ```ruby
30
32
  require 'tomograph'
31
- ```
32
33
 
33
- ```ruby
34
34
  tomogram = Tomograph::Tomogram.new(apib_path: '/path/to/doc.apib')
35
35
  ```
36
36
 
37
37
  ### Command line tool
38
38
 
39
- The command line tool allows you to convert files from API Blueprint or API Elements to JSON Schema.
40
- ```
39
+ CLI allows you to convert files from API Blueprint or API Elements to JSON Schema.
40
+
41
+ ```bash
41
42
  tomograph doc.apib doc.json
42
43
  ```
43
- Now there is support for documents pre-parsed by [drafter](https://github.com/apiaryio/drafter) versions 3 and 4, or crafter. To select the handler version, use the -d key and specify the required version. For example:
44
- ```
44
+
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:
47
+
48
+ ```bash
45
49
  tomograph -d 4 doc_by_drafter4.yaml doc.json
46
50
  ```
47
- See
48
- ```
51
+
52
+ Run CLI with `-h` to get detailed help:
53
+
54
+ ```bash
49
55
  tomograph -h
50
56
  ```
51
- for more details on usage.
52
57
 
53
58
  ## Convert
54
59
 
60
+ Use `to_json` for converting APIB to JSON:
61
+
55
62
  ```ruby
56
63
  tomogram.to_json
57
64
  ```
58
65
 
59
- Example input:
60
- ```apib
61
- FORMAT: 1A
62
- HOST: http://test.local
63
-
64
- # project
65
-
66
- # Group project
67
-
68
- Project
69
-
70
- ## Authentication [/sessions]
71
-
72
- ### Sign In [POST]
73
-
74
- + Request (application/json)
75
-
76
- + Attributes
77
- + login (string, required)
78
- + password (string, required)
79
- + captcha (string, optional)
80
-
81
- + Response 401 (application/json)
82
-
83
- + Response 429 (application/json)
84
-
85
- + Response 201 (application/json)
86
-
87
- + Attributes
88
- + confirmation (Confirmation, optional)
89
- + captcha (string, optional)
90
- + captcha_does_not_match (boolean, optional)
91
-
92
-
93
- # Data Structures
94
-
95
- ## Confirmation (object)
96
- + id (string, required)
97
- + type (string, required)
98
- + operation (string, required)
99
- ```
100
-
101
- Example output:
102
- ```json
103
- [
104
- {
105
- "path": "/sessions",
106
- "method": "POST",
107
- "content-type": "application/json",
108
- "request": {
109
- "$schema": "http://json-schema.org/draft-04/schema#",
110
- "type": "object",
111
- "properties": {
112
- "login": {
113
- "type": "string"
66
+ <details>
67
+ <summary>Example input</summary>
68
+
69
+ ```apib
70
+ FORMAT: 1A
71
+ HOST: http://test.local
72
+
73
+ # project
74
+
75
+ # Group project
76
+
77
+ Project
78
+
79
+ ## Authentication [/sessions]
80
+
81
+ ### Sign In [POST]
82
+
83
+ + Request (application/json)
84
+
85
+ + Attributes
86
+ + login (string, required)
87
+ + password (string, required)
88
+ + captcha (string, optional)
89
+
90
+ + Response 401 (application/json)
91
+
92
+ + Response 429 (application/json)
93
+
94
+ + Response 201 (application/json)
95
+
96
+ + Attributes
97
+ + confirmation (Confirmation, optional)
98
+ + captcha (string, optional)
99
+ + captcha_does_not_match (boolean, optional)
100
+
101
+
102
+ # Data Structures
103
+
104
+ ## Confirmation (object)
105
+ + id (string, required)
106
+ + type (string, required)
107
+ + operation (string, required)
108
+ ```
109
+ </details>
110
+
111
+ <details>
112
+ <summary>Example output</summary>
113
+
114
+ ```json
115
+ [
116
+ {
117
+ "path": "/sessions",
118
+ "method": "POST",
119
+ "content-type": "application/json",
120
+ "requests": [{
121
+ "$schema": "http://json-schema.org/draft-04/schema#",
122
+ "type": "object",
123
+ "properties": {
124
+ "login": {
125
+ "type": "string"
126
+ },
127
+ "password": {
128
+ "type": "string"
129
+ },
130
+ "captcha": {
131
+ "type": "string"
132
+ }
114
133
  },
115
- "password": {
116
- "type": "string"
134
+ "required": [
135
+ "login",
136
+ "password"
137
+ ]
138
+ }],
139
+ "responses": [
140
+ {
141
+ "status": "401",
142
+ "content-type": "application/json",
143
+ "body": {}
117
144
  },
118
- "captcha": {
119
- "type": "string"
120
- }
121
- },
122
- "required": [
123
- "login",
124
- "password"
125
- ]
126
- },
127
- "responses": [
128
- {
129
- "status": "401",
130
- "content-type": "application/json",
131
- "body": {}
132
- },
133
- {
134
- "status": "429",
135
- "content-type": "application/json",
136
- "body": {}
137
- },
138
- {
139
- "status": "201",
140
- "content-type": "application/json",
141
- "body": {
142
- "$schema": "http://json-schema.org/draft-04/schema#",
143
- "type": "object",
144
- "properties": {
145
- "confirmation": {
146
- "type": "object",
147
- "properties": {
148
- "id": {
149
- "type": "string"
150
- },
151
- "type": {
152
- "type": "string"
145
+ {
146
+ "status": "429",
147
+ "content-type": "application/json",
148
+ "body": {}
149
+ },
150
+ {
151
+ "status": "201",
152
+ "content-type": "application/json",
153
+ "body": {
154
+ "$schema": "http://json-schema.org/draft-04/schema#",
155
+ "type": "object",
156
+ "properties": {
157
+ "confirmation": {
158
+ "type": "object",
159
+ "properties": {
160
+ "id": {
161
+ "type": "string"
162
+ },
163
+ "type": {
164
+ "type": "string"
165
+ },
166
+ "operation": {
167
+ "type": "string"
168
+ }
153
169
  },
154
- "operation": {
155
- "type": "string"
156
- }
170
+ "required": [
171
+ "id",
172
+ "type",
173
+ "operation"
174
+ ]
175
+ },
176
+ "captcha": {
177
+ "type": "string"
157
178
  },
158
- "required": [
159
- "id",
160
- "type",
161
- "operation"
162
- ]
163
- },
164
- "captcha": {
165
- "type": "string"
166
- },
167
- "captcha_does_not_match": {
168
- "type": "boolean"
179
+ "captcha_does_not_match": {
180
+ "type": "boolean"
181
+ }
169
182
  }
170
183
  }
171
184
  }
172
- }
173
- ]
174
- }
175
- ]
176
- ```
185
+ ]
186
+ }
187
+ ]
188
+ ```
189
+ </details>
177
190
 
178
191
  ## Search
179
192
 
180
- ### find_request
193
+ Use these methods to search through parsed API Blueprint spec to get request => responses hash maps.
194
+
195
+ ### `find_request`
181
196
 
182
197
  ```ruby
183
198
  request = tomogram.find_request(method: 'GET', path: '/status/1?qwe=rty')
184
199
  ```
185
200
 
186
- ### find_request
201
+ ### `find_request_with_content_type`
187
202
 
188
203
  ```ruby
189
204
  request = tomogram.find_request_with_content_type(method: 'GET', path: '/status/1?qwe=rty', content_type: 'application/json')
190
205
  ```
191
206
 
192
- ### find_responses
193
-
207
+ ### `find_responses`
194
208
 
195
209
  ```ruby
196
210
  responses = request.find_responses(status: '200')
@@ -198,68 +212,70 @@ responses = request.find_responses(status: '200')
198
212
 
199
213
  ## Other methods
200
214
 
201
- ### prefix_match?
215
+ ### `prefix_match?`
202
216
 
203
- This can be useful if you use a prefix.
217
+ This may be useful if you specify a prefix.
204
218
 
205
219
  ```ruby
206
220
  tomogram.prefix_match?('http://local/api/v2/users')
207
221
  ```
208
222
 
209
- ## Params
223
+ ### `to_resources`
210
224
 
211
- Example:
225
+ Maps resources with possible requests.
226
+
227
+ Example output:
212
228
 
213
229
  ```ruby
214
- Tomograph::Tomogram.new(prefix: '/api/v2', apib_path: '/path/to/doc.apib')
230
+ {
231
+ '/sessions' => ['POST /sessions']
232
+ }
215
233
  ```
216
234
 
217
- or
235
+ ## Constructor params
236
+
237
+ You can specify API prefix and path to the spec using one of the possible formats:
218
238
 
219
239
  ```ruby
220
- Tomograph::Tomogram.new(prefix: '/api/v2', drafter_yaml_path: '/path/to/doc.yaml')
240
+ Tomograph::Tomogram.new(prefix: '/api/v2', apib_path: '/path/to/doc.apib')
221
241
  ```
222
242
 
223
- or
243
+ ```ruby
244
+ Tomograph::Tomogram.new(prefix: '/api/v2', drafter_yaml_path: '/path/to/doc.yaml')
245
+ ```
224
246
 
225
247
  ```ruby
226
248
  Tomograph::Tomogram.new(prefix: '/api/v2', tomogram_json_path: '/path/to/doc.json')
227
249
  ```
228
250
 
229
- ### apib_path
251
+ ### `apib_path`
230
252
 
231
253
  Path to API Blueprint documentation. There must be an installed [drafter](https://github.com/apiaryio/drafter) to parse it.
232
254
 
233
- ### drafter_yaml_path
255
+ ### `drafter_yaml_path`
234
256
 
235
257
  Path to API Blueprint documentation pre-parsed with `drafter` and saved to a YAML file.
236
258
 
237
- Drafter v.4
259
+ ### Drafter v4 & Crafter support
238
260
 
239
- If you are using a `drafter v.4`, then you need to use the following parameters`drafter_4_apib_path` and `drafter_4_yaml_path` respectively.
261
+ If you are using a `drafter v4`, you should use `drafter_4_apib_path` and `drafter_4_yaml_path` respectively.
240
262
 
241
- Crafter
263
+ In case when you want to use `сrafter`, then you should pass these params: `crafter_apib_path`, `crafter_yaml_path`.
242
264
 
243
- In case when you want to use `сrafter`, then you need to specify these parameters `crafter_apib_path`, `crafter_yaml_path`.
244
-
245
- ### tomogram_json_path
265
+ ### `tomogram_json_path`
246
266
 
247
267
  Path to API Blueprint documentation converted with `tomograph` to a JSON file.
248
268
 
249
- ### prefix
269
+ ### `prefix`
250
270
 
251
- Default empty String. Prefix of API requests. Example: `'/api'`.
271
+ Default: `''`
252
272
 
253
- ### to_resources
273
+ Prefix for API requests.
254
274
 
255
- Hash resource with requests.
256
- The result for the example above:
257
- ```ruby
258
- {
259
- '/sessions' => ['POST /sessions']
260
- }
261
- ```
275
+ Example: `'/api'`.
262
276
 
263
277
  ## License
264
278
 
265
279
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
280
+
281
+ [![Sponsored by FunBox](https://funbox.ru/badges/sponsored_by_funbox_centered.svg)](https://funbox.ru)
@@ -12,7 +12,7 @@ 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.')
15
+ on('-d DRAFTER_VERSION', '--drafter', 'Choose drafter version: crafter or 4. Default: use drafter v.4.')
16
16
  on('-f INPUT_FORMAT', '--format', 'Force input format: "apib" or "yaml". Default: detect by file extension.')
17
17
  on('--exclude-description', 'Exclude "description" keys.')
18
18
  on('--split', 'Split output into files by method.')
@@ -52,12 +52,10 @@ def choose_drafter(opt_parser)
52
52
  case opt_parser
53
53
  when 'crafter'
54
54
  :crafter
55
- when '3'
56
- :drafter_3
57
55
  when '4'
58
56
  :drafter_4
59
57
  when nil
60
- :drafter_3
58
+ :drafter_4
61
59
  else
62
60
  raise 'Unsupported drafter version!'
63
61
  end
@@ -88,17 +86,13 @@ main do |input, output|
88
86
  version = choose_drafter(options['drafter'])
89
87
  format_key = case format
90
88
  when :apib
91
- if version == :drafter_3
92
- :apib_path
93
- elsif version == :crafter
89
+ if version == :crafter
94
90
  :crafter_apib_path
95
91
  else
96
92
  :drafter_4_apib_path
97
93
  end
98
94
  when :yaml
99
- if version == :drafter_3
100
- :drafter_yaml_path
101
- elsif version == :crafter
95
+ if version == :crafter
102
96
  :crafter_yaml_path
103
97
  else
104
98
  :drafter_4_yaml_path
@@ -101,8 +101,8 @@ module Tomograph
101
101
  path: "#{@prefix}#{related_actions.first.path}",
102
102
  method: related_actions.first.method,
103
103
  content_type: related_actions.first.content_type,
104
- request: related_actions.first.request,
105
- responses: related_actions.map(&:responses).flatten,
104
+ requests: related_actions.map(&:request).flatten.uniq,
105
+ responses: related_actions.map(&:responses).flatten.uniq,
106
106
  resource: related_actions.first.resource
107
107
  }
108
108
  end
@@ -101,8 +101,8 @@ module Tomograph
101
101
  path: "#{@prefix}#{related_actions.first.path}",
102
102
  method: related_actions.first.method,
103
103
  content_type: related_actions.first.content_type,
104
- request: related_actions.first.request,
105
- responses: related_actions.map(&:responses).flatten,
104
+ requests: related_actions.map(&:request).flatten.uniq,
105
+ responses: related_actions.map(&:responses).flatten.uniq,
106
106
  resource: related_actions.first.resource
107
107
  }
108
108
  end
@@ -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
1
  require 'multi_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
 
@@ -12,12 +11,10 @@ module Tomograph
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
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
14
  elsif crafter_yaml_path || crafter_apib_path
18
15
  Tomograph::ApiBlueprint::Crafter::Yaml.new(prefix, crafter_apib_path, 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_4_apib_path, drafter_4_yaml_path)
21
18
  end
22
19
  @prefix = prefix
23
20
  end
@@ -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.4'.freeze
2
+ VERSION = '3.0.0'.freeze
3
3
  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.4
4
+ version: 3.0.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: 2020-04-06 00:00:00.000000000 Z
11
+ date: 2020-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: methadone
@@ -88,42 +88,42 @@ dependencies:
88
88
  name: rspec
89
89
  requirement: !ruby/object:Gem::Requirement
90
90
  requirements:
91
- - - ">="
92
- - !ruby/object:Gem::Version
93
- version: 3.9.0
94
91
  - - "~>"
95
92
  - !ruby/object:Gem::Version
96
93
  version: '3.9'
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 3.9.0
97
97
  type: :development
98
98
  prerelease: false
99
99
  version_requirements: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: 3.9.0
104
101
  - - "~>"
105
102
  - !ruby/object:Gem::Version
106
103
  version: '3.9'
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 3.9.0
107
107
  - !ruby/object:Gem::Dependency
108
108
  name: rubocop
109
109
  requirement: !ruby/object:Gem::Requirement
110
110
  requirements:
111
- - - ">="
112
- - !ruby/object:Gem::Version
113
- version: 0.81.0
114
111
  - - "~>"
115
112
  - !ruby/object:Gem::Version
116
113
  version: '0.81'
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 0.81.0
117
117
  type: :development
118
118
  prerelease: false
119
119
  version_requirements: !ruby/object:Gem::Requirement
120
120
  requirements:
121
- - - ">="
122
- - !ruby/object:Gem::Version
123
- version: 0.81.0
124
121
  - - "~>"
125
122
  - !ruby/object:Gem::Version
126
123
  version: '0.81'
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: 0.81.0
127
127
  - !ruby/object:Gem::Dependency
128
128
  name: simplecov
129
129
  requirement: !ruby/object:Gem::Requirement
@@ -170,8 +170,6 @@ files:
170
170
  - lib/tomograph.rb
171
171
  - lib/tomograph/api_blueprint/crafter/yaml.rb
172
172
  - lib/tomograph/api_blueprint/crafter/yaml/action.rb
173
- - lib/tomograph/api_blueprint/drafter_3/yaml.rb
174
- - lib/tomograph/api_blueprint/drafter_3/yaml/action.rb
175
173
  - lib/tomograph/api_blueprint/drafter_4/yaml.rb
176
174
  - lib/tomograph/api_blueprint/drafter_4/yaml/action.rb
177
175
  - lib/tomograph/api_blueprint/json_schema.rb
@@ -200,7 +198,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
200
198
  - !ruby/object:Gem::Version
201
199
  version: '0'
202
200
  requirements: []
203
- rubygems_version: 3.0.3
201
+ rubygems_version: 3.1.2
204
202
  signing_key:
205
203
  specification_version: 4
206
204
  summary: Convert API Blueprint to Tomogram
@@ -1,128 +0,0 @@
1
- require 'yaml'
2
- require 'tomograph/api_blueprint/drafter_3/yaml/action'
3
- require 'tomograph/tomogram/action'
4
-
5
- module Tomograph
6
- module ApiBlueprint
7
- class Drafter3
8
- class Yaml
9
- def initialize(prefix, apib_path, drafter_yaml_path)
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
16
- end
17
-
18
- def groups
19
- @groups ||= @documentation['content'][0]['content'].inject([]) do |result_groups, group|
20
- next result_groups unless group?(group)
21
- result_groups.push(group)
22
- end
23
- end
24
-
25
- def group?(group)
26
- group['element'] != 'copy' && # Element is a human readable text
27
- group['meta']['classes'][0] == 'resourceGroup' # skip Data Structures
28
- end
29
-
30
- def resources
31
- @resources ||= groups.inject([]) do |result_groups, group|
32
- result_groups.push(group['content'].inject([]) do |result_resources, resource|
33
- next result_resources unless resource?(resource)
34
- result_resources.push('resource' => resource, 'resource_path' => resource_path(resource))
35
- end)
36
- end.flatten
37
- end
38
-
39
- def resource?(resource)
40
- resource['element'] != 'copy' # Element is a human readable text
41
- end
42
-
43
- def resource_path(resource)
44
- resource['attributes'] && resource['attributes']['href']
45
- end
46
-
47
- def transitions
48
- @transitions ||= resources.inject([]) do |result_resources, resource|
49
- result_resources.push(resource['resource']['content'].inject([]) do |result_transitions, transition|
50
- next result_transitions unless transition?(transition)
51
- result_transitions.push(transition_hash(transition, resource))
52
- end)
53
- end.flatten
54
- end
55
-
56
- def transition?(transition)
57
- transition['element'] == 'transition'
58
- end
59
-
60
- def transition_hash(transition, resource)
61
- {
62
- 'transition' => transition,
63
- 'transition_path' => transition_path(transition, resource['resource_path']),
64
- 'resource' => resource['resource_path']
65
- }
66
- end
67
-
68
- def transition_path(transition, resource_path)
69
- transition['attributes'] && transition['attributes']['href'] || resource_path
70
- end
71
-
72
- def without_group_actions
73
- transitions.inject([]) do |result_transition, transition|
74
- result_transition.push(transition['transition']['content'].inject([]) do |result_contents, content|
75
- next result_contents unless action?(content)
76
- result_contents.push(Tomograph::ApiBlueprint::Drafter3::Yaml::Action.new(
77
- content['content'],
78
- transition['transition_path'],
79
- transition['resource']
80
- ))
81
- end)
82
- end
83
- end
84
-
85
- def action?(content)
86
- content['element'] == 'httpTransaction'
87
- end
88
-
89
- def actions
90
- @actions ||= without_group_actions
91
- .flatten
92
- .group_by { |action| "#{action.method} #{action.path}" }.map do |_key, related_actions|
93
- action_hash(related_actions)
94
- end.flatten
95
- end
96
-
97
- def action_hash(related_actions)
98
- {
99
- path: "#{@prefix}#{related_actions.first.path}",
100
- method: related_actions.first.method,
101
- content_type: related_actions.first.content_type,
102
- request: related_actions.first.request,
103
- responses: related_actions.map(&:responses).flatten,
104
- resource: related_actions.first.resource
105
- }
106
- end
107
-
108
- def to_tomogram
109
- @tomogram ||= actions.inject([]) do |result, action|
110
- result.push(Tomograph::Tomogram::Action.new(**action))
111
- end
112
- end
113
-
114
- def to_resources
115
- return @to_resources if @to_resources
116
-
117
- @to_resources = actions.group_by { |action| action[:resource] }
118
- @to_resources = @to_resources.inject({}) do |res, related_actions|
119
- requests = related_actions[1].map do |action|
120
- "#{action[:method]} #{action[:path]}"
121
- end
122
- res.merge(related_actions[1].first[:resource] => requests)
123
- end
124
- end
125
- end
126
- end
127
- end
128
- end
@@ -1,65 +0,0 @@
1
- require 'tomograph/tomogram/action'
2
-
3
- module Tomograph
4
- module ApiBlueprint
5
- class Drafter3
6
- class Yaml
7
- class Action
8
- def initialize(content, path, resource)
9
- @content = content
10
- @path = path
11
- @resource = resource
12
- end
13
-
14
- attr_reader :path, :resource
15
-
16
- def method
17
- @method ||= @content.first['attributes']['method']
18
- end
19
-
20
- def content_type
21
- if @content.first['attributes'].has_key?('headers')
22
- @content.first['attributes']['headers']['content'][0]['content']['key']['content'] == 'Content-Type' ?
23
- @content.first['attributes']['headers']['content'][0]['content']['value']['content'] : nil
24
- end
25
- end
26
-
27
- def request
28
- return @request if @request
29
-
30
- request_action = @content.find { |el| el['element'] == 'httpRequest' }
31
- @request = json_schema(request_action['content'])
32
- end
33
-
34
- def json_schema(actions)
35
- schema_node = actions.find do |action|
36
- action && action['element'] == 'asset' && action['attributes']['contentType'] == 'application/schema+json'
37
- end
38
- return {} unless schema_node
39
-
40
- MultiJson.load(schema_node['content'])
41
- rescue MultiJson::ParseError => e
42
- puts "[Tomograph] Error while parsing #{e}. skipping..."
43
- {}
44
- end
45
-
46
- def responses
47
- return @responses if @responses
48
-
49
- @responses = @content.select do |response|
50
- response['element'] == 'httpResponse' && response['attributes']
51
- end
52
- @responses = @responses.map do |response|
53
- {
54
- 'status' => response['attributes']['statusCode'].to_s,
55
- 'body' => json_schema(response['content']),
56
- 'content-type' => response['attributes'].has_key?('headers') ?
57
- response['attributes']['headers']['content'][0]['content']['value']['content'] : nil
58
- }
59
- end
60
- end
61
- end
62
- end
63
- end
64
- end
65
- end