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 |
# 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..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
#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
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
76 77 78 |
# File 'lib/swaggard/swagger/operation.rb', line 76 def empty? @summary.blank? && @description.blank? end |
#to_doc ⇒ Object
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
72 73 74 |
# File 'lib/swaggard/swagger/operation.rb', line 72 def valid? @path.present? end |