tomograph 2.5.1 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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'],
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