-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonrpc.rb
36 lines (26 loc) · 887 Bytes
/
jsonrpc.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
33
34
35
36
require 'net/http'
require 'addressable/uri'
require 'json'
require File.expand_path(File.join(File.dirname(__FILE__), 'jsonrpc', 'version'))
require File.expand_path(File.join(File.dirname(__FILE__), 'jsonrpc', 'exceptions'))
module JsonRPC
class Client
@@id = 0
def initialize(url)
@address = Addressable::URI.parse(url)
end
def request(method, params = [])
@@id += 1 # increment id for each request
result = {}
h = {"Content-Type" => "application/json"}
Net::HTTP.start(@address.host, @address.port) do |connection|
json = {:method => method.to_s, :params => params, id: @@id, version: "1.0"}.to_json
result = JSON.parse(connection.post(@address.path, json, h).body)
end
if error = result["error"]
raise JsonRPCError, result
end
result
end
end
end