Class: JsonapiSwaggerHelpers::PayloadDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi_swagger_helpers/payload_definition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ PayloadDefinition

Returns a new instance of PayloadDefinition.



31
32
33
# File 'lib/jsonapi_swagger_helpers/payload_definition.rb', line 31

def initialize(payload)
  @payload = payload
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



3
4
5
# File 'lib/jsonapi_swagger_helpers/payload_definition.rb', line 3

def payload
  @payload
end

Class Method Details

.swagger_type_for(payload_name, attribute, type) ⇒ Object

Given a spec payload like:

key(:name, String)

Return the corresponding swagger type, ie :string If a key has multiple types, we’ll pick the first swagger type that matches:

key(:total, [String, Integer]) => :string



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jsonapi_swagger_helpers/payload_definition.rb', line 13

def self.swagger_type_for(payload_name, attribute, type)
  types = Array(type)
  return :string if types.empty?

  type_mapping.each_pair do |swagger_type, klasses|
    if types.any? { |t| klasses.include?(t) }
      return swagger_type
    end
  end

  raise JsonapiSwaggerHelpers::Errors::TypeNotFound
    .new(payload_name, attribute)
end

.type_mappingObject



27
28
29
# File 'lib/jsonapi_swagger_helpers/payload_definition.rb', line 27

def self.type_mapping
  JsonapiSwaggerHelpers.config.type_mapping
end

Instance Method Details

#contextObject



35
36
37
# File 'lib/jsonapi_swagger_helpers/payload_definition.rb', line 35

def context
  JsonapiSwaggerHelpers.docs_controller
end

#generateObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jsonapi_swagger_helpers/payload_definition.rb', line 43

def generate
  _self = self

  context.send(:swagger_schema, payload.name) do
    payload = _self.payload

    payload.keys.each_pair do |attribute, config|
      property attribute do
        type = _self.class.swagger_type_for(payload.name, attribute, config[:type])
        key :type, type
        key :description, config[:description]
      end
    end
  end
end

#jsonapi_typeObject



39
40
41
# File 'lib/jsonapi_swagger_helpers/payload_definition.rb', line 39

def jsonapi_type
  payload.type
end