Class: Platon::HttpClient

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

Constant Summary

Constants inherited from Client

Client::DEFAULT_GAS_LIMIT, Client::DEFAULT_GAS_PRICE, Client::PLATON_RPC_COMMANDS, Client::RPC_COMMANDS, Client::RPC_MANAGEMENT_COMMANDS

Instance Attribute Summary collapse

Attributes inherited from Client

#chain_id, #command, #default_account, #gas_limit, #gas_price, #hrp, #id, #logger, #ppos

Instance Method Summary collapse

Methods inherited from Client

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

Constructor Details

#initialize(host, chain_name, proxy = nil) ⇒ HttpClient

Returns a new instance of HttpClient.

Raises:

  • (ArgumentError)


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

def initialize(host,chain_name, proxy = nil)
  super(chain_name)
  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/platon/http_client.rb', line 5

def host
  @host
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

#proxyObject

Returns the value of attribute proxy.



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

def proxy
  @proxy
end

#sslObject

Returns the value of attribute ssl.



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

def ssl
  @ssl
end

#uriObject

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#send_batch(batch) ⇒ Object



48
49
50
51
52
# File 'lib/platon/http_client.rb', line 48

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/platon/http_client.rb', line 30

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

#update_setting(params) ⇒ Object

:host,:proxy, :chain_id, :hrp



19
20
21
22
23
24
25
26
27
28
# File 'lib/platon/http_client.rb', line 19

def update_setting(params)  # :host,:proxy, :chain_id, :hrp
  if params[:host]
    uri = URI.parse(params[:host])
    raise ArgumentError unless ['http', 'https'].include? uri.scheme
    @host = uri.host
    @port = uri.port
  end
  @proxy = proxy if params[:proxy]
  super(params)
end