Class: SwaggerApi::Operations::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveAttr::Model, Concerns::Columns
Defined in:
lib/swagger_api/operations/base.rb

Direct Known Subclasses

Create, Delete, Index, Show, Update

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actionObject

Returns the value of attribute action.



7
8
9
# File 'lib/swagger_api/operations/base.rb', line 7

def action
  @action
end

#controllerObject

Returns the value of attribute controller.



7
8
9
# File 'lib/swagger_api/operations/base.rb', line 7

def controller
  @controller
end

Instance Method Details

#createObject



9
10
11
12
13
14
15
16
17
# File 'lib/swagger_api/operations/base.rb', line 9

def create
  {
    summary: "#{readable_action} #{model_name}",
    description: "#{readable_action} #{model_name.downcase}'s information",
    parameters: parameters,
    responses: responses,
    tags: [model_name],
  }
end

#error_responsesObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/swagger_api/operations/base.rb', line 59

def error_responses
  [
    {
      '404' => { '$ref' => '#/components/responses/NotFound' }
    },
    {
      '401' => { '$ref' => '#/components/responses/Unauthorized' }
    },
    {
      '422' => { '$ref' => '#/components/responses/BadRequest' }
    }
  ]
end

#modelObject



81
82
83
# File 'lib/swagger_api/operations/base.rb', line 81

def model
  @model ||= model_name.constantize
end

#model_nameObject



77
78
79
# File 'lib/swagger_api/operations/base.rb', line 77

def model_name
  @model_name ||= controller.model
end

#parametersObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/swagger_api/operations/base.rb', line 19

def parameters
  [
    {
      name: 'id',
      in: 'path',
      description: "ID of #{model_name}",
      required: true,
      schema: {
        type: :integer,
        format: :int64,
        minimum: 1
      },
    }
  ]
end

#readable_actionObject



73
74
75
# File 'lib/swagger_api/operations/base.rb', line 73

def readable_action
  @readable_action ||= self.class.name.demodulize.downcase
end

#responsesObject



35
36
37
38
39
40
41
42
# File 'lib/swagger_api/operations/base.rb', line 35

def responses
  return @responses unless @responses.nil?
  @responses = success_response
  error_responses.each do |error_response|
    @responses.merge!(error_response)
  end
  @responses
end

#success_responseObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/swagger_api/operations/base.rb', line 44

def success_response
  {
    '200' => {
      description: "#{readable_action} #{model_name.downcase}'s information",
      content: {
        'application/json; charset=utf-8' => {
          schema: {
            '$ref' => "#/components/schemas/#{model_name}"
          }
        }
      }
    }
  }
end