-
Notifications
You must be signed in to change notification settings - Fork 8
/
request_factory.rb
37 lines (33 loc) · 991 Bytes
/
request_factory.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
37
class RequestFactory
def initialize(connection_options)
@url = connection_options['url']
@user = connection_options['user']
@pw = connection_options['pw']
@counter = 0
@connection = Faraday.new(:url => @url) do |faraday|
# faraday.response :logger
faraday.basic_auth(@user, @pw)
faraday.adapter Faraday.default_adapter
end
end
def request(method, params = nil)
@counter += 1
body = {
'jsonrpc' => "2.0",
'id' => @counter,
'method' => method,
}
body.merge!('params' => params) unless params.nil?
response = @connection.post do |req|
req.url '/jsonrpc.php'
req.headers['Content-Type'] = 'application/json'
req.body = body.to_json
end
parsed_body = JSON.parse(response.body)
if parsed_body['result']
return parsed_body['result']
else
raise "Error processing the request #{method} (#{params.inspect}): #{parsed_body['error'].inspect}"
end
end
end