Class: ACIrb::MoEvent

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_options = {}) ⇒ MoEvent

Returns a new instance of MoEvent.



124
125
# File 'lib/events.rb', line 124

def initialize(_options = {})
end

Class Method Details

.parse_event(event_channel, event_str) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/events.rb', line 127

def self.parse_event(event_channel, event_str)
  subscription = nil
  events = []

  if event_channel.rest.format == 'xml'
    doc = Nokogiri::XML(event_str)
    subscription_id = doc.at_css('imdata')['subscriptionId']
    puts event_str
    doc.root.elements.each do |xml_obj|
      event = {
        type: xml_obj.attributes['status'].to_s,
        properties: Hash[xml_obj.attributes.map { |k, str| [k, str.value.to_s] }],
        class: xml_obj.name,
        subscription_id: subscription_id
      }
      events.push(event)
    end
  elsif event_channel.rest.format == 'json'
    doc = JSON.parse(event_str, symbolize_names: false)
    subscription_id = doc['subscriptionId']
    imdata = doc['imdata']
    imdata.each do |obj|
      cls = obj.keys[0]
      event = {
        type: obj[cls]['attributes']['status'].to_s,
        properties: Hash[obj[cls]['attributes'].map { |k, str| [k, str.to_s] }],
        class: cls,
        subscription_id: subscription_id[0]
      }
      events.push(event)
    end

  end

  events
end