module BPS::STAN

Constants

CLIENT_OPTS

Public Class Methods

coercer() click to toggle source

@return [BPS::Coercer] the options coercer

# File lib/bps/stan.rb, line 54
def self.coercer
  @coercer ||= BPS::Coercer.new(CLIENT_OPTS).freeze
end
connect(cluster_id, client_id, nats: {}, **opts) click to toggle source

@param [String] cluster ID @param [String] client ID @param [Hash] options @return [STAN::Client] connected STAN client

# File lib/bps/stan.rb, line 39
def self.connect(cluster_id, client_id, nats: {}, **opts)
  # handle TLS if CA file is provided:
  if !nats[:tls] && nats[:tls_ca_file]
    ctx = OpenSSL::SSL::SSLContext.new
    ctx.set_params
    ctx.ca_file = nats.delete(:tls_ca_file)
    nats[:tls] = ctx
  end

  client = ::STAN::Client.new
  client.connect(cluster_id, client_id, nats: nats, **opts.slice(*CLIENT_OPTS.keys))
  client
end
parse_url(url) click to toggle source

@return [Array] arguments for connecting to STAN

# File lib/bps/stan.rb, line 59
def self.parse_url(url)
  port = url.port&.to_s || '4222'
  servers = CGI.unescape(url.host).split(',').map do |host|
    addr = "nats://#{host}"
    addr << ':' << port unless /:\d+$/.match?(addr)
    addr
  end
  opts = CGI.parse(url.query || '').transform_values {|v| v.size == 1 ? v[0] : v }
  cluster_id = opts.delete('cluster_id')
  client_id = opts.delete('client_id')
  [cluster_id, client_id, { nats: { servers: servers } }]
end