Class: Zold::WTS

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

Overview

WTS gateway.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2018-2020 Yegor Bugayenko

License

MIT

Defined Under Namespace

Classes: Fake

Instance Method Summary collapse

Constructor Details

#initialize(key, log: Loog::NULL) ⇒ WTS

Makes a new object of the class. The key you are supposed to obtain at this page: wts.zold.io/api. You will have to login and confirm your account first. Keep this key secret, to avoid information lost. However, even knowing the secret no payments can be sent without they keygap.



74
75
76
77
78
79
# File 'lib/zold/wts.rb', line 74

def initialize(key, log: Loog::NULL)
  raise 'Key can\'t be nil' if key.nil?
  @key = key
  raise 'Log can\'t be nil' if log.nil?
  @log = log
end

Instance Method Details

#balanceObject

Get wallet balance.



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/zold/wts.rb', line 102

def balance
  start = Time.now
  http = clean(
    Typhoeus::Request.get(
      'https://wts.zold.io/balance',
      headers: headers
    )
  )
  balance = Zold::Amount.new(zents: http.body.to_i)
  @log.debug("The balance #{balance} retrieved in #{Zold::Age.new(start)}")
  balance
end

#find(query) ⇒ Object

Find transactions by the criteria. All criterias are regular expressions and their summary result is concatenated by OR. For example, this request will return all transactions that have “pizza” in details OR which are coming from the root wallet:

find(details: /pizza/, bnf: '0000000000000000')

The method returns an array of Zold::Txn objects.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/zold/wts.rb', line 165

def find(query)
  start = Time.now
  http = clean(
    Typhoeus::Request.get(
      'https://wts.zold.io/find?' + query.map do |k, v|
        "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
      end.join('&'),
      headers: headers
    )
  )
  txns = http.body.split("\n").map { |t| Zold::Txn.parse(t) }
  @log.debug("#{txns.count} transactions found in #{Zold::Age.new(start)}")
  txns
end

#idObject

Get wallet ID.



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/zold/wts.rb', line 116

def id
  start = Time.now
  http = clean(
    Typhoeus::Request.get(
      'https://wts.zold.io/id',
      headers: headers
    )
  )
  id = Zold::Id.new(http.body.to_s)
  @log.debug("Wallet ID #{id} retrieved in #{Zold::Age.new(start)}")
  id
end

#pay(keygap, bnf, amount, details) ⇒ Object

Initiate PAY request. The keygap is a string you get when you confirm the account. The bnf is the name of the GitHub account, the wallet ID or the invoice (up to you). The amount is the amount in ZLD, e.g. “19.99”. The details is the text to add to the transaction.

The method returns the job ID, which you should wait for completion using the method wait().



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/zold/wts.rb', line 137

def pay(keygap, bnf, amount, details)
  start = Time.now
  job = job_of(
    clean(
      Typhoeus::Request.post(
        'https://wts.zold.io/do-pay',
        headers: headers,
        body: { keygap: keygap, bnf: bnf, amount: amount, details: details }
      )
    )
  )
  @log.debug("PAY job #{job} started in #{Zold::Age.new(start)}")
  job
end

#pullObject

Initiate PULL request. The server will pull your wallet form the network, and make it ready for future requests. Without this operation you won’t be able to perform find() or balance() requests.

The method returns the job ID, which you should wait for completion using the method wait().



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/zold/wts.rb', line 87

def pull
  start = Time.now
  job = job_of(
    clean(
      Typhoeus::Request.get(
        'https://wts.zold.io/pull',
        headers: headers
      )
    )
  )
  @log.debug("PULL job #{job} started in #{Zold::Age.new(start)}")
  job
end

#usd_rateObject

Returns current USD rate of one ZLD.



153
154
155
# File 'lib/zold/wts.rb', line 153

def usd_rate
  clean(Typhoeus::Request.get('https://wts.zold.io/usd_rate')).body.to_f
end

#wait(job, time: 5 * 60) ⇒ Object

Wait for the job to complete. If the job completes successfully, the method returns ‘OK’ in a few seconds (up to a few minutes). If the is some error, the exception will be raised.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/zold/wts.rb', line 183

def wait(job, time: 5 * 60)
  start = Time.now
  loop do
    if Time.now - start > time
      raise "Can't wait any longer for the job #{job} to complete"
    end
    http = Typhoeus::Request.get(
      'https://wts.zold.io/job?id=' + job,
      headers: headers
    )
    raise "Job #{job} not found on the server" if http.code == 404
    raise "Unpredictable response code #{http.code}" unless http.code == 200
    if http.body == 'Running'
      @log.debug("Job #{job} is still running, \
#{Zold::Age.new(start)} already...")
      sleep 1
      next
    end
    raise http.body unless http.body == 'OK'
    @log.debug("Job #{job} completed, in #{Zold::Age.new(start)}")
    return http.body
  end
end