Module: JSONRPC::RPCObject

Defined in:
lib/jsonrpc/rpc_object.rb

Overview

Since:

  • 0.1.0

Class Method Summary collapse

Class Method Details

.detailed_internal_error(exception, request_id: nil) ⇒ JSONRPC::ErrorResponse

Parameters:

  • exception (Exception)
  • request_id (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (request_id:):

  • (String, NilClass)

Returns:

Since:

  • 0.1.0



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/jsonrpc/rpc_object.rb', line 155

def detailed_internal_error(exception, request_id: nil)
  internal_error(
    error_context: {
      error_class: exception.class,
      error_message: exception.message,
      error_backtrace: exception.backtrace,
      error_object: exception
    },
    request_id:
  )
end

.error(data = {}, code: JSONRPC::ERRORS[:application_error][:code], message: JSONRPC::ERRORS[:application_error][:message], request_id: nil) ⇒ JSONRPC::ErrorResponse Also known as: application_error

Parameters:

  • data (Hash<String|Symbol,Any>) (defaults to: {})
  • code (Hash) (defaults to: JSONRPC::ERRORS[:application_error][:code])

    a customizable set of options

  • message (Hash) (defaults to: JSONRPC::ERRORS[:application_error][:message])

    a customizable set of options

  • request_id (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (code:):

  • (Integer)

Options Hash (message:):

  • (String)

Options Hash (request_id:):

  • (String, NilClass)

Returns:

Since:

  • 0.1.0



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jsonrpc/rpc_object.rb', line 29

def error(
  data = {},
  code: JSONRPC::ERRORS[:application_error][:code],
  message: JSONRPC::ERRORS[:application_error][:message],
  request_id: nil
)
  JSONRPC::ErrorResponse.new(
    jsonrpc: '2.0',
    error: { code:, message:, data: },
    id: request_id
  )
end

.internal_error(*error_codes, error_context: {}, request_id: nil) ⇒ JSONRPC::ErrorResponse

Parameters:

  • error_codes (*Array<String,Symbol>)
  • error_context (Hash) (defaults to: {})

    a customizable set of options

  • request_id (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (error_context:):

  • (Hash<String|Symbol,Any>)

Options Hash (request_id:):

  • (String, NilClass)

Returns:

Since:

  • 0.1.0



140
141
142
143
144
145
146
147
# File 'lib/jsonrpc/rpc_object.rb', line 140

def internal_error(*error_codes, error_context: {}, request_id: nil)
  error(
    { error_codes:, error_context: },
    code: JSONRPC::ERRORS[:internal_error][:code],
    message: JSONRPC::ERRORS[:internal_error][:message],
    request_id:
  )
end

.invalid_params_error(data = {}, request_id: nil) ⇒ JSONRPC::ErrorResponse

Parameters:

  • data (Hash<String|Symbol,Any>) (defaults to: {})
  • request_id (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (request_id:):

  • (String, NilClass)

Returns:

Since:

  • 0.1.0



124
125
126
127
128
129
130
131
# File 'lib/jsonrpc/rpc_object.rb', line 124

def invalid_params_error(data = {}, request_id: nil)
  error(
    data,
    code: JSONRPC::ERRORS[:invalid_params][:code],
    message: JSONRPC::ERRORS[:invalid_params][:message],
    request_id:
  )
end

.invalid_request_error(data = {}, request_id: nil) ⇒ JSONRPC::ErrorResponse

Parameters:

  • data (Hash) (defaults to: {})
  • request_id (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (request_id:):

  • (String, NilClass)

Returns:

Since:

  • 0.1.0



72
73
74
75
76
77
78
79
# File 'lib/jsonrpc/rpc_object.rb', line 72

def invalid_request_error(data = {}, request_id: nil)
  error(
    data,
    code: JSONRPC::ERRORS[:invalid_request][:code],
    message: JSONRPC::ERRORS[:invalid_request][:message],
    request_id:
  )
end

.jsonrpc_specification_violation_error(request_id: nil) ⇒ JSONRPC::ErrorResponse

Parameters:

  • request_id (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (request_id:):

  • (String, NilClass)

Returns:

Since:

  • 0.1.0



97
98
99
100
101
102
103
# File 'lib/jsonrpc/rpc_object.rb', line 97

def jsonrpc_specification_violation_error(request_id: nil)
  error(
    code: JSONRPC::ERRORS[:jsonrpc_specification_violation][:code],
    message: JSONRPC::ERRORS[:jsonrpc_specification_violation][:message],
    request_id:
  )
end

.method_not_found_error(request_id: nil) ⇒ JSONRPC::ErrorResponse

Parameters:

  • request_id (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (request_id:):

  • (String, NilClass)

Returns:

Since:

  • 0.1.0



110
111
112
113
114
115
116
# File 'lib/jsonrpc/rpc_object.rb', line 110

def method_not_found_error(request_id: nil)
  error(
    code: JSONRPC::ERRORS[:method_not_found][:code],
    message: JSONRPC::ERRORS[:method_not_found][:message],
    request_id:
  )
end

.notification(jsonrpc: '2.0', method:, params:) ⇒ JSONRPC::Notification

Parameters:

  • jsonrpc (Hash) (defaults to: '2.0')

    a customizable set of options

  • method (Hash)

    a customizable set of options

  • params (Hash)

    a customizable set of options

Options Hash (jsonrpc:):

  • (String)

Options Hash (method:):

  • (String)

Options Hash (params:):

  • (Hash<Symbol,Any>)

Returns:

Since:

  • 0.1.0



62
63
64
# File 'lib/jsonrpc/rpc_object.rb', line 62

def notification(jsonrpc: '2.0', method:, params:)
  JSONRPC::Notification.new(jsonrpc:, method:, params:)
end

.parse_errorJSONRPC::ErrorResponse

Returns:

Since:

  • 0.1.0



85
86
87
88
89
90
# File 'lib/jsonrpc/rpc_object.rb', line 85

def parse_error
  error(
    code: JSONRPC::ERRORS[:parse_error][:code],
    message: JSONRPC::ERRORS[:parse_error][:message]
  )
end

.request(jsonrpc: '2.0', method:, params:, id:) ⇒ JSONRPC::Request

Parameters:

  • jsonrpc (Hash) (defaults to: '2.0')

    a customizable set of options

  • method (Hash)

    a customizable set of options

  • params (Hash)

    a customizable set of options

  • id (Hash)

    a customizable set of options

Options Hash (jsonrpc:):

  • (String)

Options Hash (method:):

  • (String)

Options Hash (params:):

  • (Hash<String|Symbol,Any>)

Options Hash (id:):

  • (String)

Returns:

Since:

  • 0.1.0



51
52
53
# File 'lib/jsonrpc/rpc_object.rb', line 51

def request(jsonrpc: '2.0', method:, params:, id:)
  JSONRPC::Request.new(jsonrpc:, method:, params:, id:)
end

.response(result = {}, request_id: nil) ⇒ JSONRPC::Response, NilClass

Parameters:

  • result (Hash<String|Symbol,Any>) (defaults to: {})
  • request_id (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (request_id:):

  • (String, NilClass)

Returns:

Since:

  • 0.1.0



14
15
16
17
18
19
# File 'lib/jsonrpc/rpc_object.rb', line 14

def response(result = {}, request_id: nil)
  return if request_id == nil # NOTE: nil is treated as no result

  # @type var request_id: untyped
  JSONRPC::Response.new(jsonrpc: '2.0', result:, id: request_id)
end