Class: Swaggard::Swagger::Response

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

Defined Under Namespace

Classes: ResponseModel

Constant Summary collapse

PRIMITIVE_TYPES =
%w[integer long float double string byte binary boolean date date-time password hash]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation_name) ⇒ Response

Returns a new instance of Response.



11
12
13
14
15
# File 'lib/swaggard/swagger/response.rb', line 11

def initialize(operation_name)
  @operation_name = operation_name
  @response_model = ResponseModel.new
  @headers = []
end

Instance Attribute Details

#descriptionObject



46
47
48
# File 'lib/swaggard/swagger/response.rb', line 46

def description
  @description || Swaggard.configuration.default_response_description
end

#status_codeObject



25
26
27
# File 'lib/swaggard/swagger/response.rb', line 25

def status_code
  @status_code || Swaggard.configuration.default_response_status_code
end

Instance Method Details

#add_example(value) ⇒ Object



38
39
40
# File 'lib/swaggard/swagger/response.rb', line 38

def add_example(value)
  @example = value
end

#add_header(value) ⇒ Object



42
43
44
# File 'lib/swaggard/swagger/response.rb', line 42

def add_header(value)
  @headers << ResponseHeader.new(value)
end

#definitionObject



17
18
19
20
21
22
23
# File 'lib/swaggard/swagger/response.rb', line 17

def definition
  return unless @response_root.present?

  @definition ||= Definition.new("#{@operation_name}_response").tap do |definition|
    definition.add_property(@response_model)
  end
end

#example_to_docObject



66
67
68
69
70
71
72
# File 'lib/swaggard/swagger/response.rb', line 66

def example_to_doc
  Swaggard.configuration.api_formats.inject({}) do |examples, format|
    examples.merge!("application/#{format}" => { '$ref' => @example })

    examples
  end
end

#response_class=(value) ⇒ Object



29
30
31
# File 'lib/swaggard/swagger/response.rb', line 29

def response_class=(value)
  @response_model.parse(value)
end

#response_root=(root) ⇒ Object



33
34
35
36
# File 'lib/swaggard/swagger/response.rb', line 33

def response_root=(root)
  @response_root = root
  @response_model.id = root
end

#to_docObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/swaggard/swagger/response.rb', line 50

def to_doc
  { 'description' => description }.tap do |doc|
    schema = if @response_root.present?
               { '$ref' => "#/definitions/#{definition.id}" }
             elsif @response_model.response_class.present?
               @response_model.to_doc
             end

    doc.merge!('schema' => schema) if schema
    doc.merge!('examples' => example_to_doc) if @example
    if @headers.any?
      doc['headers'] = Hash[@headers.map { |header| [header.name, header.to_doc] }]
    end
  end
end