Class: RisingDragon::SQS::Emitter

Inherits:
Object
  • Object
show all
Defined in:
lib/rising_dragon/sqs/emitter.rb

Instance Method Summary collapse

Constructor Details

#initializeEmitter

Returns a new instance of Emitter.



4
5
6
# File 'lib/rising_dragon/sqs/emitter.rb', line 4

def initialize
  @handlers = {}
end

Instance Method Details

#emit_event(event) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/rising_dragon/sqs/emitter.rb', line 38

def emit_event(event)
  handler = @handlers[event.type]
  raise ::RisingDragon::UnRegisterEvent, "event_type: `#{event.type}` is unregistered" unless handler

  handler.new.handle(event)

  nil
end

#emit_sns_msg(body) ⇒ Object



58
59
60
61
# File 'lib/rising_dragon/sqs/emitter.rb', line 58

def emit_sns_msg(body)
  event = event_from_json(body)
  emit_event(event)
end

#event_from_json(body) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/rising_dragon/sqs/emitter.rb', line 47

def event_from_json(body)
  msg = JSON.parse(body["Message"])

  id = msg["id"]
  type = msg["type"]
  timestamp = Time.at(msg["timestamp"] / 1000.0)
  data = msg["data"]

  ::RisingDragon::Event.new(id: id, type: type, timestamp: timestamp, data: data)
end

#ignore(event_name) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/rising_dragon/sqs/emitter.rb', line 26

def ignore(event_name)
  unless event_name.is_a?(String)
    raise "event_name must be String, but it's #{event_name.class}. event_name: #{event_name}"
  end

  @handlers[event_name] = ::RisingDragon::SQS::EmptyHandler
end

#listObject



34
35
36
# File 'lib/rising_dragon/sqs/emitter.rb', line 34

def list
  @handlers.keys
end

#register(event_name, handler_class) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/rising_dragon/sqs/emitter.rb', line 8

def register(event_name, handler_class)
  unless event_name.is_a?(String)
    raise "event_name must be String, but it's #{event_name.class}. event_name: #{event_name}"
  end
  if @handlers[event_name]
    raise "RisingDragon::SQS::Emitter accepts only one callback per event. event_name: #{event_name}"
  end
  @handlers[event_name] = handler_class
end

#unregister(event_name) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/rising_dragon/sqs/emitter.rb', line 18

def unregister(event_name)
  unless event_name.is_a?(String)
    raise "event_name must be String, but it's #{event_name.class}. event_name: #{event_name}"
  end

  @handlers.delete(event_name)
end