Class: WaffleAPI::Client

Inherits:
Object
  • Object
show all
Includes:
AddressValidator, Helpers
Defined in:
lib/waffle_api/client.rb

Overview

A Ruby class to call the Waffle stat API. You might use this if you want to retrieve your waffle pool stats from within a Ruby program.

Example:

require 'waffle_api'
stats = WaffleAPI::Client.new address: '<BTC_ADDRESS_HERE>'

Constant Summary

Constants included from Helpers

Helpers::INVALIDATION_TIME, Helpers::NOT_FOUND_ERROR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#call_uri, #data_recent!, #data_recent?, #request_json, #stats

Constructor Details

#initialize(address: nil) ⇒ Client

Returns a new instance of Client.



22
23
24
25
26
27
# File 'lib/waffle_api/client.rb', line 22

def initialize(address: nil)
  @address   = address || ENV['BTC_ADDRESS']

  fail Error::EmptyAddress if @address.nil? || @address.empty?
  fail Error::BadAddress, @address unless valid_address?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

This is for not yet suported keys



65
66
67
68
69
# File 'lib/waffle_api/client.rb', line 65

def method_missing(method, *args, &block)
  stats method.to_s
rescue Error::UnknownKey
  super method, *args, &block
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



20
21
22
# File 'lib/waffle_api/client.rb', line 20

def address
  @address
end

Instance Method Details

#balancesObject



44
45
46
47
48
49
50
51
52
# File 'lib/waffle_api/client.rb', line 44

def balances
  balances = stats 'balances'

  WaffleAPI::Balances.new(
    sent: balances['sent'],
    confirmed: balances['confirmed'],
    unconverted: balances['unconverted']
  )
end

#hashrateObject



29
30
31
# File 'lib/waffle_api/client.rb', line 29

def hashrate
  stats 'hash_rate'
end

#paymentsObject



54
55
56
57
58
59
60
61
62
# File 'lib/waffle_api/client.rb', line 54

def payments
  stats('recent_payments').map do |payment|
    WaffleAPI::Payment.new(
      amount: payment['amount'],
      paid_at: payment['time'],
      transaction_hash: payment['txn']
    )
  end
end

#workersObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/waffle_api/client.rb', line 33

def workers
  stats('worker_hashrates').map do |worker|
    WaffleAPI::Worker.new(
      name: worker['username'],
      hash_rate: worker['hashrate'],
      stale_rate: worker['stalerate'],
      last_seen: worker['last_seen']
    )
  end
end