Class: Tomograph::OpenApi::OpenApi2
- Inherits:
-
Object
- Object
- Tomograph::OpenApi::OpenApi2
- Defined in:
- lib/tomograph/openapi/openapi2.rb
Instance Method Summary collapse
-
#initialize(prefix, json_schema_path) ⇒ OpenApi2
constructor
A new instance of OpenApi2.
- #responses(resp, defi) ⇒ Object
- #schema(sche, defi) ⇒ Object
- #search_hash(hash, key) ⇒ Object
- #to_resources ⇒ Object
- #to_tomogram ⇒ Object
Constructor Details
#initialize(prefix, json_schema_path) ⇒ OpenApi2
Returns a new instance of OpenApi2.
6 7 8 9 |
# File 'lib/tomograph/openapi/openapi2.rb', line 6 def initialize(prefix, json_schema_path) @prefix = prefix @documentation = JSON.parse(File.read(json_schema_path)) end |
Instance Method Details
#responses(resp, defi) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/tomograph/openapi/openapi2.rb', line 27 def responses(resp, defi) resp.inject([]) do |result, reponse| if reponse[1]['schema'] result.push( 'status' => reponse[0], 'body' => schema(reponse[1]['schema'], defi), 'content-type' => 'application/json' ) else result.push( 'status' => reponse[0], 'body' => {}, 'content-type' => 'application/json' ) end end end |
#schema(sche, defi) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/tomograph/openapi/openapi2.rb', line 45 def schema(sche, defi) if sche.keys.include?('$ref') res = sche.merge('definitions' => { sche['$ref'][14..-1] => defi[sche['$ref'][14..-1]] }) if defi[sche['$ref'][14..-1]].to_s.include?('$ref') keys = defi[sche['$ref'][14..-1]].to_s.split('"').find_all { |word| word.include?('definitions') } keys.each do |key| res['definitions'].merge!({ key[14..-1] => defi[key[14..-1]] }) end end res elsif sche.to_s.include?('$ref') res = sche.merge('definitions' => {}) keys = sche.to_s.split('"').find_all { |word| word.include?('definitions') } keys.each do |key| res['definitions'].merge!({ key[14..-1] => defi[key[14..-1]] }) end res else sche end end |
#search_hash(hash, key) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/tomograph/openapi/openapi2.rb', line 67 def search_hash(hash, key) return hash[key] if hash.assoc(key) hash.delete_if { |_key, value| value.class != Hash } new_hash = {} hash.each_value { |values| new_hash.merge!(values) } search_hash(new_hash, key) unless new_hash.empty? end |
#to_resources ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/tomograph/openapi/openapi2.rb', line 76 def to_resources return @to_resources if @to_resources @to_resources = @documentation.group_by { |action| action['resource'] } @to_resources = @to_resources.each_with_object({}) do |(resource, actions), resource_map| requests = actions.map do |action| "#{action['method']} #{@prefix}#{action['path']}" end resource_map[resource] = requests end end |
#to_tomogram ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tomograph/openapi/openapi2.rb', line 11 def to_tomogram @tomogram ||= @documentation['paths'].each_with_object([]) do |action, result| action[1].keys.each do |method| next result if method == 'parameters' result.push(Tomograph::Tomogram::Action.new( path: "#{@prefix}#{action[0]}", method: method.upcase, content_type: '', requests: [], responses: responses(action[1][method]['responses'], @documentation['definitions']), resource: '' )) end end end |