Class: GrapeSwagger::DocMethods::FormatData

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

Class Method Summary collapse

Class Method Details

.add_array(parameter, related_parameters) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/grape-swagger/doc_methods/format_data.rb', line 36

def add_array(parameter, related_parameters)
  related_parameters.each do |p|
    next if p.key?(:items)

    p_type = p[:type] == 'array' ? 'string' : p[:type]
    p[:items] = { type: p_type, format: p[:format], enum: p[:enum], is_array: p[:is_array] }
    p[:items].delete_if { |_k, v| v.nil? }
    p[:type] = 'array'
    p[:is_array] = parameter[:is_array]
    p.delete(:format)
    p.delete(:enum)
    p.delete_if { |_k, v| v.nil? }
  end
end

.add_braces(parameter, related_parameters) ⇒ Object



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

def add_braces(parameter, related_parameters)
  param_name = parameter[:name].gsub(/\A(.*)\[\]\z/, '\1')
  related_parameters.each { |p| p[:name] = p[:name].gsub(param_name, "#{param_name}[]") }
end

.move_down(parameter, related_parameters) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/grape-swagger/doc_methods/format_data.rb', line 17

def move_down(parameter, related_parameters)
  case parameter[:type]
  when 'array'
    add_array(parameter, related_parameters)
    unless related_parameters.blank?
      add_braces(parameter, related_parameters) if parameter[:name].match?(/\A.*\[\]\z/)
      return true
    end
  when 'object'
    return true
  end
  false
end

.to_format(parameters) ⇒ Object



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

def to_format(parameters)
  parameters.reject { |parameter| parameter[:in] == 'body' }.each do |b|
    related_parameters = parameters.select do |p|
      p[:name] != b[:name] && p[:name].to_s.start_with?("#{b[:name].to_s.gsub(/\[\]\z/, '')}[")
    end
    parameters.reject! { |p| p[:name] == b[:name] } if move_down(b, related_parameters)
  end
  parameters
end