Class: Protobuf::Nats::Config

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

Constant Summary collapse

CONFIG_MUTEX =
::Mutex.new
DEFAULTS =
{
  :connect_timeout => nil,
  :max_reconnect_attempts => 60_000,
  :servers => nil,
  :tls_client_cert => nil,
  :tls_client_key => nil,
  :tls_ca_cert => nil,
  :uses_tls => false,
  :server_subscription_key_do_not_subscribe_to_when_includes_any_of => [],
  :server_subscription_key_only_subscribe_to_when_includes_any_of => [],
  :subscription_key_replacements => [],
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



28
29
30
31
32
# File 'lib/protobuf/nats/config.rb', line 28

def initialize
  DEFAULTS.each_pair do |key, value|
    __send__("#{key}=", value)
  end
end

Instance Attribute Details

#connect_timeoutObject

Returns the value of attribute connect_timeout.



8
9
10
# File 'lib/protobuf/nats/config.rb', line 8

def connect_timeout
  @connect_timeout
end

#max_reconnect_attemptsObject

Returns the value of attribute max_reconnect_attempts.



8
9
10
# File 'lib/protobuf/nats/config.rb', line 8

def max_reconnect_attempts
  @max_reconnect_attempts
end

#server_subscription_key_do_not_subscribe_to_when_includes_any_ofObject

Returns the value of attribute server_subscription_key_do_not_subscribe_to_when_includes_any_of.



9
10
11
# File 'lib/protobuf/nats/config.rb', line 9

def server_subscription_key_do_not_subscribe_to_when_includes_any_of
  @server_subscription_key_do_not_subscribe_to_when_includes_any_of
end

#server_subscription_key_only_subscribe_to_when_includes_any_ofObject

Returns the value of attribute server_subscription_key_only_subscribe_to_when_includes_any_of.



9
10
11
# File 'lib/protobuf/nats/config.rb', line 9

def server_subscription_key_only_subscribe_to_when_includes_any_of
  @server_subscription_key_only_subscribe_to_when_includes_any_of
end

#serversObject

Returns the value of attribute servers.



8
9
10
# File 'lib/protobuf/nats/config.rb', line 8

def servers
  @servers
end

#subscription_key_replacementsObject

Returns the value of attribute subscription_key_replacements.



9
10
11
# File 'lib/protobuf/nats/config.rb', line 9

def subscription_key_replacements
  @subscription_key_replacements
end

#tls_ca_certObject

Returns the value of attribute tls_ca_cert.



8
9
10
# File 'lib/protobuf/nats/config.rb', line 8

def tls_ca_cert
  @tls_ca_cert
end

#tls_client_certObject

Returns the value of attribute tls_client_cert.



8
9
10
# File 'lib/protobuf/nats/config.rb', line 8

def tls_client_cert
  @tls_client_cert
end

#tls_client_keyObject

Returns the value of attribute tls_client_key.



8
9
10
# File 'lib/protobuf/nats/config.rb', line 8

def tls_client_key
  @tls_client_key
end

#uses_tlsObject

Returns the value of attribute uses_tls.



8
9
10
# File 'lib/protobuf/nats/config.rb', line 8

def uses_tls
  @uses_tls
end

Instance Method Details

#connection_options(reload = false) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/protobuf/nats/config.rb', line 66

def connection_options(reload = false)
  @connection_options = false if reload
  @connection_options ||= begin
    options = {
      servers: servers,
      max_reconnect_attempts: max_reconnect_attempts,
      uses_tls: uses_tls,
      tls_client_cert: tls_client_cert,
      tls_client_key: tls_client_key,
      tls_ca_cert: tls_ca_cert,
      connect_timeout: connect_timeout,
      server_subscription_key_do_not_subscribe_to_when_includes_any_of: server_subscription_key_do_not_subscribe_to_when_includes_any_of,
      server_subscription_key_only_subscribe_to_when_includes_any_of: server_subscription_key_only_subscribe_to_when_includes_any_of,
      subscription_key_replacements: subscription_key_replacements,
    }
    options[:tls] = {:context => new_tls_context} if uses_tls
    options
  end
end

#load_from_yml(reload = false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/protobuf/nats/config.rb', line 34

def load_from_yml(reload = false)
  CONFIG_MUTEX.synchronize do
    @load_from_yml = nil if reload
    @load_from_yml ||= begin
      env = ENV["RAILS_ENV"] || ENV["RACK_ENV"] || ENV["APP_ENV"] || "development"

      yaml_config = {}
      config_path = ENV["PROTOBUF_NATS_CONFIG_PATH"] || ::File.join("config", "protobuf_nats.yml")
      absolute_config_path = ::File.expand_path(config_path)
      if ::File.exist?(absolute_config_path)
        yaml_string = ::ERB.new(::File.read(absolute_config_path)).result
        # Psych 4 and newer requires unsafe_load_file in order for aliases to be used
        yaml_config = if ::YAML.respond_to?(:unsafe_load_file)
          ::YAML.unsafe_load(yaml_string)[env]
        else
          ::YAML.load(yaml_string)[env]
        end
      end

      DEFAULTS.each_pair do |key, value|
        setting = yaml_config[key.to_s]
        __send__("#{key}=", setting) if setting
      end

      # Reload the connection options hash
      connection_options(true)

      true
    end
  end
end

#make_subscription_key_replacements(subscription_key) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/protobuf/nats/config.rb', line 94

def make_subscription_key_replacements(subscription_key)
  subscription_key_replacements.each do |replacement|
    match = replacement.keys.first
    replacement = replacement[match]

    if subscription_key.include?(match)
      return subscription_key.gsub(match, replacement)
    end
  end

  subscription_key
end

#new_tls_contextObject



86
87
88
89
90
91
92
# File 'lib/protobuf/nats/config.rb', line 86

def new_tls_context
  tls_context = ::OpenSSL::SSL::SSLContext.new
  tls_context.ssl_version = :TLSv1_2
  tls_context.cert = ::OpenSSL::X509::Certificate.new(::File.read(tls_client_cert)) if tls_client_cert
  tls_context.key = ::OpenSSL::PKey::RSA.new(::File.read(tls_client_key)) if tls_client_key
  tls_context
end