Sha256: 1fe6a70c3eef7fc72f6765fdf923c712b72ee597456c4843c8b9b701146d5918

Contents?: true

Size: 862 Bytes

Versions: 11

Compression:

Stored size: 862 Bytes

Contents

# Credits for this file go to
# Mikica Ivosevic https://github.com/mikicaivosevic/bitcoin-rpc-ruby/

require 'net/http'
require 'uri'
require 'json'

class BitcoinRPC
  def initialize(service_url)
    @uri = URI.parse(service_url)
  end

  def method_missing(name, *args)
    post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json
    resp = JSON.parse( http_post_request(post_body) )
    raise JSONRPCError, resp['error'].to_s + " (method: '#{name}')" if resp['error']
    resp['result']
  end

  def http_post_request(post_body)
    http    = Net::HTTP.new(@uri.host, @uri.port)
    request = Net::HTTP::Post.new(@uri.request_uri)
    request.basic_auth @uri.user, @uri.password
    request.content_type = 'application/json'
    request.body = post_body
    http.request(request).body
  end

  class JSONRPCError < RuntimeError; end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
txcatcher-0.1.72 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.8 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.71 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.7 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.6 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.5 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.4 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.3 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.2 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.1 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.0 lib/txcatcher/bitcoin_rpc.rb