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
ENDPOINTS_WITH_ARRAY_ARGS =
['testmempoolaccept'].to_set

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Client

Returns a new instance of Client.



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

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



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

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 and !ENDPOINTS_WITH_ARRAY_ARGS.member?(fixed_method) 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



34
35
36
# File 'lib/coinrpc.rb', line 34

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