Class: SolanaRuby::HttpClient

Inherits:
BaseClient show all
Defined in:
lib/solana_ruby/http_client.rb

Constant Summary collapse

BASE_URL =
'https://api.mainnet-beta.solana.com'

Instance Method Summary collapse

Constructor Details

#initialize(endpoint = BASE_URL) ⇒ HttpClient

Returns a new instance of HttpClient.



20
21
22
# File 'lib/solana_ruby/http_client.rb', line 20

def initialize(endpoint = BASE_URL)
  @uri = URI.parse(endpoint)
end

Instance Method Details

#request(method, params = []) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/solana_ruby/http_client.rb', line 24

def request(method, params = [])
  http = Net::HTTP.new(@uri.host, @uri.port)
  local_hosts = ['localhost', '127.0.0.1', '[::1]']
  http.use_ssl = true unless local_hosts.include?(@uri.host.downcase)

  request = Net::HTTP::Post.new(@uri.request_uri, {'Content-Type' => 'application/json'})
  request.body = {
    jsonrpc: '2.0',
    id: 1,
    method: method,
    params: params
  }.to_json

  response = http.request(request)
  handle_http_response(response)
rescue StandardError => e
  handle_error(e)
end