Class: GrapeSwagger::DocMethods::MoveParams

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

Class Method Summary collapse

Class Method Details

.find_definition_and_params(path) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/grape-swagger/doc_methods/move_params.rb', line 19

def find_definition_and_params(path)
  path.keys.each do |verb|
    params = path[verb][:parameters]

    next if params.nil?
    next unless should_move?(params)

    unify!(params)

    status_code = GrapeSwagger::DocMethods::StatusCodes.get[verb.to_sym][:code]
    response = path[verb][:responses][status_code]
    referenced_definition = parse_model(response[:schema]['$ref'])

    name = build_definition(referenced_definition, verb)
    move_params_to_new(name, params)

    @definitions[name][:description] = path[verb][:description]
    path[verb][:parameters] << build_body_parameter(response.dup, name)
  end
end

.find_post_put(paths) ⇒ Object



12
13
14
15
16
17
# File 'lib/grape-swagger/doc_methods/move_params.rb', line 12

def find_post_put(paths)
  paths.each do |x|
    found = x.last.select { |y| move_methods.include?(y) }
    yield found unless found.empty?
  end
end

.move_params_to_new(name, params) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/grape-swagger/doc_methods/move_params.rb', line 40

def move_params_to_new(name, params)
  properties = {}
  definition = @definitions[name]

  nested_definitions(name, params, properties)

  params.dup.each do |param|
    next unless movable?(param)

    name = param[:name].to_sym
    properties[name] = {}

    properties[name].tap do |x|
      property_keys.each do |attribute|
        x[attribute] = param[attribute] unless param[attribute].nil?
      end
    end

    properties[name][:readOnly] = true unless deletable?(param)
    params.delete(param) if deletable?(param)

    definition[:required] << name if deletable?(param) && param[:required]
  end

  definition.delete(:required) if definition[:required].empty?
  definition[:properties] = properties
end

.nested_definitions(name, params, properties) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/grape-swagger/doc_methods/move_params.rb', line 68

def nested_definitions(name, params, properties)
  loop do
    nested_name = params.bsearch { |x| x[:name].include?('[') }
    return if nested_name.nil?

    nested_name = nested_name[:name].split('[').first

    nested, = params.partition { |x| x[:name].start_with?("#{nested_name}[") }
    nested.each { |x| params.delete(x) }
    nested_def_name = GrapeSwagger::DocMethods::OperationId.manipulate(nested_name)
    def_name = "#{name}#{nested_def_name}"
    properties[nested_name] = { '$ref' => "#/definitions/#{def_name}" }

    prepare_nested_names(nested)
    build_definition(def_name)
    @definitions[def_name][:description] = "#{name} - #{nested_name}"
    move_params_to_new(def_name, nested)
  end
end

.to_definition(paths, definitions) ⇒ Object



5
6
7
8
9
10
# File 'lib/grape-swagger/doc_methods/move_params.rb', line 5

def to_definition(paths, definitions)
  @definitions = definitions
  find_post_put(paths) do |path|
    find_definition_and_params(path)
  end
end