Module: Jsapi::DSL::SharedOperationMethods

Included in:
Operation, Path
Defined in:
lib/jsapi/dsl/shared_operation_methods.rb

Instance Method Summary collapse

Instance Method Details

#model(klass = nil, &block) ⇒ Object

Specifies the model class to access top-level parameters by.

model Foo do
  def bar
    # ...
  end
end

klass can be any subclass of Model::Base. If block is given, an anonymous class is created that inherits either from klass or Model::Base.

See Meta::Operation#model and Meta::Path#model for further information.



18
19
20
21
22
23
24
# File 'lib/jsapi/dsl/shared_operation_methods.rb', line 18

def model(klass = nil, &block)
  if block
    klass = Class.new(klass || Model::Base)
    klass.class_eval(&block)
  end
  @meta_model.model = klass
end

#parameter(name = nil, **keywords, &block) ⇒ Object

Specifies a parameter.

parameter 'foo', type: 'string'

parameter 'foo', type: 'object' do
  property 'bar', type: 'string'
end

Refers a resuable parameter if the :ref keyword is specified.

parameter ref: 'foo'

Refers the reusable parameter with the same name if neither any keywords nor a block is specified.

parameter 'foo'

See Meta::Operation#parameters and Meta::Path#parameters for further information.



45
46
47
48
49
50
51
52
53
54
# File 'lib/jsapi/dsl/shared_operation_methods.rb', line 45

def parameter(name = nil, **keywords, &block)
  define('parameter', name&.inspect) do
    name = keywords[:ref] if name.nil?
    keywords = { ref: name } unless keywords.any? || block

    @meta_model.add_parameter(name, keywords).tap do |parameter_model|
      Parameter.new(parameter_model, &block) if block
    end
  end
end

#request_body(name = nil, **keywords, &block) ⇒ Object

:call-seq:

request_body(**keywords, &block)
request_body(name)

Specifies a request body.

request_body type: 'object' do
  property 'foo', type: 'string'
end

Refers a resuable request body if the :ref keyword is specified.

request_body ref: 'foo'

Refers the reusable request body with the same name if neither any keywords nor a block is specified.

request_body 'foo'

See Meta::Operation#request_body and Meta::Path#request_body for further information.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jsapi/dsl/shared_operation_methods.rb', line 78

def request_body(name = nil, **keywords, &block)
  define('request body') do
    raise Error, "name can't be specified together with keywords or a block" \
    if name && (keywords.any? || block)

    keywords = { ref: name } if name
    @meta_model.request_body = keywords

    @meta_model.request_body.tap do |request_body|
      RequestBody.new(request_body, &block) if block
    end
  end
end

#response(status = nil, name = nil, **keywords, &block) ⇒ Object

:call-seq:

response(status = nil, **keywords, &block)
response(status, name)
response(name)

Specifies a response.

response 200, type: 'object' do
  property 'foo', type: 'string'
end

The default status is "default".

Refers a resuable response if the :ref keyword is specified.

response 200, ref: 'foo'

Refers the reusable response with the same name if neither any keywords nor a block is specified.

response 'foo'

See Meta::Operation#responses and Meta::Path#responses for further information.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/jsapi/dsl/shared_operation_methods.rb', line 117

def response(status = nil, name = nil, **keywords, &block)
  define('response', status&.inspect) do
    if keywords.none? && !block
      status, name = nil, status unless name
      keywords = { ref: name }
    elsif name
      raise Error, "name can't be specified together with keywords or a block"
    end

    @meta_model.add_response(status, keywords).tap do |response_model|
      Response.new(response_model, &block) if block
    end
  end
end