Module: Hsdq::Sender
Overview
It is holding proxy methods setting the correct type to send your messages
Instance Method Summary collapse
-
#build_spark(message) ⇒ Hash
Generate the spark from the message.
-
#hsdq_send(message) ⇒ Hash, Boolean
Generic method to send any type of message.
-
#hsdq_send_ack(message) ⇒ Hash, Boolean
Ack message is the acknowledgement that message has been received by the receiver (Subscriber).
-
#hsdq_send_callback(message) ⇒ Hash, Boolean
Callback messages are the final step for a successful response.
-
#hsdq_send_error(message) ⇒ Hash, Boolean
Error messages are used to return an error message to the sender in case of error during the processing.
-
#hsdq_send_feedback(message) ⇒ Hash, Boolean
Feedback messages are the intermediate messages used to update the sender of the progress of it's request.
-
#hsdq_send_request(message) ⇒ Hash, Boolean
To be use by your application to send a request from your hsdq class.
-
#prepare_message(message) ⇒ Hash
Complete the message with the key values needed by the system.
-
#send_message(message, spark) ⇒ String
Send the message using a Redis multi in order to do everything within a single transaction.
-
#valid_keys?(message) ⇒ Boolean
Validate that the minimum necessary keys are present into the message before sending it.
Methods included from Connectors
#cx_admin, #cx_data, #cx_listener, #cx_session
Instance Method Details
#build_spark(message) ⇒ Hash
Generate the spark from the message. The spark is ephemeral so everything in the spark is included into the message hash.
111 112 113 114 115 |
# File 'lib/hsdq/sender.rb', line 111 def build_spark() keys = [:sender, :uid, :spark_uid, :tstamp, :context, :previous_sender, :type, :topic, :task ] spark = keys.inject({}) { |memo, param| memo.merge(param => [param]) } spark end |
#hsdq_send(message) ⇒ Hash, Boolean
Generic method to send any type of message. It is preferred to use the the send proxy methods that are setting the correct type for the message the application has to send: hsdq_send_request, callback etc.. The message type must be provided.
65 66 67 68 69 70 71 72 73 |
# File 'lib/hsdq/sender.rb', line 65 def hsdq_send() = if valid_keys?() && valid_type?([:type]) spark = build_spark() , spark else false end end |
#hsdq_send_ack(message) ⇒ Hash, Boolean
Ack message is the acknowledgement that message has been received by the receiver (Subscriber).
Ack is sent automatically by the system. This method is to send additional ack messages if needed. (very seldom used)
25 26 27 |
# File 'lib/hsdq/sender.rb', line 25 def hsdq_send_ack() hsdq_send(.merge(type: :ack)) end |
#hsdq_send_callback(message) ⇒ Hash, Boolean
Callback messages are the final step for a successful response.
34 35 36 |
# File 'lib/hsdq/sender.rb', line 34 def hsdq_send_callback() hsdq_send(.merge(type: :callback)) end |
#hsdq_send_error(message) ⇒ Hash, Boolean
Error messages are used to return an error message to the sender in case of error during the processing.
Error messages are also automatically sent by the system in case of validation error at message recetion.
54 55 56 |
# File 'lib/hsdq/sender.rb', line 54 def hsdq_send_error() hsdq_send(.merge(type: :error)) end |
#hsdq_send_feedback(message) ⇒ Hash, Boolean
Feedback messages are the intermediate messages used to update the sender of the progress of it's request.
43 44 45 |
# File 'lib/hsdq/sender.rb', line 43 def hsdq_send_feedback() hsdq_send(.merge(type: :feedback)) end |
#hsdq_send_request(message) ⇒ Hash, Boolean
To be use by your application to send a request from your hsdq class. This is a Proxy for hsdq_send to send request messages
14 15 16 |
# File 'lib/hsdq/sender.rb', line 14 def hsdq_send_request() hsdq_send(.merge(type: :request)) end |
#prepare_message(message) ⇒ Hash
Complete the message with the key values needed by the system
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/hsdq/sender.rb', line 97 def () [:sender] = channel [:uid] ||= current_uid || SecureRandom.uuid [:spark_uid] = SecureRandom.uuid [:tstamp] = Time.now.utc [:context] = context_params [:previous_sender] = previous_sender [:sent_to] ||= sent_to end |
#send_message(message, spark) ⇒ String
Send the message using a Redis multi in order to do everything within a single transaction
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/hsdq/sender.rb', line 79 def (, spark) # avoid further processing into the multi redis command channel_name = [:sent_to] h_key = hsdq_key burst_j = .to_json spark_j = spark.to_json bkey = burst_key() cx_data.multi do cx_data.hset h_key, bkey, burst_j cx_data.expire h_key, 259200 #3 days todo set by options cx_data.rpush channel_name, spark_j end end |
#valid_keys?(message) ⇒ Boolean
Validate that the minimum necessary keys are present into the message before sending it.
119 120 121 |
# File 'lib/hsdq/sender.rb', line 119 def valid_keys?() [:sender, :sent_to, :type, :uid] - .keys == [] end |