Class: Jsapi::DSL::Operation
- Includes:
- Callbacks
- Defined in:
- lib/jsapi/dsl/operation.rb
Overview
Used to specify details of an operation.
Instance Method Summary collapse
-
#method(method) ⇒ Object
Overrides Object#method to handle
methodas a keyword. -
#model(klass = nil, &block) ⇒ Object
Specifies the model class to access top-level parameters by.
-
#parameter(name = nil, **keywords, &block) ⇒ Object
Defines a parameter or refers a reusable parameter.
-
#request_body(**keywords, &block) ⇒ Object
Defines the request body or refers a reusable request body.
-
#response(status_or_name = nil, name = nil, **keywords, &block) ⇒ Object
Defines a response or refers a reusable response.
Methods included from Callbacks
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
#method(method) ⇒ Object
Overrides Object#method to handle method as a keyword.
10 11 12 |
# File 'lib/jsapi/dsl/operation.rb', line 10 def method(method) # :nodoc: _keyword(:method, method) end |
#model(klass = nil, &block) ⇒ Object
Specifies the model class to access top-level parameters by.
model Foo do
def
# ...
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.
25 26 27 28 29 30 31 |
# File 'lib/jsapi/dsl/operation.rb', line 25 def model(klass = nil, &block) if block klass = Class.new(klass || Model::Base) klass.class_eval(&block) end .model = klass end |
#parameter(name = nil, **keywords, &block) ⇒ Object
Defines a parameter or refers a reusable parameter.
# define a parameter
parameter 'foo', type: 'string'
# define a nested parameter
parameter 'foo', type: 'object' do
property 'bar', type: 'string'
end
# refer a reusable parameter
parameter ref: 'foo'
Refers the reusable parameter with the same name if neither any keywords nor a block is specified.
parameter 'foo'
51 52 53 54 55 56 57 58 59 |
# File 'lib/jsapi/dsl/operation.rb', line 51 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 = .add_parameter(name, keywords) _eval(parameter_model, Parameter, &block) end end |
#request_body(**keywords, &block) ⇒ Object
Defines the request body or refers a reusable request body.
# define a request body
request_body type: 'object' do
property 'foo', type: 'string'
end
# refer a reusable request body
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'
76 77 78 79 80 81 |
# File 'lib/jsapi/dsl/operation.rb', line 76 def request_body(**keywords, &block) _define('request body') do .request_body = keywords _eval(.request_body, RequestBody, &block) end end |
#response(status_or_name = nil, name = nil, **keywords, &block) ⇒ Object
Defines a response or refers a reusable response.
# define a response
response 200, type: 'object' do
property 'foo', type: 'string'
end
# refer a reusable response
response 200, ref: 'foo'
The default status is "default".
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.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/jsapi/dsl/operation.rb', line 101 def response(status_or_name = nil, name = nil, **keywords, &block) _define('response', status_or_name&.inspect) do raise Error, 'name cannot 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 = .add_response(status, keywords) _eval(response_model, Response, &block) end end |