Class: Natsy::Config::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/natsy/config.rb

Overview

A Natsy::Config::Options object is passed as a single argument to the block (if provided) for the Natsy::Config::set method. This class should probably NOT be instantiated directly; instead, set the relevant options using Natsy::Config.set(some_option: “…”, some_other_option: “…”). If you find yourself instantiating this class directly, there’s probably a better way to do what you’re trying to do.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_queueObject

Set a default queue for subscriptions.

NOTE: The following two examples do exactly the same thing.

Leave the ::default_queue blank (or assign nil) to use no default queue.

NOTE: The following two examples do exactly the same thing.

Examples:

Natsy::Config.set(default_queue: "foobar")
Natsy::Config.set do |options|
  options.default_queue = "foobar"
end
Natsy::Config.set(default_queue: nil)
Natsy::Config.set do |options|
  options.default_queue = nil
end


109
110
111
# File 'lib/natsy/config.rb', line 109

def default_queue
  @default_queue
end

#loggerObject

Attach a logger to have natsy write out logs for messages received, responses sent, errors raised, lifecycle events, etc.

NOTE: The following two examples do exactly the same thing.

In a Rails application, you might do this instead:

Examples:

require 'natsy'
require 'logger'

nats_logger = Logger.new(STDOUT)
nats_logger.level = Logger::INFO

Natsy::Config.set(logger: nats_logger)
require 'natsy'
require 'logger'

Natsy::Config.set do |options|
  nats_logger = Logger.new(STDOUT)
  nats_logger.level = Logger::INFO

  options.logger = nats_logger
end
Natsy::Config.set(logger: Rails.logger)


76
77
78
# File 'lib/natsy/config.rb', line 76

def logger
  @logger
end

#urlObject

Specify a NATS server URL (or multiple URLs)

NOTE: The following two examples do exactly the same thing.

NOTE: The following two examples do exactly the same thing.

If left blank/omitted, natsy will fall back on the default URL, which is nats://localhost:4222.

Examples:

Natsy::Config.set(url: "nats://foo.bar:4567"))
Natsy::Config.set do |options|
  options.url = "nats://foo.bar:4567"
end
Natsy::Config.set(urls: ["nats://foo.bar:4567", "nats://foo.bar:5678"])
Natsy::Config.set do |options|
  options.urls = ["nats://foo.bar:4567", "nats://foo.bar:5678"]
end


43
44
45
# File 'lib/natsy/config.rb', line 43

def url
  @url
end

#urlsObject

Specify a NATS server URL (or multiple URLs)

NOTE: The following two examples do exactly the same thing.

NOTE: The following two examples do exactly the same thing.

If left blank/omitted, natsy will fall back on the default URL, which is nats://localhost:4222.

Examples:

Natsy::Config.set(url: "nats://foo.bar:4567"))
Natsy::Config.set do |options|
  options.url = "nats://foo.bar:4567"
end
Natsy::Config.set(urls: ["nats://foo.bar:4567", "nats://foo.bar:5678"])
Natsy::Config.set do |options|
  options.urls = ["nats://foo.bar:4567", "nats://foo.bar:5678"]
end


43
44
45
# File 'lib/natsy/config.rb', line 43

def urls
  @urls
end

Instance Method Details

#to_hObject

Returns ONLY the config options THAT HAVE BEEN SET as a Hash. Will not have keys for properties that are unassigned, but will have keys for properties assigned nil.



120
121
122
123
124
125
126
127
# File 'lib/natsy/config.rb', line 120

def to_h
  hash = {}
  hash[:url] = url if defined?(@url)
  hash[:urls] = urls if defined?(@urls)
  hash[:logger] = logger if defined?(@logger)
  hash[:default_queue] = default_queue if defined?(@default_queue)
  hash
end