class Natsy::Config

Represents the configuration options for Natsy. Configuration options are set using the `Natsy::Config::set` method, either as arguments or by using the appropriate setters on the object passed to the block.

Constants

DEFAULT_URL

The default NATS server URL (used if none is configured)

VALID_OPTIONS

Valid option keys that can be given to Natsy::Config::set, either in a Hash passed to the method, keyword arguments passed to the method, or by using setters on the Natsy::Config::Options object passed to the block.

Public Class Methods

as_json(*_args) click to toggle source

Alias for {Natsy::Config::to_h}.

# File lib/natsy/config.rb, line 230
def as_json(*_args)
  to_h
end
default_queue() click to toggle source

The default queue that natsy should use for subscriptions.

See also: {Natsy::Config::Options#default_queue=}

# File lib/natsy/config.rb, line 216
def default_queue
  Utils.presence(given_options[:default_queue])
end
logger() click to toggle source

The logger that natsy should use to write out logs for messages received, responses sent, errors raised, lifecycle events, etc.

See also: {Natsy::Config::Options#logger=}

# File lib/natsy/config.rb, line 208
def logger
  Utils.presence(given_options[:logger])
end
reset!() click to toggle source

Reset the configuration to default values.

# File lib/natsy/config.rb, line 240
def reset!
  @given_options = nil
end
set(keyword_options = {}) { |new_block_options_object| ... } click to toggle source

Specify configuration options, either by providing them as keyword arguments or by using a block. Should you choose to set options using a block, it will be passed a single argument (an instance of Natsy::Config::Options). You can set any options on the instance that you see fit.

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

@example

Natsy::Config.set(
  urls: ["nats://foo.bar:4567", "nats://foo.bar:5678"],
  default_queue: "foobar",
  logger: Rails.logger,
)

@example

Natsy::Config.set do |options|
  options.urls = ["nats://foo.bar:4567", "nats://foo.bar:5678"]
  options.default_queue = "foobar"
  options.logger = Rails.logger
end
# File lib/natsy/config.rb, line 166
def set(keyword_options = {})
  new_hash_options = (keyword_options || {}).transform_keys(&:to_sym)

  invalid_config = lambda do |detail, keys|
    raise InvalidConfigError, "Invalid options provided #{detail}: #{keys.join(', ')}"
  end

  invalid_keys = invalid_option_keys(new_hash_options)
  invalid_config.call("as arguments", invalid_keys) if invalid_keys.any?

  # Want to take advantage of the setters on +Natsy::Config::Options+...
  new_hash_options_object = new_hash_options.each_with_object(Options.new) do |(key, value), options|
    options.send(:"#{key}=", value)
  end

  given_options.merge!(new_hash_options_object.to_h)

  new_block_options_object = Options.new
  yield(new_block_options_object) if block_given?

  invalid_keys = invalid_option_keys(new_block_options_object)
  invalid_config.call("in block", invalid_keys) if invalid_keys.any?

  given_options.merge!(new_block_options_object.to_h)
end
to_h() click to toggle source

Returns all config options as a Hash.

# File lib/natsy/config.rb, line 221
def to_h
  {
    urls: urls,
    logger: logger,
    default_queue: default_queue,
  }
end
to_json(*_args) click to toggle source

Serialize the configuration into a JSON object string.

# File lib/natsy/config.rb, line 235
def to_json(*_args)
  to_h.to_json
end
urls() click to toggle source

The NATS server URLs that natsy should listen on.

See also: {Natsy::Config::Options#urls=}

# File lib/natsy/config.rb, line 196
def urls
  given_url_list = [given_options[:url]].flatten
  given_urls_list = [given_options[:urls]].flatten
  all_given_urls = [*given_url_list, *given_urls_list].compact.uniq
  Utils.presence(all_given_urls) || [DEFAULT_URL]
end

Private Class Methods

given_options() click to toggle source
# File lib/natsy/config.rb, line 246
def given_options
  @given_options ||= {}
end
invalid_option_keys(options) click to toggle source
# File lib/natsy/config.rb, line 250
def invalid_option_keys(options)
  options.to_h.keys - VALID_OPTIONS
end