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(event, options = nil) ⇒ Object

Sends a Cpi::Event



43
44
45
46
# File 'lib/cpi/event_connector.rb', line 43

def self.send_event(event, options=nil)
  return send_event_xml(event, options) if event.is_a? String # for backwards compatibility with 0.3.0
  transmit_event(event, event.xml, options)
end

.send_event_xml(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>



58
59
60
61
# File 'lib/cpi/event_connector.rb', line 58

def self.send_event_xml(xml, options=nil)
  event = EventParser.parse(xml)
  transmit_event(event, xml, options)
end

.test_connectivityObject



63
64
65
66
67
68
69
# File 'lib/cpi/event_connector.rb', line 63

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

DEPRECATED: Please use send_event instead

This method does not add the correct routing header that CPI requires in the content of the event.

This method will be made private eventually

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cpi/event_connector.rb', line 28

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