Class: NanoRpcClient

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

Overview

A simple client for sending data to the RPC server. This is a low-level API; use NanoAccount instead.

Direct Known Subclasses

NanodeClient

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint) ⇒ NanoRpcClient

Create a new client. Params:

endpoint

The location of the RPC server.



13
14
15
16
# File 'lib/clients.rb', line 13

def initialize(endpoint)
    @dispatcher = HTTP 
    @endpoint = endpoint
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(action, **kwargs) ⇒ Object

Missing methods will make requests to the RPC server using the method name as the action. The rest of the keyword arguments are passed as JSON data. Params:

action

The action field of the RPC request.

kwargs

The keyword arguments passed to the method.



36
37
38
39
40
41
# File 'lib/clients.rb', line 36

def method_missing(action, **kwargs)
    kwargs["action"] = action.to_s

    # Stringify all keys
    req(kwargs.map{|k, v| [k.to_s, v]}.to_h)
end

Class Method Details

.mrai_to_raw(mrai) ⇒ Object

Convert an amount of MRai to a raw Nano quantity. Params:

mrai

The quantity of MRai.

Returns: The quantity of raw Nano, stringified.



48
49
50
# File 'lib/clients.rb', line 48

def self.mrai_to_raw(mrai)
    BigDecimal(mrai) * BigDecimal("1000000000000000000000000000000")
end

Instance Method Details

#req(data) ⇒ Object

Make a request to the endpoint. Params:

data

A Hash to send to the RPC server.

Returns: The object returned by the server as a Hash.

Raises:



23
24
25
26
27
28
29
# File 'lib/clients.rb', line 23

def req(data)
    res = @dispatcher.post(@endpoint, :json => data).parse

    raise RpcError, res["error"] if res["error"] != nil

    res
end