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, path, verb, path_params) ⇒ Operation

Returns a new instance of Operation.



16
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/swaggard/swagger/operation.rb', line 16

def initialize(yard_object, tag, path, verb, path_params)
  @name = yard_object.name
  @tag = tag
  @summary = (yard_object.docstring.lines.first || '').chomp
  @parameters  = []
  @responses = []

  @description = (yard_object.docstring.lines[1..-1] || []).map(&:chomp).reject(&:empty?).compact.join("\n")
  @formats = Swaggard.configuration.api_formats
  @http_method = verb
  @path = path

  build_path_parameters(path_params)

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

    case yard_tag.tag_name
    when 'operation_id'
      @operation_id = "#{@tag.name}.#{value}"
    when 'query_parameter'
      @parameters << Parameters::Query.new(value)
    when 'form_parameter'
      @parameters << Parameters::Form.new(value)
    when 'body_required'
      body_parameter.is_required = true
    when 'body_description'
      body_parameter.description = value
    when 'body_title'
      body_parameter.title = value
    when 'body_definition'
      body_parameter.definition = 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
    when 'response_description'
      success_response.description = value
    when 'response_example'
      success_response.add_example(value)
    when 'response_header'
      success_response.add_header(value)
    end
  end

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

  @responses << success_response
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#error_responsesObject

Returns the value of attribute error_responses.



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

def error_responses
  @error_responses
end

#http_methodObject

Returns the value of attribute http_method.



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

def http_method
  @http_method
end

#nicknameObject (readonly)

Returns the value of attribute nickname.



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

def nickname
  @nickname
end

#notesObject

Returns the value of attribute notes.



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

def notes
  @notes
end

#parametersObject

Returns the value of attribute parameters.



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

def parameters
  @parameters
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#summaryObject

Returns the value of attribute summary.



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

def summary
  @summary
end

#tagObject

Returns the value of attribute tag.



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

def tag
  @tag
end

Instance Method Details

#definitionsObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/swaggard/swagger/operation.rb', line 94

def definitions
  @responses.map(&:definition).compact.inject({}) do |definitions, definition|
    definitions[definition.id] = definition
    definitions
  end.tap do |definitions|
    next unless @body_parameter

    definitions[@body_parameter.definition.id] = @body_parameter.definition
  end
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @summary.blank? && @description.blank?
end

#to_docObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/swaggard/swagger/operation.rb', line 80

def to_doc
  {
    'tags'           => [@tag.name],
    'operationId'    => @operation_id || @name,
    'summary'        => @summary,
    'description'    => @description,
    'produces'       => @formats.map { |format| "application/#{format}" },
  }.tap do |doc|
    doc['consumes'] = @formats.map { |format| "application/#{format}" } if @body_parameter
    doc['parameters'] = @parameters.map(&:to_doc)
    doc['responses'] = Hash[@responses.map { |response| [response.status_code, response.to_doc] }]
  end
end

#valid?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/swaggard/swagger/operation.rb', line 72

def valid?
  @path.present?
end