Class: Snoopit::Notifiers::Stomp

Inherits:
Snoopit::Notifier show all
Defined in:
lib/snoopit/notifiers/stomp.rb

Instance Attribute Summary

Attributes inherited from Snoopit::Notifier

#configuration, #klass, #name

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Stomp

The name ‘stomp’ is used by the Snooper to identify type of notifier to create The empty constructor is used to initialize the class when loaded dynamically After loaded dynamically the method set_config is called by the base class to set the configuration



12
13
14
15
16
17
18
19
# File 'lib/snoopit/notifiers/stomp.rb', line 12

def initialize(config=nil)
  super config, 'stomp'
  @host     = config['host']  || 'localhost'
  @port     = config['port']  || 61613
  @login    = config['login']
  @passcode = config['passcode']
  @headers  = config['headers']
end

Instance Method Details

#get_connect_paramsObject



32
33
34
35
36
37
# File 'lib/snoopit/notifiers/stomp.rb', line 32

def get_connect_params
  params            = { host: @host, port: @port }
  params[:login]    = @login unless @login.nil?
  params[:passcode] = @passcode unless @passcode.nil?
  params
end

#get_connectionObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/snoopit/notifiers/stomp.rb', line 21

def get_connection
  params =  { hosts: [ get_connect_params ] }
  set_connect_headers params
  ::Stomp::Connection.new params
rescue => e
  Snoopit.logger.warn 'Stomp failed to connect: ' + params.to_json
  Snoopit.logger.warn 'Stomp failed to connect: ' + e.message
  Snoopit.logger.warn 'Stomp failed to connect: ' + e.backtrace.join('\n')
  nil
end

#notify(found, notify_params) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/snoopit/notifiers/stomp.rb', line 43

def notify(found, notify_params)
  conn = get_connection
  send_message(conn, found, notify_params) unless conn.nil?
rescue => e
    Snoopit.logger.warn e.message
    Snoopit.logger.warn e.backtrace.join('\n')
ensure
  conn.disconnect unless conn.nil?
end

#send_message(conn, found, notify_params) ⇒ Object



53
54
55
# File 'lib/snoopit/notifiers/stomp.rb', line 53

def send_message(conn, found, notify_params)
  conn.publish notify_params['queue'], found.to_json, notify_params['headers']
end

#set_connect_headers(params) ⇒ Object



39
40
41
# File 'lib/snoopit/notifiers/stomp.rb', line 39

def set_connect_headers(params)
  params[:connect_headers] = @headers unless @headers.nil?
end