Class: BitcoinRPC

Inherits:
Object
  • Object
show all
Defined in:
lib/txcatcher/bitcoin_rpc.rb

Defined Under Namespace

Classes: JSONRPCError

Instance Method Summary collapse

Constructor Details

#initialize(service_url) ⇒ BitcoinRPC

Returns a new instance of BitcoinRPC.



10
11
12
# File 'lib/txcatcher/bitcoin_rpc.rb', line 10

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Raises:



14
15
16
17
18
19
# File 'lib/txcatcher/bitcoin_rpc.rb', line 14

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

Instance Method Details

#http_post_request(post_body) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/txcatcher/bitcoin_rpc.rb', line 21

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