Class: Surfliner::Mq::ConnectionConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/surfliner/mq/connection_config.rb

Overview

An object encapsulating RabbitMQ configuration.

Constant Summary collapse

FALSE_VALUES =

Returns values of RABBITMQ_AWAIT_ON_CLOSE interpreted as false.

Returns:

  • (Array<String>)

    values of RABBITMQ_AWAIT_ON_CLOSE interpreted as false

([""] + %w[0 off Off OFF f false False FALSE F n no No NO N]).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:, username:, password:, await_response_on_close: true, **opts) ⇒ ConnectionConfig

Initializes a new MqConfig object.

Parameters:

  • host (String)

    RabbitMQ hostname

  • port (String)

    RabbitMQ AMQP port

  • username (String)

    RabbitMQ username

  • password (String)

    RabbitMQ passsword

  • await_response_on_close (Boolean, String) (defaults to: true)

    whether the RabbitMQ client should wait for a response when closing the connection

  • opts (Hash)

    additional RabbitMQ conection options (see Bunny::Session#initialize)



33
34
35
36
37
38
39
40
# File 'lib/surfliner/mq/connection_config.rb', line 33

def initialize(host:, port:, username:, password:, await_response_on_close: true, **opts)
  @host = host
  @port = port
  @username = username
  @password = password
  @await_response_on_close = parse_boolean(await_response_on_close)
  @opts = opts
end

Instance Attribute Details

#await_response_on_closeBoolean (readonly)

Returns whether the RabbitMQ client should wait for a response when closing the connection.

Returns:

  • (Boolean)

    whether the RabbitMQ client should wait for a response when closing the connection



21
22
23
# File 'lib/surfliner/mq/connection_config.rb', line 21

def await_response_on_close
  @await_response_on_close
end

#hostString (readonly)

Returns The RabbitMQ hostname.

Returns:

  • (String)

    The RabbitMQ hostname



9
10
11
# File 'lib/surfliner/mq/connection_config.rb', line 9

def host
  @host
end

#optsHash (readonly)

Returns Additional RabbitMQ connection options (see Bunny::Session#initialize).

Returns:

  • (Hash)

    Additional RabbitMQ connection options (see Bunny::Session#initialize)



24
25
26
# File 'lib/surfliner/mq/connection_config.rb', line 24

def opts
  @opts
end

#passwordString (readonly)

Returns The RabbitMQ passsword.

Returns:

  • (String)

    The RabbitMQ passsword



18
19
20
# File 'lib/surfliner/mq/connection_config.rb', line 18

def password
  @password
end

#portString (readonly)

Returns The RabbitMQ AMQP port.

Returns:

  • (String)

    The RabbitMQ AMQP port



12
13
14
# File 'lib/surfliner/mq/connection_config.rb', line 12

def port
  @port
end

#usernameString (readonly)

Returns The RabbitMQ username.

Returns:

  • (String)

    The RabbitMQ username



15
16
17
# File 'lib/surfliner/mq/connection_config.rb', line 15

def username
  @username
end

Class Method Details

.from_env(**opts) ⇒ ConnectionConfig

Reads RabbitMQ configuration from environment variables and returns it as a new ConnectionConfig object.

Variable Sample value Description
RABBITMQ_HOST rabbitmq Hostname of RabbitMQ server
RABBITMQ_NODE_PORT_NUMBER 5672 Port name of RabbitMQ server
RABBITMQ_USERNAME user RabbitMQ username
RABBITMQ_PASSWORD bitnami RabbitMQ password
RABBITMQ_AWAIT_ON_CLOSE false Whether to wait on response when closing (default = true)

Returns:



55
56
57
58
59
60
61
62
63
64
# File 'lib/surfliner/mq/connection_config.rb', line 55

def from_env(**opts)
  ConnectionConfig.new(
    host: ENV.fetch("RABBITMQ_HOST"),
    port: ENV.fetch("RABBITMQ_NODE_PORT_NUMBER"),
    username: ENV.fetch("RABBITMQ_USERNAME"),
    password: ENV.fetch("RABBITMQ_PASSWORD"),
    await_response_on_close: ENV["RABBITMQ_AWAIT_ON_CLOSE"] || true,
    **opts
  )
end

Instance Method Details

#==(other) ⇒ Boolean

Whether other represents the same configuration as this object. Note that if any attributes or connection options are modified, equality becomes unstable.

Returns:

  • (Boolean)

    true if other represents the same configuration as this object, false otherwise



87
88
89
90
# File 'lib/surfliner/mq/connection_config.rb', line 87

def ==(other)
  return unless other.class == self.class
  other.options == options
end

#eql?(other) ⇒ Boolean

Whether other represents the same configuration as this object. Note that if any attributes or connection options are modified, equality becomes unstable.

Returns:

  • (Boolean)

    true if other represents the same configuration as this object, false otherwise



80
81
82
# File 'lib/surfliner/mq/connection_config.rb', line 80

def eql?(other)
  self == other
end

#hashInteger

The hash value of this object. Equal configurations will have equal hash values. Note that if any attributes or connection options are modified, the hash value becomes unstable.

Returns:

  • (Integer)

    a hash value suitable for using equal configs as hash keys



95
96
97
# File 'lib/surfliner/mq/connection_config.rb', line 95

def hash
  options.hash
end

#redacted_urlString

Returns the connection URL as a string, without the password.

Returns:

  • (String)

    the connection URL as a string, without the password



73
74
75
# File 'lib/surfliner/mq/connection_config.rb', line 73

def redacted_url
  @redacted_url ||= session_url.sub(password, "REDACTED")
end

#session_urlString

Returns the connection URL as a string.

Returns:

  • (String)

    the connection URL as a string



68
69
70
# File 'lib/surfliner/mq/connection_config.rb', line 68

def session_url
  @session_url ||= "amqp://#{username}:#{password}@#{host}:#{port}"
end