Sha256: 649eaa86c3c13a8859e69c4d3c62f13a4820961c34219fdefc4a98de71b48ed6

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module Bitcoiner
  class Client
    attr_accessor :endpoint, :username, :password

    def initialize(user, pass, host)
      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
    end

    def balance
      request 'getbalance'
    end

    def accounts
      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,
        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} #{username}:#{password} >"
    end

    class JSONRPCError < RuntimeError; end

    private

    def parse_body(response)
      if response.success?
        JSON.parse(response.body)
      else
        error_message = %i[code return_code].map do |attr|
          "#{attr}: `#{response.send(attr)}`"
        end.join(', ')
        raise JSONRPCError, "unsuccessful response; #{error_message}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bitcoiner-0.1.3 lib/bitcoiner/client.rb