lib/tomograph/tomogram.rb in tomograph-0.4.0 vs lib/tomograph/tomogram.rb in tomograph-1.0.0
- old
+ new
@@ -1,140 +1,28 @@
require 'multi_json'
+require 'tomograph/path'
+require 'tomograph/api_blueprint/yaml'
module Tomograph
- class Tomogram < String
- class << self
- def json
- single_sharps = find_resource
+ class Tomogram
+ def initialize(prefix: '', apib_path: nil, drafter_yaml_path: nil)
+ @documentation = Tomograph::ApiBlueprint::Yaml.new(prefix, apib_path, drafter_yaml_path)
+ @prefix = prefix
+ end
- result = []
- single_sharps.map do |single_sharp|
- result += single_sharp['content'].inject([]) do |result, resource|
- next result if text_node?(resource)
+ def to_hash
+ @documentation.to_tomogram.map(&:to_hash)
+ end
- result.concat(extract_actions(resource))
- end
- end
- MultiJson.dump(result)
- end
+ def to_json
+ MultiJson.dump(to_hash)
+ end
- def delete_query_and_last_slash(path)
- path = delete_till_the_end(path, '{&')
- path = delete_till_the_end(path, '{?')
+ def find_request(method:, path:)
+ path = Tomograph::Path.new(path).to_s
- remove_the_slash_at_the_end(path)
- end
-
- private
-
- def extract_actions(resource)
- actions = []
- resource_path = resource['attributes'] && resource['attributes']['href']
-
- transitions = resource['content']
- transitions.each do |transition|
- next unless transition['element'] == 'transition'
-
- path = transition['attributes'] && transition['attributes']['href'] || resource_path
-
- transition['content'].each do |content|
- next unless content['element'] == 'httpTransaction'
-
- action = build_action(content, path)
- actions << action if action
- end
- end
-
- actions.group_by { |action| action['method'] + action['path'] }.map do |_key, resource_actions|
- # because in yaml a response has a copy of the same request we can only use the first
- {
- 'path' => resource_actions.first['path'],
- 'method' => resource_actions.first['method'],
- 'request' => resource_actions.first['request'],
- 'responses' => resource_actions.flat_map { |action| action['responses'] }.compact
- }
- end
- end
-
- def find_resource
- documentation['content'][0]['content'].find_all do |resource|
- !text_node?(resource) &&
- resource['meta']['classes'][0] == 'resourceGroup' # skip Data Structures
- end
- end
-
- def documentation
- if Tomograph.configuration.drafter_yaml
- YAML.load(Tomograph.configuration.drafter_yaml)
- else
- YAML.load(File.read("#{Rails.root}/#{Tomograph.configuration.documentation}"))
- end
- end
-
- def delete_till_the_end(path, beginning_with)
- path_index = path.index(beginning_with)
- path_index ||= 0
- path_index -= 1
- path.slice(0..path_index)
- end
-
- def remove_the_slash_at_the_end(path)
- if path[-1] == '/'
- path[0..-2]
- else
- path
- end
- end
-
- def build_action(content, path)
- return if text_node?(content)
-
- action_to_hash(content['content'], path)
- end
-
- def action_to_hash(actions, path)
- {
- 'path' => "#{Tomograph.configuration.prefix}#{delete_query_and_last_slash(path)}",
- 'method' => actions.first['attributes']['method'],
- 'request' => request(actions),
- 'responses' => responses(actions)
- }
- end
-
- def request(actions)
- request_action = actions.find {|el| el['element'] === 'httpRequest' }
- json_schema(request_action['content'])
- end
-
- def json_schema?(action)
- action && action['element'] == 'asset' && action['attributes']['contentType'] == 'application/schema+json'
- end
-
- def responses(actions)
- response_actions = actions.select {|el| el['element'] === 'httpResponse' }
-
- response_actions.map do |response|
- return unless response['attributes'] # if no response
-
- {
- 'status' => response['attributes']['statusCode'],
- 'body' => json_schema(response['content'])
- }
- end.compact
- end
-
- def json_schema(actions)
- schema_node = actions.find {|action| json_schema?(action) }
- return {} unless schema_node
-
- MultiJson.load(schema_node['content'])
- rescue MultiJson::ParseError => e
- puts "[Tomograph] Error while parsing #{ e }. skipping..."
- {}
- end
-
- def text_node?(node)
- node['element'] == 'copy' # Element is a human readable text
+ @documentation.to_tomogram.find do |action|
+ action.method == method && action.path.match(path)
end
end
end
end