Class: Habeel::JsonRPC::Client

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
# File 'lib/client.rb', line 13

def initialize(url, opts = {})
	@url = url
	@opts = opts || {}
	@opts[:content_type] = "application/json"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/client.rb', line 19

def method_missing(method, *args)
	post_data = {
		:jsonrpc => '2.0',
		:method => method,
		:params => args,
		:id => self.class.make_id
	}.to_json

	resp = RestClient::Request.execute(:method => :post, :url => @url, :payload => post_data, :headers => @opts, :timeout => 300, :open_timeout => 300)

	body = JSON.parse(resp.body)
	return body['result']
end

Class Method Details

.make_idObject



9
10
11
# File 'lib/client.rb', line 9

def self.make_id
	rand(10**12)
end