Class: CoinRPC::Client

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

Constant Summary collapse

JSONRPC_V1_1 =
"1.1".freeze
JSONRPC_V2_0 =
"2.0".freeze

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
# File 'lib/coinrpc.rb', line 15

def initialize(url)

  urinfo = URI.parse(url)

  @client = HTTP.persistent("http://#{urinfo.host}:#{urinfo.port}").timeout(60).basic_auth({:user => urinfo.user, :pass => urinfo.password})
  @id = rand(1000000)

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/coinrpc.rb', line 24

def method_missing(method, *args)

  fixed_method = method.to_s.gsub(/\_/,"").freeze
  post_data = nil
  
  if args[0].is_a?(Array) and args[0].size > 0 then
    # batch request
    post_data = args.map{|arg| {:jsonrpc => JSONRPC_V2_0, :method => fixed_method, :params => arg, :id => (@id += 1)} }
  else
    post_data = {:method => fixed_method, :params => args, :jsonrpc => JSONRPC_V1_1, :id => (@id += 1)}
  end
  
  api_call(post_data)
  
end