Class: ApiRegulator::SharedSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/api_regulator/shared_schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, &block) ⇒ SharedSchema

Returns a new instance of SharedSchema.



5
6
7
8
9
10
11
# File 'lib/api_regulator/shared_schema.rb', line 5

def initialize(name, description, &block)
  @name = name
  @description = description
  @params = []
  @responses = {}
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



3
4
5
# File 'lib/api_regulator/shared_schema.rb', line 3

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/api_regulator/shared_schema.rb', line 3

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/api_regulator/shared_schema.rb', line 3

def params
  @params
end

#responsesObject (readonly)

Returns the value of attribute responses.



3
4
5
# File 'lib/api_regulator/shared_schema.rb', line 3

def responses
  @responses
end

Instance Method Details

#param(name, type = nil, **options, &block) ⇒ Object



13
14
15
# File 'lib/api_regulator/shared_schema.rb', line 13

def param(name, type = nil, **options, &block)
  @params << Param.new(name, type, **options, &block)
end

#response(status_code, description_or_options, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/api_regulator/shared_schema.rb', line 17

def response(status_code, description_or_options, &block)
  if description_or_options.is_a?(Hash) && description_or_options[:ref]
    # Reference to a shared schema
    resp_ref  = description_or_options[:ref]
    resp_desc = description_or_options[:desc] || resp_ref
    @responses[status_code] = Param.new(:root, :object, ref: resp_ref, desc: resp_desc, &block)
  else
    # Inline schema definition
    schema = Param.new(:root, :object, desc: description_or_options, &block)
    @responses[status_code] = schema
  end
end