Class: Jsapi::DSL::Operation

Inherits:
Base
  • Object
show all
Defined in:
lib/jsapi/dsl/operation.rb

Overview

Used to define an API operation.

Instance Method Summary collapse

Methods inherited from Base

#import, #import_relative, #initialize, #respond_to_missing?

Constructor Details

This class inherits a constructor from Jsapi::DSL::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Jsapi::DSL::Base

Instance Method Details

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

Specifies a callback.

callback 'foo' do
  operation '{$request.query.bar}'
end

Refers a resuable callback if the ‘:ref` keyword is specified.

callback ref: 'foo'

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

callback 'foo'

See Meta::Operation#callbacks for further information.



24
25
26
27
28
29
30
31
32
# File 'lib/jsapi/dsl/operation.rb', line 24

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

    callback_model = @meta_model.add_callback(name, keywords)
    Callback.new(callback_model, &block) if block
  end
end

#method(arg) ⇒ Object

Specifies the HTTP verb of the operation.

method 'post'

See Meta::Operation#method for further information.



60
61
62
# File 'lib/jsapi/dsl/operation.rb', line 60

def method(arg)
  keyword(:method, arg)
end

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

Defines 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.



74
75
76
77
78
79
80
# File 'lib/jsapi/dsl/operation.rb', line 74

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 for further information.



100
101
102
103
104
105
106
107
108
# File 'lib/jsapi/dsl/operation.rb', line 100

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

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

#request_body(**keywords, &block) ⇒ Object

Specifies the 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 for further information.



131
132
133
134
135
136
# File 'lib/jsapi/dsl/operation.rb', line 131

def request_body(**keywords, &block)
  define('request body') do
    @meta_model.request_body = keywords
    RequestBody.new(@meta_model.request_body, &block) if block
  end
end

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

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'

Raises an Error if name is specified together with keywords or a block.

See Meta::Operation#responses for further information.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/jsapi/dsl/operation.rb', line 158

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

    if keywords.any? || block
      status = status_or_name
    else
      status = status_or_name if name
      keywords = { ref: name || status_or_name }
    end
    response_model = @meta_model.add_response(status, keywords)
    Response.new(response_model, &block) if block
  end
end