class BitcoindRPC::Connection

Constants

CONTENT_TYPE
DEFAULT_OPTIONS

Attributes

options[R]
uri[R]

Public Class Methods

new(opts) click to toggle source

Creates a new connection to a bitcoind

@param [Hash] opts Connection parameters @option opts [String] :host @option opts [String] :port @option opts [String] :username @option opts [String] :password @option opts [String,URI] :uri Specify a complete URI instead of separate host, port etc

# File lib/bitcoind_rpc/connection.rb, line 24
def initialize(opts)
  @options = DEFAULT_OPTIONS.dup.merge(opts.dup)
  @uri = @options[:uri] ? URI(@options[:uri]) : URI(uri_to_s)
end

Public Instance Methods

method_missing(name, *args) click to toggle source
Calls superclass method
# File lib/bitcoind_rpc/connection.rb, line 33
def method_missing(name, *args)
  return request(name, *args) if supported_methods.include?(name.to_sym)
  super
end
request(name, *args) click to toggle source
# File lib/bitcoind_rpc/connection.rb, line 38
def request(name, *args)
  BitcoindRPC.logger.debug "> #{name}: #{args.join(',')}"
  response = request_http_post(name, args)
  BitcoindRPC.logger.debug "< #{response.code} #{response.message}"
  raise Error, response.message unless (200...300).cover?(response.code.to_i)
  begin
    response = Oj.load(response.body, symbol_keys: true, bigdecimal_load: true)
  rescue StandardError => e
    BitcoindRPC.logger.warn "Failed to parse JSON response: #{e}"
    raise
  end
  raise Error, response[:error] if response[:error]
  response[:result]
end
respond_to_missing?(name, _include_all = false) click to toggle source
Calls superclass method
# File lib/bitcoind_rpc/connection.rb, line 29
def respond_to_missing?(name, _include_all = false)
  supported_methods.include?(name.to_sym) || super
end
supported_methods() click to toggle source

Makes a request to a bitcoind instance once and returns the list of supported RPC methods

# File lib/bitcoind_rpc/connection.rb, line 55
def supported_methods
  return @supported_methods if @supported_methods
  help_response = request(:help)
  mm = help_response.split("\n").select { |l| l =~ /^\w+(\s|$)/ }
  @supported_methods = mm.map { |l| l.split(' ').first }.map(&:to_sym)
end

Private Instance Methods

request_body(name, params) click to toggle source
# File lib/bitcoind_rpc/connection.rb, line 79
def request_body(name, params)
  Oj.dump({ method: name, params: params, id: 'jsonrpc' }, mode: :compat)
end
request_http_post(name, params) click to toggle source
# File lib/bitcoind_rpc/connection.rb, line 68
def request_http_post(name, params)
  username = uri.user
  password = uri.password
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.basic_auth(username, password)
  request.body = request_body(name, params)
  request['Content-Type'] = CONTENT_TYPE
  http.request(request)
end
uri_to_s() click to toggle source
# File lib/bitcoind_rpc/connection.rb, line 64
def uri_to_s
  "http://#{options[:username]}:#{options[:password]}@#{options[:host]}:#{options[:port]}"
end