Module: JSONRPC::Helpers
- Defined in:
- lib/jsonrpc/helpers.rb
Overview
Framework-agnostic helpers for JSON-RPC
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ void
Extends the including class with ClassMethods when module is included.
Instance Method Summary collapse
-
#env ⇒ Hash
private
Gets the Rack environment hash from @env or the Rails Request.
-
#jsonrpc_batch ⇒ BatchRequest?
Gets the current JSON-RPC batch request object.
-
#jsonrpc_batch? ⇒ Boolean
Checks if the current request is a batch request.
-
#jsonrpc_batch_response(responses) ⇒ Array
Creates a JSON-RPC batch response.
-
#jsonrpc_error(error) ⇒ String
Creates a JSON-RPC error response.
-
#jsonrpc_internal_error(data: nil) ⇒ String
Creates an Internal error (-32603) for server errors.
-
#jsonrpc_invalid_params_error(data: nil) ⇒ String
Creates an Invalid params error (-32602) for invalid parameters.
-
#jsonrpc_invalid_request_error(data: nil) ⇒ String
Creates an Invalid Request error (-32600) for invalid Request objects.
-
#jsonrpc_method_not_found_error(data: nil) ⇒ String
Creates a Method not found error (-32601) for missing methods.
-
#jsonrpc_notification ⇒ Notification?
Gets the current JSON-RPC notification object.
-
#jsonrpc_notification? ⇒ Boolean
Checks if the current request is a notification.
-
#jsonrpc_notification_response ⇒ Array
Creates a response for a notification (no content).
-
#jsonrpc_params ⇒ Array, ...
Gets the current JSON-RPC request params object.
-
#jsonrpc_parse_error(data: nil) ⇒ String
Creates a Parse error (-32700) for invalid JSON.
-
#jsonrpc_request ⇒ Request?
Gets the current JSON-RPC request object.
-
#jsonrpc_request? ⇒ Boolean
Checks if the current request is a regular request.
-
#jsonrpc_response(result) ⇒ Array
Creates a JSON-RPC response.
Class Method Details
.included(base) ⇒ void
This method returns an undefined value.
Extends the including class with ClassMethods when module is included
19 20 21 |
# File 'lib/jsonrpc/helpers.rb', line 19 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#env ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Gets the Rack environment hash from @env or the Rails Request
270 271 272 |
# File 'lib/jsonrpc/helpers.rb', line 270 def env @env ||= request.env end |
#jsonrpc_batch ⇒ BatchRequest?
Gets the current JSON-RPC batch request object
90 |
# File 'lib/jsonrpc/helpers.rb', line 90 def jsonrpc_batch = env['jsonrpc.batch'] |
#jsonrpc_batch? ⇒ Boolean
Checks if the current request is a batch request
57 |
# File 'lib/jsonrpc/helpers.rb', line 57 def jsonrpc_batch? = env.key?('jsonrpc.batch') |
#jsonrpc_batch_response(responses) ⇒ Array
Creates a JSON-RPC batch response
140 141 142 143 144 145 |
# File 'lib/jsonrpc/helpers.rb', line 140 def jsonrpc_batch_response(responses) # If batch contained only notifications, responses will be empty or contain only nils return [204, {}, []] if responses.compact.empty? [200, { 'content-type' => 'application/json' }, [responses.to_json]] end |
#jsonrpc_error(error) ⇒ String
Creates a JSON-RPC error response
172 173 174 |
# File 'lib/jsonrpc/helpers.rb', line 172 def jsonrpc_error(error) Response.new(id: jsonrpc_request.id, error: error).to_json end |
#jsonrpc_internal_error(data: nil) ⇒ String
Creates an Internal error (-32603) for server errors
260 261 262 |
# File 'lib/jsonrpc/helpers.rb', line 260 def jsonrpc_internal_error(data: nil) jsonrpc_error(InternalError.new(data: data)) end |
#jsonrpc_invalid_params_error(data: nil) ⇒ String
Creates an Invalid params error (-32602) for invalid parameters
245 246 247 |
# File 'lib/jsonrpc/helpers.rb', line 245 def jsonrpc_invalid_params_error(data: nil) jsonrpc_error(InvalidParamsError.new(data: data)) end |
#jsonrpc_invalid_request_error(data: nil) ⇒ String
Creates an Invalid Request error (-32600) for invalid Request objects
215 216 217 |
# File 'lib/jsonrpc/helpers.rb', line 215 def jsonrpc_invalid_request_error(data: nil) jsonrpc_error(InvalidRequestError.new(data: data)) end |
#jsonrpc_method_not_found_error(data: nil) ⇒ String
Creates a Method not found error (-32601) for missing methods
230 231 232 |
# File 'lib/jsonrpc/helpers.rb', line 230 def jsonrpc_method_not_found_error(data: nil) jsonrpc_error(MethodNotFoundError.new(data: data)) end |
#jsonrpc_notification ⇒ Notification?
Gets the current JSON-RPC notification object
112 |
# File 'lib/jsonrpc/helpers.rb', line 112 def jsonrpc_notification = env['jsonrpc.notification'] |
#jsonrpc_notification? ⇒ Boolean
Checks if the current request is a notification
68 |
# File 'lib/jsonrpc/helpers.rb', line 68 def jsonrpc_notification? = env.key?('jsonrpc.notification') |
#jsonrpc_notification_response ⇒ Array
Creates a response for a notification (no content)
156 157 158 |
# File 'lib/jsonrpc/helpers.rb', line 156 def jsonrpc_notification_response [204, {}, []] end |
#jsonrpc_params ⇒ Array, ...
Gets the current JSON-RPC request params object
185 186 187 |
# File 'lib/jsonrpc/helpers.rb', line 185 def jsonrpc_params jsonrpc_request.params end |
#jsonrpc_parse_error(data: nil) ⇒ String
Creates a Parse error (-32700) for invalid JSON
200 201 202 |
# File 'lib/jsonrpc/helpers.rb', line 200 def jsonrpc_parse_error(data: nil) jsonrpc_error(ParseError.new(data: data)) end |
#jsonrpc_request ⇒ Request?
Gets the current JSON-RPC request object
101 |
# File 'lib/jsonrpc/helpers.rb', line 101 def jsonrpc_request = env['jsonrpc.request'] |
#jsonrpc_request? ⇒ Boolean
Checks if the current request is a regular request
79 |
# File 'lib/jsonrpc/helpers.rb', line 79 def jsonrpc_request? = env.key?('jsonrpc.request') |
#jsonrpc_response(result) ⇒ Array
Creates a JSON-RPC response
125 126 127 |
# File 'lib/jsonrpc/helpers.rb', line 125 def jsonrpc_response(result) [200, { 'content-type' => 'application/json' }, [Response.new(id: jsonrpc_request.id, result: result).to_json]] end |