Module: Cloudenvoy::Backend::MemoryPubSub

Defined in:
lib/cloudenvoy/backend/memory_pub_sub.rb

Overview

Store messages in a memory queue. Used for testing

Class Method Summary collapse

Class Method Details

.clear(topic) ⇒ Array

Clear all messages in a specific topic.

Parameters:

  • name (String)

    The topic to clear.

Returns:

  • (Array)

    The cleared array.



30
31
32
# File 'lib/cloudenvoy/backend/memory_pub_sub.rb', line 30

def clear(topic)
  queue(topic).clear
end

.clear_allObject

Clear all messages across all topics.

Parameters:

  • name (String)

    The topic to clear.



39
40
41
# File 'lib/cloudenvoy/backend/memory_pub_sub.rb', line 39

def clear_all
  @queues&.values&.each { |e| e.clear }
end

.publish(topic, payload, metadata = {}) ⇒ Cloudenvoy::Message

Publish a message to a topic.

Parameters:

  • topic (String)

    The name of the topic

  • payload (Hash, String)

    The message content.

  • attrs (Hash)

    The message attributes.

Returns:



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cloudenvoy/backend/memory_pub_sub.rb', line 52

def publish(topic, payload,  = {})
  msg = Message.new(
    id: SecureRandom.uuid,
    payload: payload,
    metadata: ,
    topic: topic
  )
  queue(topic).push(msg)

  msg
end

.publish_all(topic, msg_args) ⇒ Array<Cloudenvoy::Message>

Publish multiple messages to a topic.

Parameters:

  • topic (String)

    The name of the topic

  • msg_args (Array<Array<[Hash, String]>>)

    A list of message [payload, metadata].

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cloudenvoy/backend/memory_pub_sub.rb', line 72

def publish_all(topic, msg_args)
  # Build the messages
  msgs = msg_args.map do |(payload, )|
    Message.new(
      id: SecureRandom.uuid,
      payload: payload,
      metadata: ,
      topic: topic
    )
  end

  # Push all the messages and return them
  queue(topic).push(*msgs)
  msgs
end

.queue(topic) ⇒ Array

Return the message queue for a specific topic.

Parameters:

  • name (String)

    The topic to retrieve.

Returns:

  • (Array)

    The list of messages for the provided topic



18
19
20
21
# File 'lib/cloudenvoy/backend/memory_pub_sub.rb', line 18

def queue(topic)
  @queues ||= {}
  @queues[topic.to_s] ||= []
end

.upsert_subscription(_topic, name, _opts) ⇒ Cloudenvoy::Subscription

Create or update a subscription for a specific topic.

Parameters:

  • topic (String)

    The name of the topic

  • name (String)

    The name of the subscription

  • opts (Hash)

    The subscription configuration options

Returns:



97
98
99
# File 'lib/cloudenvoy/backend/memory_pub_sub.rb', line 97

def upsert_subscription(_topic, name, _opts)
  Subscription.new(name: name)
end

.upsert_topic(topic) ⇒ Cloudenvoy::Topic

Create or update a topic.

Parameters:

  • topic (String)

    The topic name.

Returns:



108
109
110
# File 'lib/cloudenvoy/backend/memory_pub_sub.rb', line 108

def upsert_topic(topic)
  Topic.new(name: topic)
end