Module: QueueBus::Subscriber::ClassMethods

Defined in:
lib/queue_bus/subscriber.rb

Overview

The class methods that will be added to the class it’s included in. Use them to configure and subscribe.

Instance Method Summary collapse

Instance Method Details

#app_keyObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/queue_bus/subscriber.rb', line 17

def app_key
  return @app_key if @app_key

  @app_key = ::QueueBus.default_app_key
  return @app_key if @app_key

  # module or class_name
  val = name.to_s.split('::').first
  @app_key = ::QueueBus::Util.underscore(val)
end

#application(app_key) ⇒ Object



13
14
15
# File 'lib/queue_bus/subscriber.rb', line 13

def application(app_key)
  @app_key = ::QueueBus::Application.normalize(app_key)
end

#perform(attributes) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/queue_bus/subscriber.rb', line 49

def perform(attributes)
  ::QueueBus.with_global_attributes(attributes) do
    sub_key = attributes['bus_rider_sub_key']
    meth_key = sub_key.split('.').last
    queue_bus_execute(meth_key, attributes)
  end
end

#queue_bus_execute(key, attributes) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/queue_bus/subscriber.rb', line 57

def queue_bus_execute(key, attributes)
  args = attributes
  args = send(@transform, attributes) if @transform
  args = [args] unless args.is_a?(Array)
  me = if respond_to?(:subscriber_with_attributes)
         subscriber_with_attributes(attributes)
       else
         new
       end
  me.send(key, *args)
end

#subscribe(method_name, matcher_hash = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/queue_bus/subscriber.rb', line 28

def subscribe(method_name, matcher_hash = nil)
  queue_name   = nil
  queue_name ||= instance_variable_get(:@queue) || (respond_to?(:queue) && queue)
  queue_name ||= ::QueueBus.default_queue
  queue_name ||= "#{app_key}_default"
  subscribe_queue(queue_name, method_name, matcher_hash)
end

#subscribe_queue(queue_name, method_name, matcher_hash = nil) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/queue_bus/subscriber.rb', line 36

def subscribe_queue(queue_name, method_name, matcher_hash = nil)
  klass = self
  matcher_hash ||= { 'bus_event_type' => method_name }
  sub_key = "#{name}.#{method_name}"
  dispatcher = ::QueueBus.dispatcher_by_key(app_key)
  dispatcher.add_subscription(queue_name, sub_key, klass.name.to_s, matcher_hash,
                              ->(att) { klass.perform(att) })
end

#transform(method_name) ⇒ Object



45
46
47
# File 'lib/queue_bus/subscriber.rb', line 45

def transform(method_name)
  @transform = method_name
end