Class: ActionSubscriber::Router

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

Constant Summary collapse

DEFAULT_SETTINGS =
{
  :acknowledgements => false,
  :durable => false,
  :exchange => "events",
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



15
16
17
# File 'lib/action_subscriber/router.rb', line 15

def initialize
  @current_threadpool_name = :default
end

Class Method Details

.draw_routes(&block) ⇒ Object



3
4
5
6
7
# File 'lib/action_subscriber/router.rb', line 3

def self.draw_routes(&block)
  router = self.new
  router.instance_eval(&block)
  router.routes
end

Instance Method Details

#connection(name, settings, &block) ⇒ Object



19
20
21
22
# File 'lib/action_subscriber/router.rb', line 19

def connection(name, settings, &block)
  ::ActionSubscriber.print_deprecation_warning("setting up a threadpool for #{name} instead of a new connection")
  threadpool(name, settings, &block)
end

#default_queue_for(route_settings) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/action_subscriber/router.rb', line 39

def default_queue_for(route_settings)
  [
    local_application_name,
    route_settings[:publisher],
    resource_name(route_settings),
    route_settings[:action].to_s,
  ].compact.join(".")
end

#default_routes_for(subscriber, options = {}) ⇒ Object



48
49
50
51
52
53
# File 'lib/action_subscriber/router.rb', line 48

def default_routes_for(subscriber, options = {})
  options = options.merge({:threadpool_name => @current_threadpool_name})
  subscriber.routes(options).each do |route|
    routes << route
  end
end

#default_routing_key_for(route_settings) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/action_subscriber/router.rb', line 31

def default_routing_key_for(route_settings)
  [
    route_settings[:publisher],
    resource_name(route_settings),
    route_settings[:action].to_s,
  ].compact.join(".")
end

#local_application_nameObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/action_subscriber/router.rb', line 55

def local_application_name
  @_local_application_name ||= begin
    local_application_name = case
                             when ENV['APP_NAME'] then
                               ENV['APP_NAME'].to_s.dup
                             when defined?(::Rails) then
                               if ::Rails.application.class.respond_to?(:module_parent_name)
                                 ::Rails.application.class.module_parent_name.dup
                               else
                                 ::Rails.application.class.parent_name.dup
                               end
                             else
                               raise "Define an application name (ENV['APP_NAME'])"
                             end
    local_application_name.downcase
  end
end

#resource_name(route_settings) ⇒ Object



73
74
75
# File 'lib/action_subscriber/router.rb', line 73

def resource_name(route_settings)
  route_settings[:subscriber].name.underscore.gsub(/_subscriber/, "").to_s
end

#route(subscriber, action, options = {}) ⇒ Object



77
78
79
80
81
82
# File 'lib/action_subscriber/router.rb', line 77

def route(subscriber, action, options = {})
  route_settings = DEFAULT_SETTINGS.merge(:threadpool_name => @current_threadpool_name).merge(options).merge(:subscriber => subscriber, :action => action)
  route_settings[:routing_key] ||= default_routing_key_for(route_settings)
  route_settings[:queue] ||= default_queue_for(route_settings)
  routes << Route.new(route_settings)
end

#routesObject



84
85
86
# File 'lib/action_subscriber/router.rb', line 84

def routes
  @routes ||= []
end

#threadpool(name, settings) ⇒ Object



24
25
26
27
28
29
# File 'lib/action_subscriber/router.rb', line 24

def threadpool(name, settings)
  ::ActionSubscriber::ThreadPools.setup_threadpool(name, settings)
  @current_threadpool_name = name
  yield
  @current_threadpool_name = :default
end