Class: ERPC::RpcNamespace

Inherits:
Object
  • Object
show all
Defined in:
lib/erpc_sdk/rpc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport, methods, parameter_mode:, batch_policy: :any) ⇒ RpcNamespace

Returns a new instance of RpcNamespace.



200
201
202
203
204
205
206
207
# File 'lib/erpc_sdk/rpc.rb', line 200

def initialize(transport, methods, parameter_mode:, batch_policy: :any)
  @transport = transport
  @endpoint = transport.endpoint
  @methods = methods.to_h { |method| [method, true] }.freeze
  @aliases = methods.to_h { |method| [snake_case(method), method] }.freeze
  @parameter_mode = parameter_mode
  @batch_policy = batch_policy
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, &block) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/erpc_sdk/rpc.rb', line 239

def method_missing(name, *arguments, &block)
  return super if block

  string_name = name.to_s
  method = @methods.key?(string_name) ? string_name : @aliases[string_name]
  return super unless method

  params = if @parameter_mode == :positional
             arguments
           elsif arguments.empty?
             nil
           elsif arguments.length == 1 && arguments.first.is_a?(Hash)
             arguments.first
           else
             raise ConfigError, "named RPC methods require one Hash argument"
           end
  request(method, params)
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



198
199
200
# File 'lib/erpc_sdk/rpc.rb', line 198

def endpoint
  @endpoint
end

Instance Method Details

#batch(calls) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/erpc_sdk/rpc.rb', line 223

def batch(calls)
  if @batch_policy == :unsupported && !calls.empty?
    raise BatchPolicyError, "Leader RPC methods do not support batching"
  end
  if @batch_policy == :solana_standard
    methods = calls.map { |call| call[:method] || call["method"] }
    has_heavy = methods.any? { |method| HEAVY_SOLANA_METHODS.include?(method) }
    has_standard = methods.any? { |method| !HEAVY_SOLANA_METHODS.include?(method) }
    if has_heavy && has_standard
      raise BatchPolicyError, "Solana indexed and standard RPC methods cannot share a batch"
    end
  end

  PendingRpcBatchRequest.new(@transport, calls)
end

#raw(method, params = nil, decoder: nil) ⇒ Object

Raises:



217
218
219
220
221
# File 'lib/erpc_sdk/rpc.rb', line 217

def raw(method, params = nil, decoder: nil)
  raise ConfigError, "method must not be empty" if method.to_s.empty?

  PendingRpcRequest.new(@transport, method, params, decoder || ->(value) { value })
end

#request(method, params = nil, decoder: nil) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/erpc_sdk/rpc.rb', line 209

def request(method, params = nil, decoder: nil)
  unless @methods.key?(method)
    raise ConfigError, "#{method.inspect} is not in this namespace; use raw for forward-compatible methods"
  end

  PendingRpcRequest.new(@transport, method, params, decoder || ->(value) { value })
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


258
259
260
261
# File 'lib/erpc_sdk/rpc.rb', line 258

def respond_to_missing?(name, include_private = false)
  string_name = name.to_s
  @methods.key?(string_name) || @aliases.key?(string_name) || super
end