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



70
71
72
73
74
75
76
77
78
# File 'lib/jetstream_bridge/core/connection_factory.rb', line 70

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}-#{config.env}"
  )
end

.create_client(options = nil) ⇒ Object

Create a new NATS client



81
82
83
84
85
86
# File 'lib/jetstream_bridge/core/connection_factory.rb', line 81

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



89
90
91
92
93
94
95
96
# File 'lib/jetstream_bridge/core/connection_factory.rb', line 89

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