Class: GrapeSwagger::DocMethods::Extensions

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

Class Method Summary collapse

Class Method Details

.add(path, definitions, route) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/grape-swagger/doc_methods/extensions.rb', line 7

def add(path, definitions, route)
  @route = route

  description = route.settings[:description]
  add_extension_to(path[method], extension(description)) if description && extended?(description, :x)

  settings = route.settings
  add_extensions_to_operation(settings, path, route) if settings && extended?(settings, :x_operation)
  add_extensions_to_path(settings, path) if settings && extended?(settings, :x_path)
  add_extensions_to_definition(settings, path, definitions) if settings && extended?(settings, :x_def)
end

.add_extension_to(part, extensions) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/grape-swagger/doc_methods/extensions.rb', line 62

def add_extension_to(part, extensions)
  return if part.nil?

  concatenate(extensions).each do |key, value|
    part[key] = value unless key.start_with?('x-for')
  end
end

.add_extensions_to_definition(settings, path, definitions) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/grape-swagger/doc_methods/extensions.rb', line 35

def add_extensions_to_definition(settings, path, definitions)
  def_extension = extension(settings, :x_def)

  if def_extension[:x_def].is_a?(Array)
    def_extension[:x_def].each { |extension| setup_definition(extension, path, definitions) }
  else
    setup_definition(def_extension[:x_def], path, definitions)
  end
end

.add_extensions_to_info(settings, info) ⇒ Object



23
24
25
# File 'lib/grape-swagger/doc_methods/extensions.rb', line 23

def add_extensions_to_info(settings, info)
  add_extension_to(info, extension(settings)) if extended?(settings, :x)
end

.add_extensions_to_operation(settings, path, route) ⇒ Object



27
28
29
# File 'lib/grape-swagger/doc_methods/extensions.rb', line 27

def add_extensions_to_operation(settings, path, route)
  add_extension_to(path[route.request_method.downcase.to_sym], extension(settings, :x_operation))
end

.add_extensions_to_path(settings, path) ⇒ Object



31
32
33
# File 'lib/grape-swagger/doc_methods/extensions.rb', line 31

def add_extensions_to_path(settings, path)
  add_extension_to(path, extension(settings, :x_path))
end

.add_extensions_to_root(settings, object) ⇒ Object



19
20
21
# File 'lib/grape-swagger/doc_methods/extensions.rb', line 19

def add_extensions_to_root(settings, object)
  add_extension_to(object, extension(settings)) if extended?(settings, :x)
end

.concatenate(extensions) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/grape-swagger/doc_methods/extensions.rb', line 70

def concatenate(extensions)
  result = {}

  extensions.each_value do |extension|
    extension.each do |key, value|
      result["x-#{key}"] = value
    end
  end

  result
end

.extended?(part, identifier = :x) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/grape-swagger/doc_methods/extensions.rb', line 82

def extended?(part, identifier = :x)
  !extension(part, identifier).empty?
end

.extension(part, identifier = :x) ⇒ Object



86
87
88
# File 'lib/grape-swagger/doc_methods/extensions.rb', line 86

def extension(part, identifier = :x)
  part.select { |x| x == identifier }
end

.find_definition(status, path) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/grape-swagger/doc_methods/extensions.rb', line 54

def find_definition(status, path)
  response = path[method][:responses][status]
  return if response.nil?

  return response[:schema]['$ref'].split('/').last if response[:schema].key?('$ref')
  return response[:schema]['items']['$ref'].split('/').last if response[:schema].key?('items')
end

.method(*args) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/grape-swagger/doc_methods/extensions.rb', line 90

def method(*args)
  # We're shadowing Object.method(:symbol) here so we provide
  # a compatibility layer for code that introspects the methods
  # of this class
  return super if args.size.positive?

  @route.request_method.downcase.to_sym
end

.setup_definition(def_extension, path, definitions) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/grape-swagger/doc_methods/extensions.rb', line 45

def setup_definition(def_extension, path, definitions)
  return unless def_extension.key?(:for)

  status = def_extension[:for]

  definition = find_definition(status, path)
  add_extension_to(definitions[definition], x_def: def_extension)
end