Class: Rubyists::Leopard::NatsServiceDiscovery::Operation::Connect

Inherits:
Trailblazer::Operation
  • Object
show all
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

Instance Method Details

#connect(ctx) ⇒ Boolean

Connects to NATS using :server or :nats_url.

Parameters:

  • ctx (Hash)

    Operation context.

Returns:

  • (Boolean)

    Whether the connection was opened.



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.

Parameters:

  • ctx (Hash)

    Operation context.

Returns:

  • (String)


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_urlString (private)

Default NATS URL used when no URL is provided.

Returns:

  • (String)


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