Module: XRBP::DSL

Defined in:
lib/xrbp/dsl.rb,
lib/xrbp/dsl/ledgers.rb,
lib/xrbp/dsl/accounts.rb,
lib/xrbp/dsl/webclient.rb,
lib/xrbp/dsl/websocket.rb,
lib/xrbp/dsl/validators.rb

Overview

The DSL namespace can be included in client logic to provide an easy-to-use mechanism to read and write XRP data.

Examples:

Retrieve ledger, subscribe to updates

include XRBP::DSL

puts "Genesis ledger: "
puts ledger(32570)

websocket_msg do |msg|
  puts "Ledger received:"
  puts msg
end

subscribe_to_ledgers

Instance Method Summary collapse

Instance Method Details

#account_info(id) ⇒ Hash?

Return info for the specified account id

Parameters:

  • id (String)

    account id to query

Returns:

  • (Hash, nil)

    the account info or nil otherwise


7
8
9
10
11
# File 'lib/xrbp/dsl/accounts.rb', line 7

def (id)
  websocket.add_plugin :autoconnect unless websocket.plugin?(:autoconnect)
  websocket.add_plugin :command_dispatcher unless websocket.plugin?(:command_dispatcher)
  websocket.cmd(WebSocket::Cmds::AccountInfo.new(id))
end

#ledger(id = nil) ⇒ Hash?

Return ledger object for the specified id

Parameters:

  • id (Integer) (defaults to: nil)

    id of the ledger to query

Returns:

  • (Hash, nil)

    the ledger object retrieved or nil otherwise


7
8
9
10
11
# File 'lib/xrbp/dsl/ledgers.rb', line 7

def ledger(id=nil)
  websocket.add_plugin :autoconnect unless websocket.plugin?(:autoconnect)
  websocket.add_plugin :command_dispatcher unless websocket.plugin?(:command_dispatcher)
  websocket.cmd(WebSocket::Cmds::Ledger.new(id))
end

#subscribe_to_ledgersObject

Subscribed to the ledger stream.

After calling this, :ledger events will be emitted via the websocket connection object.


17
18
19
20
21
# File 'lib/xrbp/dsl/ledgers.rb', line 17

def subscribe_to_ledgers
  websocket.add_plugin :autoconnect unless websocket.plugin?(:autoconnect)
  websocket.add_plugin :command_dispatcher unless websocket.plugin?(:command_dispatcher)
  websocket.cmd(WebSocket::Cmds::Subscribe.new(:streams => ["ledger"]))
end

#validatorsArray<Hash>

Return list of all validators

Returns:

  • (Array<Hash>)

    list of validators retrieved


6
7
8
# File 'lib/xrbp/dsl/validators.rb', line 6

def validators
  Model::Validator.all :connection => webclient
end

#webclientObject

Client which may be used to access HTTP resources


4
5
6
# File 'lib/xrbp/dsl/webclient.rb', line 4

def webclient
  @webclient ||= WebClient::Connection.new
end

#websocketObject

Client which may be used to access websocket endpoints.

By default a RoundRobin strategy will be used to cycle through specified endpoints.


13
14
15
# File 'lib/xrbp/dsl/websocket.rb', line 13

def websocket
  @websocket ||= WebSocket::RoundRobin.new *websocket_endpoints
end

#websocket_endpointsObject

Default websocket endpoints. Override to specify different ones.


5
6
7
# File 'lib/xrbp/dsl/websocket.rb', line 5

def websocket_endpoints
  ["wss://s1.ripple.com:443", "wss://s2.ripple.com:443"]
end

#websocket_msg(&bl) ⇒ Object

Register a callback to be invoked when messages are received via websocket connections


19
20
21
# File 'lib/xrbp/dsl/websocket.rb', line 19

def websocket_msg(&bl)
  websocket.on :message, &bl
end

#websocket_waitObject

Block until all websocket connections are closed


24
25
26
# File 'lib/xrbp/dsl/websocket.rb', line 24

def websocket_wait
  websocket.wait_for_close
end