lib/bitcoiner/client.rb in bitcoiner-0.1.2 vs lib/bitcoiner/client.rb in bitcoiner-0.1.3

- old
+ new

@@ -1,15 +1,17 @@ # frozen_string_literal: true module Bitcoiner class Client - attr_accessor :endpoint + attr_accessor :endpoint, :username, :password def initialize(user, pass, host) uri = Addressable::URI.heuristic_parse(host) - uri.user ||= user - uri.password ||= pass + self.username = uri.user || user + self.password = uri.password || pass + uri.user = uri.password = nil + self.endpoint = uri.to_s end def balance request 'getbalance' @@ -19,18 +21,28 @@ balance_hash = request 'listaccounts' AccountHash.new self, balance_hash end def request(method, *args) - post_body = { 'method' => method, 'params' => args, 'id' => 'jsonrpc' }.to_json - response = Typhoeus.post(endpoint, body: post_body) + post_body = { + 'method' => method, + 'params' => args, + 'id' => 'jsonrpc' + }.to_json + + response = Typhoeus.post( + endpoint, + userpwd: [username, password].join(":"), + body: post_body, + ) + response_hash = parse_body(response) raise JSONRPCError, response_hash['error'] if response_hash['error'] response_hash['result'] end def inspect - "#<Bitcoiner::Client #{endpoint.inspect} >" + "#<Bitcoiner::Client #{endpoint.inspect} #{username}:#{password} >" end class JSONRPCError < RuntimeError; end private