Module: Hsdq::Sender

Includes:
Connectors
Included in:
Hsdq
Defined in:
lib/hsdq/sender.rb

Overview

It is holding proxy methods setting the correct type to send your messages

Instance Method Summary collapse

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.

Parameters:

  • message (Hash)

    to be sent

Returns:

  • (Hash)

    spark, the tiny part pushed to the list



111
112
113
114
115
# File 'lib/hsdq/sender.rb', line 111

def build_spark(message)
  keys = [:sender, :uid, :spark_uid, :tstamp, :context, :previous_sender, :type, :topic, :task ]
  spark = keys.inject({}) { |memo, param| memo.merge(param => message[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.

Parameters:

  • message (Hash)

    to send

Returns:

  • (Hash)

    original message with the system additional parameters if the message is sent

  • (Boolean)

    false if the message's validation failed



65
66
67
68
69
70
71
72
73
# File 'lib/hsdq/sender.rb', line 65

def hsdq_send(message)
  message = prepare_message message
  if valid_keys?(message) && valid_type?(message[:type])
    spark = build_spark(message)
    send_message message, 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)

Parameters:

  • message (Hash)

    for the acknowledge you want to send (sent automatically by the system)

Returns:

  • (Hash)

    your original message with the system additional parameters

  • (Boolean)

    false if the message's validation failed



25
26
27
# File 'lib/hsdq/sender.rb', line 25

def hsdq_send_ack(message)
  hsdq_send(message.merge(type: :ack))
end

#hsdq_send_callback(message) ⇒ Hash, Boolean

Callback messages are the final step for a successful response.

Parameters:

  • message (Hash)

    for the callback you want to send

Returns:

  • (Hash)

    your original message with the system additional parameters

  • (Boolean)

    false if the message's validation failed



34
35
36
# File 'lib/hsdq/sender.rb', line 34

def hsdq_send_callback(message)
  hsdq_send(message.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.

Parameters:

  • message (Hash)

    for the error message you want to send

Returns:

  • (Hash)

    your original message with the system additional parameters

  • (Boolean)

    false if the message's validation failed



54
55
56
# File 'lib/hsdq/sender.rb', line 54

def hsdq_send_error(message)
  hsdq_send(message.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.

Parameters:

  • message (Hash)

    for the feedback you want to send

Returns:

  • (Hash)

    your original message with the system additional parameters

  • (Boolean)

    false if the message's validation failed



43
44
45
# File 'lib/hsdq/sender.rb', line 43

def hsdq_send_feedback(message)
  hsdq_send(message.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

Parameters:

  • message (Hash)

    The request you want to send

Returns:

  • (Hash)

    your original message with the system additional parameters

  • (Boolean)

    false if the message's validation failed



14
15
16
# File 'lib/hsdq/sender.rb', line 14

def hsdq_send_request(message)
  hsdq_send(message.merge(type: :request))
end

#prepare_message(message) ⇒ Hash

Complete the message with the key values needed by the system

Parameters:

  • message (Hash)

    original message

Returns:

  • (Hash)

    message with the additional system data



97
98
99
100
101
102
103
104
105
106
# File 'lib/hsdq/sender.rb', line 97

def prepare_message(message)
  message[:sender]           = channel
  message[:uid]            ||= current_uid || SecureRandom.uuid
  message[:spark_uid]        = SecureRandom.uuid
  message[:tstamp]           = Time.now.utc
  message[:context]          = context_params
  message[:previous_sender]  = previous_sender
  message[:sent_to]        ||= sent_to
  message
end

#send_message(message, spark) ⇒ String

Send the message using a Redis multi in order to do everything within a single transaction

Parameters:

  • message (Hash)

    to send, will be stored as an entry in the message hash

  • spark (Hash)

    the ephemeral part of the message. pushed to a list

Returns:

  • (String)

    "OK"



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hsdq/sender.rb', line 79

def send_message(message, spark)
  # avoid further processing into the multi redis command
  channel_name = message[:sent_to]
  h_key        = hsdq_key message
  burst_j      = message.to_json
  spark_j      = spark.to_json
  bkey         = burst_key(message)

  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.

Parameters:

  • message (Hash)

    The full message to be sent (including the system data)

Returns:

  • (Boolean)


119
120
121
# File 'lib/hsdq/sender.rb', line 119

def valid_keys?(message)
  [:sender, :sent_to, :type, :uid] - message.keys == []
end