Class: Swaggard::Swagger::Operation
- Inherits:
-
Object
- Object
- Swaggard::Swagger::Operation
- Defined in:
- lib/swaggard/swagger/operation.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#error_responses ⇒ Object
Returns the value of attribute error_responses.
-
#http_method ⇒ Object
Returns the value of attribute http_method.
-
#nickname ⇒ Object
readonly
Returns the value of attribute nickname.
-
#notes ⇒ Object
Returns the value of attribute notes.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#path ⇒ Object
Returns the value of attribute path.
-
#summary ⇒ Object
Returns the value of attribute summary.
-
#tag ⇒ Object
Returns the value of attribute tag.
Instance Method Summary collapse
- #definitions ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(yard_object, tag, path, verb, path_params) ⇒ Operation
constructor
A new instance of Operation.
- #to_doc ⇒ Object
- #valid? ⇒ Boolean
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 71 |
# File 'lib/swaggard/swagger/operation.rb', line 16 def initialize(yard_object, tag, path, verb, path_params) @name = yard_object.name.to_s @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") @http_method = verb @path = path build_path_parameters(path_params) yard_object..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_content_type' body_parameter.content_type = 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
#description ⇒ Object
Returns the value of attribute description.
13 14 15 |
# File 'lib/swaggard/swagger/operation.rb', line 13 def description @description end |
#error_responses ⇒ Object
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_method ⇒ Object
Returns the value of attribute http_method.
13 14 15 |
# File 'lib/swaggard/swagger/operation.rb', line 13 def http_method @http_method end |
#nickname ⇒ Object (readonly)
Returns the value of attribute nickname.
12 13 14 |
# File 'lib/swaggard/swagger/operation.rb', line 12 def nickname @nickname end |
#notes ⇒ Object
Returns the value of attribute notes.
13 14 15 |
# File 'lib/swaggard/swagger/operation.rb', line 13 def notes @notes end |
#parameters ⇒ Object
Returns the value of attribute parameters.
13 14 15 |
# File 'lib/swaggard/swagger/operation.rb', line 13 def parameters @parameters end |
#path ⇒ Object
Returns the value of attribute path.
13 14 15 |
# File 'lib/swaggard/swagger/operation.rb', line 13 def path @path end |
#summary ⇒ Object
Returns the value of attribute summary.
13 14 15 |
# File 'lib/swaggard/swagger/operation.rb', line 13 def summary @summary end |
#tag ⇒ Object
Returns the value of attribute tag.
13 14 15 |
# File 'lib/swaggard/swagger/operation.rb', line 13 def tag @tag end |
Instance Method Details
#definitions ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/swaggard/swagger/operation.rb', line 104 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
77 78 79 |
# File 'lib/swaggard/swagger/operation.rb', line 77 def empty? @summary.blank? && @description.blank? end |
#to_doc ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/swaggard/swagger/operation.rb', line 81 def to_doc regular_params = @parameters.reject { |p| p.is_a?(Parameters::Body) || p.is_a?(Parameters::Form) } body_param = @parameters.find { |p| p.is_a?(Parameters::Body) } form_params = @parameters.select { |p| p.is_a?(Parameters::Form) } doc = { 'tags' => [@tag.name], 'operationId' => @operation_id || @name, 'summary' => @summary, 'description' => @description, 'parameters' => regular_params.map(&:to_doc), 'responses' => Hash[@responses.map { |response| [response.status_code, response.to_doc] }] } if body_param && !body_param.empty? doc['requestBody'] = body_param.to_request_body elsif form_params.any? doc['requestBody'] = build_form_request_body(form_params) end doc end |
#valid? ⇒ Boolean
73 74 75 |
# File 'lib/swaggard/swagger/operation.rb', line 73 def valid? @path.present? end |