Class: Tincan::Sender
- Inherits:
-
Object
- Object
- Tincan::Sender
- Defined in:
- lib/tincan/sender.rb
Overview
An object whose purpose is to send messages to a given series of Redis message queues for those receiving them.
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#redis_host ⇒ Object
Returns the value of attribute redis_host.
-
#redis_port ⇒ Object
Returns the value of attribute redis_port.
Instance Method Summary collapse
-
#flush_all_queues_for_object(object_name) ⇒ Boolean
Tells Redis to delete any pending messages for registered receivers, by essentially deleting the message key.
-
#identifier_for_message(message) ⇒ Integer
Generates an identifier to be used for a message.
-
#initialize(options = {}) ⇒ Tincan::Receiver
constructor
Creates and return a sender object, ready to send.
-
#keys_for_receivers(object_name, exclude: []) ⇒ Array
Asks Redis for the set of all active receivers and generates string keys for all of them.
-
#primary_key_for_message(message) ⇒ String
Generates a key to be used as the primary destination key in Redis.
-
#publish(message, exclude: []) ⇒ Boolean
Bundles up an object in a message object and publishes it to the Redis host.
-
#redis_client ⇒ Redis
The instance of a Redis communicator that can publish messages.
Constructor Details
#initialize(options = {}) ⇒ Tincan::Receiver
Creates and return a sender object, ready to send. You can pass in either a hash or a block; the block takes priority.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/tincan/sender.rb', line 17 def initialize( = {}) if block_given? yield(self) else @config = ivars = %i(redis_host redis_port namespace) ivars.each { |n| send("#{n}=".to_sym, @config[n]) } end self.redis_port ||= 6379 end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/tincan/sender.rb', line 8 def config @config end |
#namespace ⇒ Object
Returns the value of attribute namespace.
9 10 11 |
# File 'lib/tincan/sender.rb', line 9 def namespace @namespace end |
#redis_host ⇒ Object
Returns the value of attribute redis_host.
9 10 11 |
# File 'lib/tincan/sender.rb', line 9 def redis_host @redis_host end |
#redis_port ⇒ Object
Returns the value of attribute redis_port.
9 10 11 |
# File 'lib/tincan/sender.rb', line 9 def redis_port @redis_port end |
Instance Method Details
#flush_all_queues_for_object(object_name) ⇒ Boolean
Tells Redis to delete any pending messages for registered receivers, by essentially deleting the message key.
57 58 59 60 61 62 |
# File 'lib/tincan/sender.rb', line 57 def flush_all_queues_for_object(object_name) keys_for_receivers(object_name).each do |key| redis_client.del(key) end true end |
#identifier_for_message(message) ⇒ Integer
Generates an identifier to be used for a message. It’s unique!
86 87 88 |
# File 'lib/tincan/sender.rb', line 86 def () . end |
#keys_for_receivers(object_name, exclude: []) ⇒ Array
Asks Redis for the set of all active receivers and generates string keys for all of them. Formatted like “namespace:object:client:messages”.
45 46 47 48 49 50 51 52 |
# File 'lib/tincan/sender.rb', line 45 def keys_for_receivers(object_name, exclude: []) exclude ||= [] receiver_list_key = key_for_elements(object_name, 'receivers') receivers = redis_client.smembers(receiver_list_key) receivers.reject { |r| exclude.include?(r) }.map do |receiver| key_for_elements(object_name, receiver, 'messages') end end |
#primary_key_for_message(message) ⇒ String
Generates a key to be used as the primary destination key in Redis.
93 94 95 96 |
# File 'lib/tincan/sender.rb', line 93 def () identifier = () key_for_elements(.object_name.downcase, 'messages', identifier) end |
#publish(message, exclude: []) ⇒ Boolean
Bundles up an object in a message object and publishes it to the Redis host.
72 73 74 75 76 77 78 |
# File 'lib/tincan/sender.rb', line 72 def publish(, exclude: []) identifier = () redis_client.set((), .to_json) keys = keys_for_receivers(.object_name.downcase, exclude: exclude) keys.each { |key| redis_client.rpush(key, identifier) } true end |
#redis_client ⇒ Redis
The instance of a Redis communicator that can publish messages.
32 33 34 |
# File 'lib/tincan/sender.rb', line 32 def redis_client @redis_client ||= ::Redis.new(host: redis_host, port: redis_port) end |