Class: Grape::API

Inherits:
Object
  • Object
show all
Defined in:
lib/grape-swagger.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.combined_namespace_identifiersObject

Returns the value of attribute combined_namespace_identifiers


12
13
14
# File 'lib/grape-swagger.rb', line 12

def combined_namespace_identifiers
  @combined_namespace_identifiers
end

.combined_namespace_routesObject

Returns the value of attribute combined_namespace_routes


12
13
14
# File 'lib/grape-swagger.rb', line 12

def combined_namespace_routes
  @combined_namespace_routes
end

.combined_namespacesObject

Returns the value of attribute combined_namespaces


12
13
14
# File 'lib/grape-swagger.rb', line 12

def combined_namespaces
  @combined_namespaces
end

.combined_routesObject

Returns the value of attribute combined_routes


12
13
14
# File 'lib/grape-swagger.rb', line 12

def combined_routes
  @combined_routes
end

Class Method Details

.add_swagger_documentation(options = {}) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/grape-swagger.rb', line 14

def add_swagger_documentation(options = {})
  documentation_class = create_documentation_class

  options = { target_class: self }.merge(options)
  @target_class = options[:target_class]

  documentation_class.setup(options)
  mount(documentation_class)

  @target_class.combined_routes = {}
  @target_class.routes.each do |route|
    route_path = route.route_path
    route_match = route_path.split(/^.*?#{route.route_prefix.to_s}/).last
    next unless route_match
    route_match = route_match.match('\/([\w|-]*?)[\.\/\(]') || route_match.match('\/([\w|-]*)$')
    next unless route_match
    resource = route_match.captures.first
    next if resource.empty?
    resource.downcase!
    @target_class.combined_routes[resource] ||= []
    next if documentation_class.hide_documentation_path && route.route_path.match(/#{documentation_class.mount_path}($|\/|\(\.)/)
    @target_class.combined_routes[resource] << route
  end

  @target_class.combined_namespaces = {}
  combine_namespaces(@target_class)

  @target_class.combined_namespace_routes = {}
  @target_class.combined_namespace_identifiers = {}
  combine_namespace_routes(@target_class.combined_namespaces)

  exclusive_route_keys = @target_class.combined_routes.keys - @target_class.combined_namespaces.keys
  exclusive_route_keys.each { |key| @target_class.combined_namespace_routes[key] = @target_class.combined_routes[key] }
  documentation_class
end