Class: Aggro::Subscriber

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

Overview

Public: Subscribes to topics at a given endpoint.

Constant Summary collapse

RAW_HANDLER =
:handle_raw

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, callable = nil, &block) ⇒ Subscriber

Returns a new instance of Subscriber.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/aggro/subscriber.rb', line 6

def initialize(endpoint, callable = nil, &block)
  if callable
    @callback = callable
  elsif block_given?
    @callback = block
  else
    fail ArgumentError
  end

  @transport_sub = Aggro.transport.subscriber endpoint, method(RAW_HANDLER)
  @subscribed_topics = Set.new
end

Instance Method Details

#bindObject



19
20
21
# File 'lib/aggro/subscriber.rb', line 19

def bind
  @transport_sub.start
end

#handle_message(message) ⇒ Object



23
24
25
# File 'lib/aggro/subscriber.rb', line 23

def handle_message(message)
  @callback.call message.id, message.events if message.is_a? Message::Events
end

#stopObject



27
28
29
# File 'lib/aggro/subscriber.rb', line 27

def stop
  @transport_sub.stop
end

#subscribe_to_topic(topic) ⇒ Object



31
32
33
34
35
36
# File 'lib/aggro/subscriber.rb', line 31

def subscribe_to_topic(topic)
  return if @subscribed_topics.include? topic

  @subscribed_topics << topic
  @transport_sub.add_subscription message_prefix_for_topic(topic)
end