Class: Jsapi::DSL::Definitions
- Defined in:
- lib/jsapi/dsl/definitions.rb
Overview
Used to define top-level API components.
Instance Method Summary collapse
-
#include(*klasses) ⇒ Object
Includes API definitions from
klasses. -
#openapi(**keywords, &block) ⇒ Object
Defines the root of an OpenAPI document.
-
#operation(name = nil, **keywords, &block) ⇒ Object
Defines an operation.
-
#parameter(name, **keywords, &block) ⇒ Object
Defines a reusable parameter.
-
#request_body(name, **keywords, &block) ⇒ Object
Defines a reusable request body.
-
#rescue_from(*klasses, with: nil) ⇒ Object
Specifies the HTTP status code of an error response rendered when an exception of any of
klasseshas been raised. -
#response(name, **keywords, &block) ⇒ Object
Defines a reusable response.
-
#schema(name, **keywords, &block) ⇒ Object
Defines a reusable schema.
Methods inherited from Node
#initialize, #method_missing, #respond_to_missing?
Constructor Details
This class inherits a constructor from Jsapi::DSL::Node
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Jsapi::DSL::Node
Instance Method Details
#include(*klasses) ⇒ Object
Includes API definitions from klasses.
9 10 11 12 13 |
# File 'lib/jsapi/dsl/definitions.rb', line 9 def include(*klasses) klasses.each do |klass| .include(klass.api_definitions) end end |
#openapi(**keywords, &block) ⇒ Object
Defines the root of an OpenAPI document.
openapi do
info title: 'Foo', version: '1'
end
20 21 22 23 24 25 |
# File 'lib/jsapi/dsl/definitions.rb', line 20 def openapi(**keywords, &block) _define('openapi') do .openapi_root = keywords OpenAPI::Root.new(.openapi_root, &block) if block end end |
#operation(name = nil, **keywords, &block) ⇒ Object
Defines 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.
37 38 39 40 41 42 |
# File 'lib/jsapi/dsl/definitions.rb', line 37 def operation(name = nil, **keywords, &block) _define('operation', name&.inspect) do operation_model = .add_operation(name, keywords) Operation.new(operation_model, &block) if block end end |
#parameter(name, **keywords, &block) ⇒ Object
Defines a reusable parameter.
parameter 'foo', type: 'string'
48 49 50 51 52 53 |
# File 'lib/jsapi/dsl/definitions.rb', line 48 def parameter(name, **keywords, &block) _define('parameter', name.inspect) do parameter_model = .add_parameter(name, keywords) Parameter.new(parameter_model, &block) if block end end |
#request_body(name, **keywords, &block) ⇒ Object
Defines a reusable request body.
request_body 'foo', type: 'string'
59 60 61 62 63 64 |
# File 'lib/jsapi/dsl/definitions.rb', line 59 def request_body(name, **keywords, &block) _define('request_body', name.inspect) do request_body_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
71 72 73 74 75 |
# File 'lib/jsapi/dsl/definitions.rb', line 71 def rescue_from(*klasses, with: nil) klasses.each do |klass| .add_rescue_handler(klass, status: with) end end |
#response(name, **keywords, &block) ⇒ Object
Defines a reusable response.
response 'Foo', type: 'object' do
property 'bar', type: 'string'
end
82 83 84 85 86 87 |
# File 'lib/jsapi/dsl/definitions.rb', line 82 def response(name, **keywords, &block) _define('response', name.inspect) do response_model = .add_response(name, keywords) Response.new(response_model, &block) if block end end |
#schema(name, **keywords, &block) ⇒ Object
Defines a reusable schema.
schema 'Foo' do
property 'bar', type: 'string'
end
94 95 96 97 98 99 |
# File 'lib/jsapi/dsl/definitions.rb', line 94 def schema(name, **keywords, &block) _define('schema', name.inspect) do schema_model = .add_schema(name, keywords) Schema.new(schema_model, &block) if block end end |