Class: SwaggerParser

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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ SwaggerParser

Returns a new instance of SwaggerParser.



6
7
8
9
10
11
# File 'lib/swagger_parser.rb', line 6

def initialize(path)
	@path = path
	@yaml_file = @path.include?('yaml')
	@yaml_file ? @docs = YAML.load_file(path) : @docs = File.read(path)
	@yaml_file ? @parse = @docs : @parse = JSON.parse(@docs)
end

Instance Method Details

#api_endpointsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/swagger_parser.rb', line 13

def api_endpoints
	endpoints = []
		base_path = @parse['basePath']
		if base_path == nil
			raise "'basePath' not specified in swagger documentation"
		end
		swag_path = ''
		@parse['paths'].each do |path|
			swag_path = base_path + path.first.gsub('{', ':').gsub('}', '')
			endpoints << swag_path if swag_path.include?('api')
		end
	endpoints
end

#http_requestsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/swagger_parser.rb', line 27

def http_requests
	request_types_arr = []
	paths_arr = []
	base_path = @parse['basePath']
	if base_path == nil
		raise "'basePath' not specified in swagger documentation"
	end
	@parse['paths'].each do |path|
		if base_path.include?('api')
			paths_arr << path.first
		else
			paths_arr << path.first if path.first.include?('api')
		end
	end
	paths_arr.each do |path|
		@yaml_file ? req = @parse['paths'] : req = @parse['paths'][path]
		@yaml_file ? req = req[path] : req
		req.each do |type|
			result = (type.first).upcase
			request_types_arr << result
		end
	end
	request_types_arr.sort
end