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
# File 'lib/jsapi/dsl/definitions.rb', line 23

def callback(name, **keywords, &block)
  define('callback', name.inspect) do
    callback = @meta_model.add_callback(name, keywords)
    Callback.new(callback, &block) if block
  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.



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

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

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

Specifies a reusable example.

example '/foo', value: 'bar'

See Meta::Definitions#examples for further information.



47
48
49
50
51
52
# File 'lib/jsapi/dsl/definitions.rb', line 47

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

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

Specifies a reusable header.

header 'foo', type: 'string'

See Meta::Definitions#headers for further information.



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

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

#include(*klasses) ⇒ Object

Includes API definitions from klasses.



64
65
66
67
68
# File 'lib/jsapi/dsl/definitions.rb', line 64

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.



107
108
109
110
111
112
# File 'lib/jsapi/dsl/definitions.rb', line 107

def link(name, **keywords, &block)
  define('link', name.inspect) do
    link = @meta_model.add_link(name, keywords)
    Base.new(link, &block) if block
  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


121
122
123
124
125
# File 'lib/jsapi/dsl/definitions.rb', line 121

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.



139
140
141
142
143
144
# File 'lib/jsapi/dsl/definitions.rb', line 139

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

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

Specifies a reusable parameter.

parameter 'foo', type: 'string'

See Meta::Definitions#parameters for further information.



151
152
153
154
155
156
# File 'lib/jsapi/dsl/definitions.rb', line 151

def parameter(name, **keywords, &block)
  define('parameter', name.inspect) do
    parameter_model = @meta_model.add_parameter(name, keywords)
    Parameter.new(parameter_model, &block) if block
  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.



163
164
165
166
167
168
# File 'lib/jsapi/dsl/definitions.rb', line 163

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

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

Specifies the HTTP status code of an error response rendered when an exception of any of klasses has been raised.

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


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

def rescue_from(*klasses, with: nil)
  klasses.each do |klass|
    @meta_model.add_rescue_handler({ error_class: klass, status: 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.



188
189
190
191
192
193
# File 'lib/jsapi/dsl/definitions.rb', line 188

def response(name, **keywords, &block)
  define('response', name.inspect) do
    response_model = @meta_model.add_response(name, keywords)
    Response.new(response_model, &block) if block
  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.



202
203
204
205
206
207
# File 'lib/jsapi/dsl/definitions.rb', line 202

def schema(name, **keywords, &block)
  define('schema', name.inspect) do
    schema_model = @meta_model.add_schema(name, keywords)
    Schema.new(schema_model, &block) if block
  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.



234
235
236
237
238
239
# File 'lib/jsapi/dsl/definitions.rb', line 234

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