Class: OpenAPIRest::ApiDocParser

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_rest/api_doc_parser.rb

Overview

Api doc parser based on OpenAPI 2.0 specs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(openapi_path) ⇒ ApiDocParser

Returns a new instance of ApiDocParser.



9
10
11
12
13
14
# File 'lib/openapi_rest/api_doc_parser.rb', line 9

def initialize(openapi_path)
  @document = OpenAPIRest::ApiDoc.document
  @route = openapi_path[:path]
  @method = openapi_path[:method]
  @method = 'patch' if @method == 'put'
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



7
8
9
# File 'lib/openapi_rest/api_doc_parser.rb', line 7

def document
  @document
end

#methodObject (readonly)

Returns the value of attribute method.



6
7
8
# File 'lib/openapi_rest/api_doc_parser.rb', line 6

def method
  @method
end

Instance Method Details

#[](key) ⇒ Object



101
102
103
104
# File 'lib/openapi_rest/api_doc_parser.rb', line 101

def [](key)
  @current_step = @current_step.fetch(key, {})
  self
end

#base_pathObject



36
37
38
# File 'lib/openapi_rest/api_doc_parser.rb', line 36

def base_path
  document.fetch('basePath', {})
end

#definitionsObject



16
17
18
19
# File 'lib/openapi_rest/api_doc_parser.rb', line 16

def definitions
  @current_step = document.fetch('definitions', {})
  self
end

#find(key) ⇒ Object



31
32
33
34
# File 'lib/openapi_rest/api_doc_parser.rb', line 31

def find(key)
  @current_step = @current_step.fetch(key, {})
  self
end

#find_parametersObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/openapi_rest/api_doc_parser.rb', line 67

def find_parameters
  return if @current_step['parameters'].nil?

  params = {}
  ref_params = []
  @current_step['parameters'].each do |parameter|
    next if parameter['in'] == 'path'
    if parameter['in'] == 'query' || parameter['in'] == 'body'
      params[parameter['name']] = parameter['name']
      next
    end

    if !parameter['$ref'].nil? && parameter['$ref'].include?('#/parameters/')
      param = parameter['$ref'].gsub('#/parameters/', '')
      ref_params << document.fetch('parameters', {}).fetch(param, {})
    end
  end

  if ref_params.length > 0
    params.merge!(ref_params.compact.map { |param| param.fetch('schema', {}).fetch('properties', {}) }.first)
  end

  params
end

#find_pathObject



40
41
42
# File 'lib/openapi_rest/api_doc_parser.rb', line 40

def find_path
  paths.find(@route.sub(base_path, '')).find(method)
end

#keysObject



97
98
99
# File 'lib/openapi_rest/api_doc_parser.rb', line 97

def keys
  @current_step.keys
end

#parametersObject



21
22
23
24
# File 'lib/openapi_rest/api_doc_parser.rb', line 21

def parameters
  @current_step = document.fetch('parameters', {})
  self
end

#pathsObject



26
27
28
29
# File 'lib/openapi_rest/api_doc_parser.rb', line 26

def paths
  @current_step = document.fetch('paths', {})
  self
end

#propertiesObject



44
45
46
47
# File 'lib/openapi_rest/api_doc_parser.rb', line 44

def properties
  @current_step = @current_step.fetch('properties', {})
  self
end

#responsesObject



92
93
94
95
# File 'lib/openapi_rest/api_doc_parser.rb', line 92

def responses
  @current_step = @current_step.fetch('responses', {})
  self
end

#schemaObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/openapi_rest/api_doc_parser.rb', line 49

def schema
  @current_step = @current_step.fetch('schema', {})

  if !@current_step['$ref'].nil?
    if @current_step['$ref'].include?('#/definitions/')
      str = @current_step['$ref'].gsub('#/definitions/', '')
      return definitions.find(str)
    end
  elsif !@current_step['items'].nil?
    if @current_step['items']['$ref'].include?('#/definitions/')
      str = @current_step['items']['$ref'].gsub('#/definitions/', '')
      return definitions.find(str)
    end
  end

  self
end

#to_sObject



106
107
108
# File 'lib/openapi_rest/api_doc_parser.rb', line 106

def to_s
  @current_step
end