Class: StartcoinClient::RPC

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RPC

Returns a new instance of RPC.



4
5
6
7
8
9
# File 'lib/startcoin_client/rpc.rb', line 4

def initialize(options)
  @user, @pass = options[:user], options[:pass]
  @host, @port = options[:host], options[:port]
  @ssl, @proxy = options[:ssl], options[:proxy]
  @verify_ssl  = options[:verify_ssl]
end

Instance Method Details

#credentialsObject



11
12
13
14
15
16
17
# File 'lib/startcoin_client/rpc.rb', line 11

def credentials
  if @user
    "#{@user}:#{@pass}"
  else
    nil
  end
end

#dispatch(request) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/startcoin_client/rpc.rb', line 26

def dispatch(request)
  process_response = lambda do |respdata, _request, _result|
    response = JSON.parse(respdata)
    raise StartcoinClient::Errors::RPCError, response['error'] if response['error']
    response['result']
  end
  RestClient::Request.execute(method: :post,
                              url: service_url,
                              payload: request.to_post_data,
                              proxy: @proxy,
                              verify_ssl: false,
                              headers: { content_type: :json },
                              &process_response)
end

#service_urlObject



19
20
21
22
23
24
# File 'lib/startcoin_client/rpc.rb', line 19

def service_url
  url = @ssl ? "https://" : "http://"
  url.concat "#{credentials}@" if c = credentials
  url.concat "#{@host}:#{@port}"
  url
end