Sha256: 933682afbbec26fd2cb99e79a379e499f5b9f0991d6c72c636dc83ab5c5cdaa8

Contents?: true

Size: 975 Bytes

Versions: 28

Compression:

Stored size: 975 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.new(resp['error'].merge({ "method" => name.to_s })) 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
    attr_accessor :data
    def initialize(data)
      super(data.to_s)
      self.data = data
    end
  end

end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
txcatcher-0.1.100 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.99 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.98 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.97 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.96 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.95 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.94 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.93 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.92 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.91 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.90 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.89 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.88 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.87 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.86 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.85 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.84 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.83 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.82 lib/txcatcher/bitcoin_rpc.rb
txcatcher-0.1.81 lib/txcatcher/bitcoin_rpc.rb