Class: EvmClient::HttpClient

Inherits:
Client
  • Object
show all
Defined in:
lib/evm_client/http_client.rb

Constant Summary

Constants inherited from Client

Client::DEFAULT_BLOCK_NUMBER, Client::DEFAULT_GAS_LIMIT, Client::DEFAULT_GAS_PRICE, Client::RPC_COMMANDS, Client::RPC_MANAGEMENT_COMMANDS

Instance Attribute Summary collapse

Attributes inherited from Client

#block_number, #command, #default_account, #gas_limit, #gas_price, #id, #log, #logger

Instance Method Summary collapse

Methods inherited from Client

#batch, create, #encode_params, #get_balance, #get_chain, #get_id, #get_nonce, #int_to_hex, #reset_id, #send_command, #transfer, #transfer_and_wait, #transfer_to, #transfer_to_and_wait, #wait_for

Constructor Details

#initialize(host, proxy = nil, log = false) ⇒ HttpClient

Returns a new instance of HttpClient.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/evm_client/http_client.rb', line 7

def initialize(host, proxy = nil, log = false)
  super(log)
  uri = URI.parse(host)
  raise ArgumentError unless ['http', 'https'].include? uri.scheme
  @host = uri.host
  @port = uri.port
  @proxy = proxy
  
  @ssl = uri.scheme == 'https'
  @uri = URI("#{uri.scheme}://#{@host}:#{@port}#{uri.path}")
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



5
6
7
# File 'lib/evm_client/http_client.rb', line 5

def host
  @host
end

#portObject

Returns the value of attribute port.



5
6
7
# File 'lib/evm_client/http_client.rb', line 5

def port
  @port
end

#proxyObject

Returns the value of attribute proxy.



5
6
7
# File 'lib/evm_client/http_client.rb', line 5

def proxy
  @proxy
end

#sslObject

Returns the value of attribute ssl.



5
6
7
# File 'lib/evm_client/http_client.rb', line 5

def ssl
  @ssl
end

#uriObject

Returns the value of attribute uri.



5
6
7
# File 'lib/evm_client/http_client.rb', line 5

def uri
  @uri
end

Instance Method Details

#send_batch(batch) ⇒ Object



37
38
39
40
41
# File 'lib/evm_client/http_client.rb', line 37

def send_batch(batch)
  result = send_single(batch.to_json)
  result = JSON.parse(result)
  result.sort_by! { |c| c['id'] }
end

#send_single(payload) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/evm_client/http_client.rb', line 19

def send_single(payload)
  if @proxy.present?
    _, p_username, p_password, p_host, p_port = @proxy.gsub(/(:|\/|@)/,' ').squeeze(' ').split
    http = ::Net::HTTP.new(@host, @port, p_host, p_port, p_username, p_password)
  else
    http = ::Net::HTTP.new(@host, @port)
  end

  if @ssl
    http.use_ssl = true
  end
  header = {'Content-Type' => 'application/json'}
  request = ::Net::HTTP::Post.new(uri, header)
  request.body = payload
  response = http.request(request)
  response.body
end