Class: Bitcoiner::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bitcoiner/client.rb

Defined Under Namespace

Classes: JSONRPCError

Constant Summary collapse

DEFAULT_ID =
'jsonrpc'.freeze
LOG_PREFIX =
"[bitcoiner]".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, pass, host, logger: nil) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
# File 'lib/bitcoiner/client.rb', line 11

def initialize(user, pass, host, logger: nil)
  uri = Addressable::URI.heuristic_parse(host)
  self.username = uri.user || user
  self.password = uri.password || pass
  uri.user = uri.password = nil

  self.endpoint = uri.to_s

  self.logger = logger
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



9
10
11
# File 'lib/bitcoiner/client.rb', line 9

def endpoint
  @endpoint
end

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/bitcoiner/client.rb', line 9

def logger
  @logger
end

#passwordObject

Returns the value of attribute password.



9
10
11
# File 'lib/bitcoiner/client.rb', line 9

def password
  @password
end

#usernameObject

Returns the value of attribute username.



9
10
11
# File 'lib/bitcoiner/client.rb', line 9

def username
  @username
end

Instance Method Details

#accountsObject



26
27
28
29
# File 'lib/bitcoiner/client.rb', line 26

def accounts
  balance_hash = request 'listaccounts'
  AccountHash.new self, balance_hash
end

#balanceObject



22
23
24
# File 'lib/bitcoiner/client.rb', line 22

def balance
  request 'getbalance'
end

#inspectObject



41
42
43
# File 'lib/bitcoiner/client.rb', line 41

def inspect
  "#<Bitcoiner::Client #{endpoint.inspect} #{username}:#{password} >"
end

#request(method_or_array_of_methods, *args) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/bitcoiner/client.rb', line 31

def request(method_or_array_of_methods, *args)
  if method_or_array_of_methods.is_a?(Array)
    log("#{method_or_array_of_methods}")
    batch_request(method_or_array_of_methods)
  else
    log("#{method_or_array_of_methods}; args: #{args.inspect}")
    single_request(method_or_array_of_methods, *args)
  end
end