Class: Rubyists::Leopard::NatsServiceDiscovery::Operation::Connect
- Inherits:
-
Trailblazer::Operation
- Object
- Trailblazer::Operation
- Rubyists::Leopard::NatsServiceDiscovery::Operation::Connect
- Defined in:
- lib/leopard/nats_service_discovery/operation/connect.rb
Overview
Opens a NATS client connection for discovery operations.
On success, the result exposes :client.
On failure, the result exposes :error.
Instance Method Summary collapse
-
#connect(ctx) ⇒ Boolean
Connects to NATS using
:serveror:nats_url. -
#connection_url(ctx) ⇒ String
private
Resolves the NATS URL from the operation context.
-
#default_url ⇒ String
private
Default NATS URL used when no URL is provided.
Instance Method Details
#connect(ctx) ⇒ Boolean
Connects to NATS using :server or :nats_url.
23 24 25 26 27 28 29 30 |
# File 'lib/leopard/nats_service_discovery/operation/connect.rb', line 23 def connect(ctx, **) opts = (ctx[:connect_options] || {}).dup opts[:user_credentials] = ctx[:creds] if ctx[:creds] ctx[:client] = NATS.connect(connection_url(ctx), opts) rescue StandardError => e ctx[:error] = e false end |
#connection_url(ctx) ⇒ String (private)
Resolves the NATS URL from the operation context.
46 47 48 |
# File 'lib/leopard/nats_service_discovery/operation/connect.rb', line 46 def connection_url(ctx) ctx[:nats_url] || ctx[:server] || default_url end |
#default_url ⇒ String (private)
Default NATS URL used when no URL is provided.
37 38 39 |
# File 'lib/leopard/nats_service_discovery/operation/connect.rb', line 37 def default_url ENV.fetch('NATS_URL', 'nats://127.0.0.1:4222') end |