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
TIMEOUT =
60.freeze

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Client

Returns a new instance of Client.



23
24
25
26
27
28
29
30
# File 'lib/coinrpc.rb', line 23

def initialize(url)

  urinfo = URI.parse(url)

  @options = {:timeout => TIMEOUT, :userpwd => "#{urinfo.user}:#{urinfo.password}", :headers => {"User-Agent" => "CoinRPC/#{CoinRPC::VERSION}"}}.freeze
  @url = "http://#{urinfo.host}:#{urinfo.port}/".freeze
  
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/coinrpc.rb', line 36

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 => random_id} }
  else
    post_data = {:method => fixed_method, :params => args, :jsonrpc => JSONRPC_V1_1, :id => random_id}
  end
  
  api_call(post_data)
  
end

Instance Method Details

#random_idObject



32
33
34
# File 'lib/coinrpc.rb', line 32

def random_id
  SecureRandom.hex(4).to_i(16)
end