Class: XRBP::Model::Ledger

Inherits:
Base
  • Object
show all
Extended by:
Base::ClassMethods
Defined in:
lib/xrbp/model/ledger.rb

Instance Attribute Summary collapse

Attributes included from Base::ClassMethods

#connection, #opts

Attributes inherited from Base

#connection, #opts

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base::ClassMethods

set_opts

Methods inherited from Base

#full_opts, #set_opts

Constructor Details

#initialize(opts = {}) ⇒ Ledger

Returns a new instance of Ledger.



8
9
10
11
# File 'lib/xrbp/model/ledger.rb', line 8

def initialize(opts={})
  @id = opts[:id]
  super(opts)
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/xrbp/model/ledger.rb', line 6

def id
  @id
end

Class Method Details

.subscribe(opts = {}, &bl) ⇒ Object

Subscribe to ledger stream via WebSocket::Connection

Parameters:

  • opts (Hash) (defaults to: {})

    options to subscribe to ledger stream with

Options Hash (opts):



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/xrbp/model/ledger.rb', line 28

def self.subscribe(opts={}, &bl)
  set_opts(opts)
  conn = connection
  conn.cmd(WebSocket::Cmds::Subscribe.new(:streams => ["ledger"]), &bl)
  conn.on :message do |*args|
    c,msg = args.size > 1 ? [args[0], args[1]] :
                            [nil,     args[0]]

    begin
      i = JSON.parse(msg.to_s)
      if i["ledger_hash"] &&
         i["ledger_index"]
        if c
          conn.emit :ledger, c, i
        else
          conn.emit :ledger, i
          conn.parent.emit :ledger, conn, i if conn.parent
        end
      end
    rescue
    end
  end
end

Instance Method Details

#sync(opts = {}, &bl) ⇒ Object

Retreive specified ledger via WebSocket::Connection

Parameters:

  • opts (Hash) (defaults to: {})

    options to retrieve ledger with

Options Hash (opts):



18
19
20
21
# File 'lib/xrbp/model/ledger.rb', line 18

def sync(opts={}, &bl)
  set_opts(opts)
  connection.cmd(WebSocket::Cmds::Ledger.new(id, full_opts.except(:id)), &bl)
end