Class: JetstreamBridge::Core::ConnectionFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/jetstream_bridge/core/connection_factory.rb

Overview

Factory for creating and managing NATS connections

Defined Under Namespace

Classes: ConnectionOptions

Class Method Summary collapse

Class Method Details

.build_options(config = JetstreamBridge.config) ⇒ Object

Create connection options from config



65
66
67
68
69
70
71
72
73
# File 'lib/jetstream_bridge/core/connection_factory.rb', line 65

def build_options(config = JetstreamBridge.config)
  servers = config.nats_urls
  raise ConnectionNotEstablishedError, 'No NATS URLs configured' if servers.to_s.strip.empty?

  ConnectionOptions.new(
    servers: servers,
    name: config.app_name
  )
end

.create_client(options = nil) ⇒ Object

Create a new NATS client



76
77
78
79
80
81
# File 'lib/jetstream_bridge/core/connection_factory.rb', line 76

def create_client(options = nil)
  opts = options || build_options
  client = NATS::IO::Client.new
  client.connect(opts.to_h)
  client
end

.create_jetstream(client) ⇒ Object

Create JetStream context with health monitoring



84
85
86
87
88
89
90
91
# File 'lib/jetstream_bridge/core/connection_factory.rb', line 84

def create_jetstream(client)
  jts = client.jetstream

  # Ensure JetStream responds to #nc
  jts.define_singleton_method(:nc) { client } unless jts.respond_to?(:nc)

  jts
end