Class: SwaggerCoverage::Parser

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

Instance Method Summary collapse

Constructor Details

#initialize(output, swagger_docs) ⇒ Parser

Returns a new instance of Parser.



8
9
10
11
12
13
# File 'lib/swagger_cov.rb', line 8

def initialize(output, swagger_docs)
  @output = output
  @swagger_docs = swagger_docs
  @swagger_parser = SwaggerParser.new(@swagger_docs)
  @code_parser = CodeParser.new(output)
end

Instance Method Details

#check_request_typesObject



15
16
17
18
19
# File 'lib/swagger_cov.rb', line 15

def check_request_types
  code_request_types = @code_parser.http_requests
  swagger_request_types = @swagger_parser.http_requests
  request_types_diff = compare_routes(code_request_types, swagger_request_types)
end

#check_routes(api = false) ⇒ Object



21
22
23
24
25
# File 'lib/swagger_cov.rb', line 21

def check_routes(api = false)
  code_endpoints = @code_parser.api_endpoints(@output)
  swagger_endpoints = @swagger_parser.api_endpoints
  routes_diff = compare_routes(code_endpoints, swagger_endpoints)
end

#compare_routes(code_routes, swagger_routes) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/swagger_cov.rb', line 27

def compare_routes(code_routes, swagger_routes)
  endpoints_match = code_routes.eql?(swagger_routes)
  endpoint_diff = []
  if !endpoints_match
    if code_routes.size() > swagger_routes.size()
      endpoint_diff = code_routes - swagger_routes
      #raise "DOCUMENTATION DOES NOT CONTAIN THE FOLLOWING ROUTES FROM SOURCE: #{endpoint_diff}"
    else
      endpoint_diff = swagger_routes - code_routes
      #raise "SOURCE CODE CONTAINS THE FOLLOWING ROUTES NOT FOUND IN DOCUMENTATION: #{endpoint_diff}"
    end
  end
  endpoint_diff
end

#source_api_routesObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/swagger_cov.rb', line 42

def source_api_routes
  output = @output
  endpoints = []
  @code_parser.parsed_routes(output).each do |route|
    if route[:route] != nil
      endpoints << route[:route] if route[:route].include?('/api')
    end
  end
  endpoints.sort
end

#source_routesObject



53
54
55
56
57
58
59
60
# File 'lib/swagger_cov.rb', line 53

def source_routes
  output = @output
  endpoints = []
  @code_parser.parsed_routes(ouput) do |route|
    endpoints << route[:route] if route[:route] != nil
  end
endpoints.sort
end

#swagger_api_routesObject



62
63
64
65
# File 'lib/swagger_cov.rb', line 62

def swagger_api_routes
  swagger_routes = @swagger_parser.api_endpoints
  swagger_routes.sort
end