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.



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

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



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/client.rb', line 14

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

	resp = RestClient.post(@url, post_data, @opts)

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

Class Method Details

.make_idObject



4
5
6
# File 'lib/client.rb', line 4

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