Class: Sock::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sock/client.rb

Overview

Client is the interface for publishing events to Drawer

Instance Method Summary collapse

Constructor Details

#initialize(name: DEFAULT_NAME, logger: Logger.new(STDOUT), redis: Redis.new) ⇒ Client

Returns a new instance of Client.



4
5
6
7
8
9
10
# File 'lib/sock/client.rb', line 4

def initialize(name: DEFAULT_NAME,
               logger: Logger.new(STDOUT),
               redis: Redis.new)
  @logger = logger
  @name = name
  @redis = redis
end

Instance Method Details

#pub(msg, postfix: '') ⇒ Object

send a message to all subscribed listeners.



13
14
15
16
# File 'lib/sock/client.rb', line 13

def pub(msg, postfix: '')
  @logger.info "sending #{msg} on channel: #{channel_name(postfix)}"
  @redis.publish(channel_name(postfix), msg)
end

#sub(server, channel, &block) ⇒ Object

subscribe to all events fired on a given channel



19
20
21
22
# File 'lib/sock/client.rb', line 19

def sub(server, channel, &block)
  @logger.info "subscribing to #{channel}"
  server.channel(channel_name(channel)).subscribe { |msg| block.call(msg) }
end