class XRBP::Model::Account

Constants

DATE_FORMAT

Attributes

id[RW]

Public Class Methods

all(opts={}) click to toggle source

Retrieve all accounts via WebClient::Connection

@param opts [Hash] options to retrieve accounts with @option opts [WebClient::Connection] :connection Connection

to use to retrieve accounts
# File lib/xrbp/model/account.rb, line 55
def self.all(opts={})
  set_opts(opts)
  FileUtils.mkdir_p(cache) unless File.exist?(cache)

  cached.each { |acct|
    break if connection.force_quit?
    connection.emit :account, acct
  } if opts[:replay]

  # start at last marker
  marker = File.exist?("#{cache}/marker") ?
             File.read("#{cache}/marker") : nil
  marker = nil if marker&.strip&.empty?

  # load start time, if set
  start = File.exist?("#{cache}/start") ?
    DateTime.parse(File.read("#{cache}/start")) :
     GENESIS_TIME

  # Parse results
  connection.add_plugin :result_parser       unless connection.plugin?(:result_parser)
  connection.add_plugin Parsers::AccountInfo unless connection.plugin?(Parsers::AccountInfo)

  # Retrieve data until complete
  accounts = []
  finished = false
  until finished || connection.force_quit?
    # HTTP request
    connection.url = "https://data.ripple.com/v2/accounts/?"\
                       "start=#{start.strftime(DATE_FORMAT)}&"\
                       "limit=1000&marker=#{marker}"
    res = connection.perform
    break if connection.force_quit?
    break unless res[:accounts] && !res[:accounts].empty?

    page = marker || start.strftime("%Y%m%d%H%M%S")

    # Cache data
    cache_file = "#{cache}/#{page}"
    File.write(cache_file, res[:accounts].to_json)

    # Emit signal
    res[:accounts].each { |acct|
      break if connection.force_quit?
      connection.emit :account, acct
      # TODO yield account
    }

    break if connection.force_quit?

    marker = res[:marker]
    accounts += res[:accounts]

    # Store state, eval exit condition
    File.write("#{cache}/marker", marker.to_s)
    finished = !marker

    connection.emit :account_page, page
  end

  # Store state for next run
  # FIXME: results in an overlap, accounts created
  #        at this inception will also be retrieved
  #        during next run
  File.write("#{cache}/start",
             accounts.last[:inception]) unless connection.force_quit? ||
                                                      accounts.empty? ||
                                                              marker

  accounts
end
cache() click to toggle source

Local data cache location

# File lib/xrbp/model/account.rb, line 15
def self.cache
  @cache ||= File.expand_path("~/.xrbp/accounts/")
end
cached() click to toggle source

All cached accounts

# File lib/xrbp/model/account.rb, line 20
def self.cached
  Dir.glob("#{cache}/*").sort.collect { |f|
    next nil if f == "#{cache}marker" ||
                f == "#{cache}start"
    begin
      JSON.parse(File.read(f))
          .collect { |acct|
            # convert string keys to symbols
            Hash[acct.map { |k,v| [k.intern, v] }]
          }
    rescue
      nil
    end
  }.flatten.compact
end
latest(opts={}) click to toggle source

Retrieve latest account using specified WebClient::Connection

@param opts [Hash] options to retrieve account with @option opts [WebClient::Connection] :connection Connection

to use to retrieve account
# File lib/xrbp/model/account.rb, line 132
def self.latest(opts={})
  set_opts(opts)

  connection.add_plugin :result_parser       unless connection.plugin?(:result_parser)
  connection.add_plugin Parsers::AccountInfo unless connection.plugin?(Parsers::AccountInfo)

  connection.url = "https://data.ripple.com/v2/accounts/?"\
                     "descending=true&limit=1000"
  res = connection.perform
  res[:accounts].first
end
new(opts={}) click to toggle source

Initialize new Account instance

@param opts [Hash] options to initialize account with @option opts [String] :id id of account

to use to retrieve account
Calls superclass method
# File lib/xrbp/model/account.rb, line 151
def initialize(opts={})
  @id = opts[:id]
  super(opts)
end

Public Instance Methods

info(opts={}, &bl) click to toggle source

Retrieve account info via WebSocket::Connection

@param opts [Hash] options to retrieve account info with @option opts [WebSocket::Connection] :connection Connection

to use to retrieve account info
# File lib/xrbp/model/account.rb, line 161
def info(opts={}, &bl)
  set_opts(opts)
  connection.cmd(WebSocket::Cmds::AccountInfo.new(id, full_opts.except(:id)), &bl)
end
objects(opts={}, &bl) click to toggle source

Retrieve account objects via WebSocket::Connection

@param opts [Hash] options to retrieve account objects with @option opts [WebSocket::Connection] :connection Connection

to use to retrieve account objects
# File lib/xrbp/model/account.rb, line 171
def objects(opts={}, &bl)
  set_opts(opts)
  connection.cmd(WebSocket::Cmds::AccountObjects.new(id, full_opts.except(:id)), &bl)
end
username(opts={}, &bl) click to toggle source

Retrieve account username via WebClient::Connection

@param opts [Hash] options to retrieve account username with @option opts [WebClient::Connection] :connection Connection

to use to retrieve account username
# File lib/xrbp/model/account.rb, line 181
def username(opts={}, &bl)
  set_opts(opts)
  connection.url = "https://id.ripple.com/v1/authinfo?username=#{id}"

  connection.add_plugin :result_parser           unless connection.plugin?(:result_parser)
  connection.add_plugin Parsers::AccountUsername unless connection.plugin?(Parsers::AccountUsername)

  connection.perform
end