Class: Mqlight::Service

Inherits:
Object
  • Object
show all
Includes:
Logging, URI
Defined in:
lib/mqlight/util.rb

Overview

A contain design to hold all the connection information. Note. the ‘to_s’ has been designed to supress showing the password.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Constructor Details

#initialize(uri, user = nil, password = nil) ⇒ Service

Returns a new instance of Service.

Parameters:

  • service (URI)

    of the service to connect to

  • user (String) (defaults to: nil)

    the user id to connect with

  • password (String) (defaults to: nil)

    the password for the given user id.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/mqlight/util.rb', line 199

def initialize(uri, user = nil, password = nil)
  # No Trace - security

  @service_uri = uri
  unless @service_uri.port
    @service_uri.port = (@service_uri.scheme == 'amqps') ? 5671 : 5672
  end

  # Handle authentication information.
  if user && password && @service_uri.userinfo.nil?
    @service_uri.userinfo = "#{URI.encode_www_form_component(user)}:"\
           "#{URI.encode_www_form_component(password)}"
  end

  @address = @service_uri.to_s
  p = @service_uri.clone
  p.userinfo = ''
  @pattern = p.to_s
  @service = "#{@service_uri.scheme}://#{@service_uri.host}:" \
    "#{@service_uri.port}"
  @host = @service_uri.host
  @port = @service_uri.port
  # No Trace
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



189
190
191
# File 'lib/mqlight/util.rb', line 189

def address
  @address
end

#hostObject (readonly)

Returns the value of attribute host.



191
192
193
# File 'lib/mqlight/util.rb', line 191

def host
  @host
end

#patternObject (readonly)

Returns the value of attribute pattern.



188
189
190
# File 'lib/mqlight/util.rb', line 188

def pattern
  @pattern
end

#portObject (readonly)

Returns the value of attribute port.



192
193
194
# File 'lib/mqlight/util.rb', line 192

def port
  @port
end

#serviceObject (readonly)

Returns the value of attribute service.



190
191
192
# File 'lib/mqlight/util.rb', line 190

def service
  @service
end

Instance Method Details

#inspectObject

Override inspect so that the URI passwords are not returned as clear text



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/mqlight/util.rb', line 248

def inspect
  a = '<Mqlight::Service'
  a << ' @service_uri = '
  a << @service_uri.inspect.sub(%r{:[^\/:]+@}, ':********@')
  a << ', @address = '
  a << @address.inspect.sub(%r{:[^\/:]+@}, ':********@')
  a << ', @pattern = '
  a << @pattern.inspect
  a << ', @service = '
  a << @service.inspect
  a << ', @host = '
  a << @host.inspect
  a << ', @port = '
  a << @port.inspect
  a << '>'
end

#ssl?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/mqlight/util.rb', line 227

def ssl?
  @service_uri.scheme == 'amqps'
end

#to_sObject



234
235
236
237
238
239
240
241
242
# File 'lib/mqlight/util.rb', line 234

def to_s
  if @service_uri.userinfo
    "[Service] #{@service_uri.scheme}://#{@service_uri.user}:*******" \
      "@#{@service_uri.host}:#{@service_uri.port}"
  else
    "[Service] #{@service_uri.scheme}://#{@service_uri.host}:" \
      "#{@service_uri.port}"
  end
end