Class: Cpi::EventConnector

Inherits:
Object
  • Object
show all
Defined in:
lib/cpi/event_connector.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.connection_paramsObject

Returns the value of attribute connection_params.



8
9
10
# File 'lib/cpi/event_connector.rb', line 8

def connection_params
  @connection_params
end

.loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/cpi/event_connector.rb', line 9

def logger
  @logger
end

Class Method Details

.configure(params, logger = nil) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/cpi/event_connector.rb', line 14

def self.configure(params, logger=nil)
  @logger = logger || Logger.new("/dev/null")
  @logger.info "CPI Event Connector initialized"
  @enabled = !!params.delete(:enabled)
  @logger.info "CPI Event Connector is " + (@enabled ? "" : "not ") + "enabled"
  self.connection_params = params
end

.send_event(xml, options = nil) ⇒ Object

This method expects a routing header to be present in the xml document

<cpi_event>

<header>
  <event_uid>unique_id</event_uid> - optional
  <event_type>type</event_type>
  <source_tenant_uid>tenant_uid</source_tenant_uid>
</header>
<content>some content</content>

</cpi_event>



46
47
48
49
50
51
# File 'lib/cpi/event_connector.rb', line 46

def self.send_event(xml, options=nil)
  options ||= {}
  d = Event.new(xml)

  transmit(d.event_type, d.source_tenant_uid, xml, {:queue => options.delete(:queue), :uid => d.event_uid})
end

.test_connectivityObject



53
54
55
56
57
58
59
# File 'lib/cpi/event_connector.rb', line 53

def self.test_connectivity
  if !@enabled
    return 'Event Connector is not enabled.'
  end

  self.connector.test_connectivity
end

.transmit(event_type, tenant_uid, content, options = nil) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cpi/event_connector.rb', line 22

def self.transmit(event_type, tenant_uid, content, options=nil)
  options ||= {}
  if !@enabled
    @logger.debug "Aborting transmit because CPI Event Connector not enabled"
    return
  end
  raise ArgumentError, "Tenant UID must be specified" if tenant_uid.blank?

  event_json = Generator.create_event(event_type, tenant_uid, content, options.delete(:uid))
  @logger.debug "Transmitting message '#{event_json}'"
  self.connector.transmit(event_json, options)
  @logger.debug "Transmission complete"
end