Module: Jsapi::Controller::Methods
- Included in:
- Base
- Defined in:
- lib/jsapi/controller/methods.rb
Instance Method Summary collapse
-
#api_definitions ⇒ Object
Returns the Meta::Definitions instance associated with the controller class.
-
#api_operation(operation_name = nil, status: nil, strong: false, &block) ⇒ Object
Performs an API operation by calling the given block.
-
#api_operation!(operation_name = nil, status: nil, strong: false, &block) ⇒ Object
Like
api_operation, except that a ParametersInvalid exception is raised if the request parameters are invalid. -
#api_params(operation_name = nil, strong: false) ⇒ Object
Returns the request parameters as an instance of the operation’s model class.
-
#api_response(result, operation_name = nil, status: nil) ⇒ Object
Returns a Response to serialize the JSON representation of
resultaccording to the appropriateresponsespecification.
Instance Method Details
#api_definitions ⇒ Object
Returns the Meta::Definitions instance associated with the controller class. This method can be used to create an OpenAPI document, for example:
render(json: api_definitions.openapi_document)
11 12 13 |
# File 'lib/jsapi/controller/methods.rb', line 11 def api_definitions self.class.api_definitions end |
#api_operation(operation_name = nil, status: nil, strong: false, &block) ⇒ Object
Performs an API operation by calling the given block. The request parameters are passed as an instance of the operation’s model class to the block. The object returned by the block is implicitly rendered according to the appropriate response specification.
api_operation('foo') do |api_params|
# ...
end
operation_name can be nil if the controller handles one operation only.
If strong is true, parameters that can be mapped are accepted only. That means that the model passed to the block is invalid if there are any request parameters that cannot be mapped to a parameter or a request body property of the operation.
31 32 33 34 35 36 37 38 |
# File 'lib/jsapi/controller/methods.rb', line 31 def api_operation(operation_name = nil, status: nil, strong: false, &block) _perform_api_operation( operation_name, false, status: status, strong: strong, &block ) end |
#api_operation!(operation_name = nil, status: nil, strong: false, &block) ⇒ Object
Like api_operation, except that a ParametersInvalid exception is raised if the request parameters are invalid.
api_operation!('foo') do |api_params|
# ...
end
47 48 49 50 51 52 53 54 |
# File 'lib/jsapi/controller/methods.rb', line 47 def api_operation!(operation_name = nil, status: nil, strong: false, &block) _perform_api_operation( operation_name, true, status: status, strong: strong, &block ) end |
#api_params(operation_name = nil, strong: false) ⇒ Object
Returns the request parameters as an instance of the operation’s model class.
params = api_params('foo')
operation_name can be nil if the controller handles one operation only.
If strong is true, parameters that can be mapped are accepted only. That means that the model returned is invalid if there are any request parameters that cannot be mapped to a parameter or a request body property of the operation.
Note that each call of api_params returns a newly created instance.
69 70 71 72 73 74 75 76 |
# File 'lib/jsapi/controller/methods.rb', line 69 def api_params(operation_name = nil, strong: false) definitions = api_definitions _api_params( _api_operation(operation_name, definitions), definitions, strong: strong ) end |
#api_response(result, operation_name = nil, status: nil) ⇒ Object
Returns a Response to serialize the JSON representation of result according to the appropriate response specification.
render(json: api_response(, 'foo', status: 200))
operation_name can be nil if the controller handles one operation only.
84 85 86 87 88 89 90 |
# File 'lib/jsapi/controller/methods.rb', line 84 def api_response(result, operation_name = nil, status: nil) definitions = api_definitions operation = _api_operation(operation_name, definitions) response = _api_response(operation, status, definitions) Response.new(result, response, api_definitions) end |