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
  @allowable_values = options[:allowable_values] || []
end

Instance Attribute Details

#allow_multipleObject (readonly)

Returns the value of attribute allow_multiple.



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

def allow_multiple
  @allow_multiple
end

#allowable_valuesObject (readonly)

Returns the value of attribute allowable_values.



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

def allowable_values
  @allowable_values
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 (readonly)

Returns the value of attribute param_type.



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

def param_type
  @param_type
end

#requiredObject (readonly)

Returns the value of attribute required.



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

def required
  @required
end

Class Method Details

.from_path_param(name) ⇒ Object

TODO: support more variation in scope types



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

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

.from_yard_tag(tag, operation) ⇒ Object



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

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

#allowable_values_hashObject



48
49
50
51
52
53
54
55
# File 'lib/swagger_yard/parameter.rb', line 48

def allowable_values_hash
  return nil if allowable_values.empty?

  {
    "valueType" => "LIST",
    "values" => allowable_values
  }
end

#to_hObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/swagger_yard/parameter.rb', line 57

def to_h
  {
    "paramType"       => param_type,
    "name"            => name,
    "description"     => description,
    "required"        => required,
    "allowMultiple"   => !!allow_multiple,
    "allowableValues" => allowable_values_hash 
  }.merge(@type.to_h).reject {|k,v| v.nil?}
end

#typeObject



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

def type
  @type.name
end