Class: Swaggard::Swagger::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/swaggard/swagger/operation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yard_object, tag, routes) ⇒ Operation

Returns a new instance of Operation.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/swaggard/swagger/operation.rb', line 17

def initialize(yard_object, tag, routes)
  @name = yard_object.name
  @tag = tag
  @summary = yard_object.docstring.lines.first || ''
  @parameters  = []
  @responses = []
  @description = (yard_object.docstring.lines[1..-1] || []).join("\n")
  @formats = Swaggard.configuration.api_formats

  yard_object.tags.each do |yard_tag|
    value = yard_tag.text

    case yard_tag.tag_name
    when 'query_parameter'
      @parameters << Parameters::Query.new(value)
    when 'form_parameter'
      @parameters << Parameters::Form.new(value)
    when 'body_parameter'
      body_parameter.add_property(value)
    when 'parameter_list'
      @parameters << Parameters::List.new(value)
    when 'response_class'
      success_response.response_class = value
    when 'response_status'
      success_response.status_code = value
    when 'response_root'
      success_response.response_root = value
    end
  end

  build_path_parameters(routes)

  @parameters.sort_by { |parameter| parameter.name }

  @responses << success_response
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



14
15
16
# File 'lib/swaggard/swagger/operation.rb', line 14

def description
  @description
end

#error_responsesObject

Returns the value of attribute error_responses.



14
15
16
# File 'lib/swaggard/swagger/operation.rb', line 14

def error_responses
  @error_responses
end

#http_methodObject

Returns the value of attribute http_method.



14
15
16
# File 'lib/swaggard/swagger/operation.rb', line 14

def http_method
  @http_method
end

#nicknameObject (readonly)

Returns the value of attribute nickname.



13
14
15
# File 'lib/swaggard/swagger/operation.rb', line 13

def nickname
  @nickname
end

#notesObject

Returns the value of attribute notes.



14
15
16
# File 'lib/swaggard/swagger/operation.rb', line 14

def notes
  @notes
end

#parametersObject

Returns the value of attribute parameters.



14
15
16
# File 'lib/swaggard/swagger/operation.rb', line 14

def parameters
  @parameters
end

#pathObject

Returns the value of attribute path.



14
15
16
# File 'lib/swaggard/swagger/operation.rb', line 14

def path
  @path
end

#summaryObject

Returns the value of attribute summary.



14
15
16
# File 'lib/swaggard/swagger/operation.rb', line 14

def summary
  @summary
end

#tagObject

Returns the value of attribute tag.



14
15
16
# File 'lib/swaggard/swagger/operation.rb', line 14

def tag
  @tag
end

Instance Method Details

#definitionsObject



74
75
76
77
78
# File 'lib/swaggard/swagger/operation.rb', line 74

def definitions
  @responses.map(&:definition).compact.tap do |definitions|
    definitions << @body_parameter.definition if @body_parameter
  end
end

#to_docObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/swaggard/swagger/operation.rb', line 58

def to_doc
  produces = @formats.map { |format| "application/#{format}" }
  consumes = @formats.map { |format| "application/#{format}" }

  {
    'tags'           => [@tag.name],
    'summary'        => @summary,
    'description'    => @description,
    'operationId'    => @name,
    'consumes'       => consumes,
    'produces'       => produces,
    'parameters'     => @parameters.map(&:to_doc),
    'responses'      => Hash[@responses.map { |response| [response.status_code, response.to_doc] }]
  }
end

#valid?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/swaggard/swagger/operation.rb', line 54

def valid?
  @path.present?
end