Class: Solana::Ruby::Kit::Rpc::Transport

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/solana/ruby/kit/rpc/transport.rb

Overview

HTTP transport for Solana’s JSON-RPC API. Mirrors TypeScript’s ‘createHttpTransport(config)` from @solana/rpc-transport-http.

Makes synchronous POST requests — TypeScript’s async fetch maps to Ruby’s blocking Net::HTTP. Use threads or Fibers for concurrency.

Constant Summary collapse

DEFAULT_TIMEOUT =

seconds

T.let(30, Integer)
DEFAULT_OPEN_TIMEOUT =

seconds

T.let(10, Integer)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, headers: {}, timeout: DEFAULT_TIMEOUT, open_timeout: DEFAULT_OPEN_TIMEOUT, to_json: nil, from_json: nil) ⇒ Transport

Returns a new instance of Transport.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/solana/ruby/kit/rpc/transport.rb', line 67

def initialize(
  url:,
  headers:      {},
  timeout:      DEFAULT_TIMEOUT,
  open_timeout: DEFAULT_OPEN_TIMEOUT,
  to_json:      nil,
  from_json:    nil
)
  @url          = T.let(url, String)
  @headers      = T.let(headers, T::Hash[String, String])
  @timeout      = T.let(timeout, Integer)
  @open_timeout = T.let(open_timeout, Integer)
  @to_json      = T.let(to_json, T.nilable(T.proc.params(arg0: T.untyped).returns(String)))
  @from_json    = T.let(from_json, T.nilable(T.proc.params(arg0: String, arg1: T.untyped).returns(T.untyped)))
  @request_id   = T.let(0, Integer)
  @uri          = T.let(URI.parse(url), URI::Generic)
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



55
56
57
# File 'lib/solana/ruby/kit/rpc/transport.rb', line 55

def url
  @url
end

Instance Method Details

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/solana/ruby/kit/rpc/transport.rb', line 88

def request(method, params = [])
  payload = build_payload(method, params)
  body    = @to_json ? @to_json.call(payload) : JSON.generate(payload)

  response = post(body)

  parsed = @from_json ? @from_json.call(T.must(response.body), payload) : JSON.parse(T.must(response.body))

  if parsed.key?('error')
    err = parsed['error']
    raise RpcError.new(err['code'].to_i, err['message'].to_s, err['data'])
  end

  parsed['result']
end