Class: Fairway::Connection

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

Constant Summary collapse

DEFAULT_CHANNEL =
"default"

Instance Method Summary collapse

Constructor Details

#initialize(config = Fairway.config) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
# File 'lib/fairway/connection.rb', line 7

def initialize(config = Fairway.config)
  @config = config
  register_queues
end

Instance Method Details

#deliver(message, channel = DEFAULT_CHANNEL) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/fairway/connection.rb', line 20

def deliver(message, channel = DEFAULT_CHANNEL)
  scripts.fairway_deliver(
    channel,
    @config.facet.call(message),
    message.to_json
  )
end

#queuesObject



12
13
14
15
16
17
18
# File 'lib/fairway/connection.rb', line 12

def queues
  @queues ||= begin
    scripts.registered_queues.map do |name, _|
      Queue.new(self, name)
    end
  end
end

#redisObject



46
47
48
# File 'lib/fairway/connection.rb', line 46

def redis
  @config.redis
end

#register_queuesObject



36
37
38
39
40
# File 'lib/fairway/connection.rb', line 36

def register_queues
  @config.defined_queues.each do |queue|
    scripts.register_queue(queue.name, queue.channel)
  end
end

#scriptsObject



42
43
44
# File 'lib/fairway/connection.rb', line 42

def scripts
  @config.scripts
end

#subscribe(channel_pattern, &block) ⇒ Object



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

def subscribe(channel_pattern, &block)
  redis.psubscribe(channel_pattern) do |on|
    on.pmessage do |pattern, channel, message|
      block.call(channel, message)
    end
  end
end