Class: XRBP::Model::Account

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

Constant Summary collapse

DATE_FORMAT =
"%Y-%m-%dT%H:%M:%SZ"

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 = {}) ⇒ Account

Initialize new Account instance

Parameters:

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

    options to initialize account with

Options Hash (opts):

  • :id (String)

    id of account to use to retrieve account


151
152
153
154
# File 'lib/xrbp/model/account.rb', line 151

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

Instance Attribute Details

#idObject

Returns the value of attribute id.


12
13
14
# File 'lib/xrbp/model/account.rb', line 12

def id
  @id
end

Class Method Details

.all(opts = {}) ⇒ Object

Retrieve all accounts via WebClient::Connection

Parameters:

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

    options to retrieve accounts with

Options Hash (opts):


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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

.cacheObject

Local data cache location


15
16
17
# File 'lib/xrbp/model/account.rb', line 15

def self.cache
  @cache ||= File.expand_path("~/.xrbp/accounts/")
end

.cachedObject

All cached accounts


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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 = {}) ⇒ Object

Retrieve latest account using specified WebClient::Connection

Parameters:

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

    options to retrieve account with

Options Hash (opts):


132
133
134
135
136
137
138
139
140
141
142
# 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

Instance Method Details

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

Retrieve account info via WebSocket::Connection

Parameters:

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

    options to retrieve account info with

Options Hash (opts):


161
162
163
164
# 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) ⇒ Object

Retrieve account objects via WebSocket::Connection

Parameters:

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

    options to retrieve account objects with

Options Hash (opts):


171
172
173
174
# 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) ⇒ Object

Retrieve account username via WebClient::Connection

Parameters:

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

    options to retrieve account username with

Options Hash (opts):


181
182
183
184
185
186
187
188
189
# 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