Class: SwaggerYard::Parameter

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, description, options = {}) ⇒ Parameter

Returns a new instance of Parameter.



35
36
37
38
39
40
41
42
# File 'lib/swagger_yard/parameter.rb', line 35

def initialize(name, type, description, options={})
  @name, @type, @description = name, type, description

  @required = options[:required] || false
  @param_type = options[:param_type] || 'query'
  @allow_multiple = options[:allow_multiple] || false
  @from_path      = options[:from_path] || false
end

Instance Attribute Details

#allow_multipleObject

Returns the value of attribute allow_multiple.



3
4
5
# File 'lib/swagger_yard/parameter.rb', line 3

def allow_multiple
  @allow_multiple
end

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/swagger_yard/parameter.rb', line 3

def description
  @description
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/swagger_yard/parameter.rb', line 3

def name
  @name
end

#param_typeObject

Returns the value of attribute param_type.



3
4
5
# File 'lib/swagger_yard/parameter.rb', line 3

def param_type
  @param_type
end

#requiredObject

Returns the value of attribute required.



3
4
5
# File 'lib/swagger_yard/parameter.rb', line 3

def required
  @required
end

Class Method Details

.from_path_param(name) ⇒ Object

TODO: support more variation in scope types



26
27
28
29
30
31
32
33
# File 'lib/swagger_yard/parameter.rb', line 26

def self.from_path_param(name)
  new(name, Type.new("string"), "Scope response to #{name}", {
    required: true,
    allow_multiple: false,
    param_type: "path",
    from_path: true
  })
end

.from_yard_tag(tag, operation) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/swagger_yard/parameter.rb', line 5

def self.from_yard_tag(tag, operation)
  description = tag.text
  name, options_string = tag.name.split(/[\(\)]/)
  type = Type.from_type_list(tag.types)

  options = {}

  operation.model_names << type.name if type.ref?

  unless options_string.nil?
    options_string.split(',').map(&:strip).tap do |arr|
      options[:required] = !arr.delete('required').nil?
      options[:allow_multiple] = !arr.delete('multiple').nil?
      options[:param_type] = arr.last
    end
  end

  new(name, type, description, options)
end

Instance Method Details

#from_path?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/swagger_yard/parameter.rb', line 44

def from_path?
  @from_path
end

#to_hObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/swagger_yard/parameter.rb', line 48

def to_h
  { "name"        => name,
    "description" => description,
    "required"    => required,
    "in"          => param_type
  }.tap do |h|
    if h["in"] == "body"
      h["schema"] = @type.to_h
    else
      h.update(@type.to_h)
    end
    h["collectionFormat"] = 'multi' if !Array(allow_multiple).empty? && h["items"]
  end
end