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

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ void

This method returns an undefined value.

Extends the including class with ClassMethods when module is included

Examples:

Include helpers in a class

class MyController
  include JSONRPC::Helpers
end

Parameters:

  • base (Class)

    The class including this module



19
20
21
# File 'lib/jsonrpc/helpers.rb', line 19

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#envHash

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

Returns:

  • (Hash)

    the Rack environment hash



270
271
272
# File 'lib/jsonrpc/helpers.rb', line 270

def env
  @env ||= request.env
end

#jsonrpc_batchBatchRequest?

Gets the current JSON-RPC batch request object

Examples:

Get current batch

batch = jsonrpc_batch

Returns:



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

Examples:

Check if request is batch

jsonrpc_batch? # => true/false

Returns:

  • (Boolean)

    true if current request is a batch



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

Examples:

Create a batch response

jsonrpc_batch_response([response1, response2]) # => [200, headers, body]

Parameters:

  • responses (Array)

    array of responses

Returns:

  • (Array)

    Rack response tuple



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

Examples:

Create error response

error = JSONRPC::Error.new(code: -32600, message: "Invalid Request")
jsonrpc_error(error) # => JSON string

Parameters:

  • error (Error)

    the error object

Returns:

  • (String)

    JSON-formatted 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

Examples:

Create internal error

jsonrpc_internal_error # => JSON error response

Parameters:

  • data (Object, nil) (defaults to: nil)

    additional error data

Returns:

  • (String)

    JSON-formatted error response



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

Examples:

Create invalid params error

jsonrpc_invalid_params_error # => JSON error response

Parameters:

  • data (Object, nil) (defaults to: nil)

    additional error data

Returns:

  • (String)

    JSON-formatted error response



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

Examples:

Create invalid request error

jsonrpc_invalid_request_error # => JSON error response

Parameters:

  • data (Object, nil) (defaults to: nil)

    additional error data

Returns:

  • (String)

    JSON-formatted error response



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

Examples:

Create method not found error

jsonrpc_method_not_found_error # => JSON error response

Parameters:

  • data (Object, nil) (defaults to: nil)

    additional error data

Returns:

  • (String)

    JSON-formatted error response



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_notificationNotification?

Gets the current JSON-RPC notification object

Examples:

Get current notification

notification = jsonrpc_notification

Returns:



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

Examples:

Check if request is notification

jsonrpc_notification? # => true/false

Returns:

  • (Boolean)

    true if current request is a notification



68
# File 'lib/jsonrpc/helpers.rb', line 68

def jsonrpc_notification? = env.key?('jsonrpc.notification')

#jsonrpc_notification_responseArray

Creates a response for a notification (no content)

Examples:

Create notification response

jsonrpc_notification_response # => [204, {}, []]

Returns:

  • (Array)

    Rack response tuple with 204 status



156
157
158
# File 'lib/jsonrpc/helpers.rb', line 156

def jsonrpc_notification_response
  [204, {}, []]
end

#jsonrpc_paramsArray, ...

Gets the current JSON-RPC request params object

Examples:

Get request parameters

params = jsonrpc_params # => [1, 2, 3] or {"a": 1, "b": 2}

Returns:

  • (Array, Hash, nil)

    the request parameters



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

Examples:

Create parse error

jsonrpc_parse_error # => JSON error response

Parameters:

  • data (Object, nil) (defaults to: nil)

    additional error data

Returns:

  • (String)

    JSON-formatted error response



200
201
202
# File 'lib/jsonrpc/helpers.rb', line 200

def jsonrpc_parse_error(data: nil)
  jsonrpc_error(ParseError.new(data: data))
end

#jsonrpc_requestRequest?

Gets the current JSON-RPC request object

Examples:

Get current request

request = jsonrpc_request

Returns:

  • (Request, nil)

    the current request or nil



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

Examples:

Check if request is regular request

jsonrpc_request? # => true/false

Returns:

  • (Boolean)

    true if 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

Examples:

Create a successful response

jsonrpc_response(42) # => [200, headers, body]

Parameters:

  • result (Object)

    the result to return

Returns:

  • (Array)

    Rack response tuple



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