Class: Jsapi::DSL::Definitions

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

Overview

Used to define top-level API components.

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, **keywords, &block) ⇒ Object

Specifies a reusable callback.

callback 'foo' do
  operation '{$request.query.foo}', path: '/bar'
end

See Meta::Definitions#callbacks for further information.



23
24
25
26
27
28
29
# File 'lib/jsapi/dsl/definitions.rb', line 23

def callback(name, **keywords, &block)
  define('callback', name.inspect) do
    @meta_model.add_callback(name, keywords).tap do |callback|
      Base.new(callback, &block) if block
    end
  end
end

#default(type, **keywords, &block) ⇒ Object

Specifies the general default values for type.

default 'array', within_requests: [], within_responses: []

See Meta::Definitions#defaults for further information.



36
37
38
39
40
41
42
# File 'lib/jsapi/dsl/definitions.rb', line 36

def default(type, **keywords, &block)
  define('default', type.inspect) do
    @meta_model.add_default(type, keywords).tap do |default|
      Base.new(default, &block) if block
    end
  end
end

#example(name, **keywords, &block) ⇒ Object

Specifies a reusable example.

example '/foo', value: 'bar'

See Meta::Definitions#examples for further information.



49
50
51
52
53
54
55
# File 'lib/jsapi/dsl/definitions.rb', line 49

def example(name, **keywords, &block)
  define('example', name.inspect) do
    @meta_model.add_example(name, keywords).tap do |example|
      Base.new(example, &block) if block
    end
  end
end

#header(name, **keywords, &block) ⇒ Object

Specifies a reusable header.

header 'foo', type: 'string'

See Meta::Definitions#headers for further information.



78
79
80
81
82
83
84
# File 'lib/jsapi/dsl/definitions.rb', line 78

def header(name, **keywords, &block)
  define('header', name.inspect) do
    @meta_model.add_header(name, keywords).tap do |header|
      Base.new(header, &block) if block
    end
  end
end

#include(*klasses) ⇒ Object

Includes API definitions from klasses.



67
68
69
70
71
# File 'lib/jsapi/dsl/definitions.rb', line 67

def include(*klasses)
  klasses.each do |klass|
    @meta_model.include(klass.api_definitions)
  end
end

Specifies a reusable link.

link 'foo', operation_id: 'bar'

See Meta::Definitions#links for further information.



111
112
113
114
115
116
117
# File 'lib/jsapi/dsl/definitions.rb', line 111

def link(name, **keywords, &block)
  define('link', name.inspect) do
    @meta_model.add_link(name, keywords).tap do |link|
      Base.new(link, &block) if block
    end
  end
end

#on_rescue(method = nil, &block) ⇒ Object

Registers a callback to be called when rescuing an exception.

on_rescue :foo

on_rescue do |error|
  # ...
end


126
127
128
129
130
# File 'lib/jsapi/dsl/definitions.rb', line 126

def on_rescue(method = nil, &block)
  define('on_rescue') do
    @meta_model.add_on_rescue(method || block)
  end
end

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

Specifies an operation.

operation 'foo', path: '/foo' do
  parameter 'bar', type: 'string'
  response do
    property 'foo', type: 'string'
  end
end

name can be nil if the controller handles one operation only.

See Meta::Definitions#operations for further information.



144
145
146
147
148
149
150
# File 'lib/jsapi/dsl/definitions.rb', line 144

def operation(name = nil, **keywords, &block)
  define('operation', name&.inspect) do
    @meta_model.add_operation(name, keywords).tap do |operation_model|
      Operation.new(operation_model, &block) if block
    end
  end
end

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

Specifies a reusable parameter.

parameter 'foo', type: 'string'

See Meta::Definitions#parameters for further information.



157
158
159
160
161
162
163
# File 'lib/jsapi/dsl/definitions.rb', line 157

def parameter(name, **keywords, &block)
  define('parameter', name.inspect) do
    @meta_model.add_parameter(name, keywords).tap do |parameter_model|
      Parameter.new(parameter_model, &block) if block
    end
  end
end

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

Groups operations by path.

path 'api' do
  operation 'foo'
  operation 'bar'
end


172
173
174
175
176
177
178
# File 'lib/jsapi/dsl/definitions.rb', line 172

def path(name = nil, **keywords, &block)
  define('path', name&.inspect) do
    @meta_model.add_path(name, keywords).tap do |path_model|
      Path.new(path_model, &block) if block
    end
  end
end

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

Specifies a reusable request body.

request_body 'foo', type: 'string'

See Meta::Definitions#request_bodies for further information.



185
186
187
188
189
190
191
# File 'lib/jsapi/dsl/definitions.rb', line 185

def request_body(name, **keywords, &block)
  define('request_body', name.inspect) do
    @meta_model.add_request_body(name, keywords).tap do |request_body_model|
      RequestBody.new(request_body_model, &block) if block
    end
  end
end

#rescue_from(*klasses, with: nil) ⇒ Object

Specifies the status code of a response to be produced when an error of any of klasses has been raised.

rescue_from Jsapi::Controller::ParametersInvalid, with: 400


198
199
200
201
202
203
204
# File 'lib/jsapi/dsl/definitions.rb', line 198

def rescue_from(*klasses, with: nil)
  klasses.each do |klass|
    @meta_model.add_rescue_handler(
      { error_class: klass, status_code: with }
    )
  end
end

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

Specifies a reusable response.

response 'Foo', type: 'object' do
  property 'bar', type: 'string'
end

See Meta::Definitions#responses for further information.



213
214
215
216
217
218
219
# File 'lib/jsapi/dsl/definitions.rb', line 213

def response(name, **keywords, &block)
  define('response', name.inspect) do
    @meta_model.add_response(name, keywords).tap do |response_model|
      Response.new(response_model, &block) if block
    end
  end
end

#schema(name, **keywords, &block) ⇒ Object

Specifies a reusable schema.

schema 'Foo' do
  property 'bar', type: 'string'
end

See Meta::Definitions#schemas for further information.



228
229
230
231
232
233
234
# File 'lib/jsapi/dsl/definitions.rb', line 228

def schema(name, **keywords, &block)
  define('schema', name.inspect) do
    @meta_model.add_schema(name, keywords).tap do |schema_model|
      Schema.new(schema_model, &block) if block
    end
  end
end

#security_scheme(name, **keywords, &block) ⇒ Object

Specifies a security scheme.

security_scheme 'basic_auth', type: 'http', scheme: 'basic'

See Meta::Definitions#security_schemes for further information.



261
262
263
264
265
266
267
# File 'lib/jsapi/dsl/definitions.rb', line 261

def security_scheme(name, **keywords, &block)
  define('security_scheme', name.inspect) do
    @meta_model.add_security_scheme(name, keywords).tap do |security_scheme|
      Base.new(security_scheme, &block) if block
    end
  end
end