Class: SolanaRpcRuby::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/solana_rpc_ruby/api_client.rb

Overview

ApiClient class serves as a client for solana JSON RPC API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster = nil) ⇒ ApiClient

Initialize object with cluster address where requests will be sent.

Parameters:

  • cluster (String) (defaults to: nil)

Raises:

  • (ArgumentError)

18
19
20
21
22
23
# File 'lib/solana_rpc_ruby/api_client.rb', line 18

def initialize(cluster = nil)
  @cluster = cluster || SolanaRpcRuby.cluster

  message = 'Cluster is missing. Please provide default cluster in config or pass it to the client directly.'
  raise ArgumentError, message unless @cluster
end

Instance Attribute Details

#clusterString

Determines which cluster will be used to send requests.

Returns:

  • (String)

9
10
11
# File 'lib/solana_rpc_ruby/api_client.rb', line 9

def cluster
  @cluster
end

#default_headers=(value) ⇒ Hash

Default headers.

Returns:

  • (Hash)

13
14
15
# File 'lib/solana_rpc_ruby/api_client.rb', line 13

def default_headers=(value)
  @default_headers = value
end

Instance Method Details

#call_api(body:, http_method:, params: {}) ⇒ Object

Sends request to the api.

Parameters:

  • body (Hash)
  • http_method (Symbol)
  • params (Hash) (defaults to: {})

Returns:

  • (Object)

    Net::HTTPOK


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/solana_rpc_ruby/api_client.rb', line 32

def call_api(body:, http_method:, params: {})
  uri = URI(@cluster)
  rpc_response = Net::HTTP.public_send(
    http_method,
    uri,
    body,
    default_headers,
  )

  rpc_response

rescue Timeout::Error,
       Net::HTTPError,
       Net::HTTPNotFound,
       Net::HTTPClientException,
       Net::HTTPFatalError,
       Net::ReadTimeout => e
  fail ApiError.new(message: e.message)
rescue StandardError => e

  message = "#{e.class} #{e.message}\n Backtrace: \n #{e.backtrace}"
  fail ApiError.new(message: message)
end