forked from osyoyu/monakuji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonacoin_rpc.rb
32 lines (27 loc) · 883 Bytes
/
monacoin_rpc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require 'net/http'
require 'uri'
require 'json'
class MonacoinRPC
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'] 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
if __FILE__ == $0
wallet = MonacoinRPC.new('http://monacoinrpc:[email protected]:10010')
p wallet.getbalance
end