Module: Cloudenvoy::Subscriber::ClassMethods

Defined in:
lib/cloudenvoy/subscriber.rb

Overview

Module class methods

Instance Method Summary collapse

Instance Method Details

#cloudenvoy_options(opts = {}) ⇒ Hash

Set the subscriber runtime options.

Parameters:

  • opts (Hash) (defaults to: {})

    The subscriber options.

Returns:

  • (Hash)

    The options set.



83
84
85
86
# File 'lib/cloudenvoy/subscriber.rb', line 83

def cloudenvoy_options(opts = {})
  opt_list = opts&.map { |k, v| [k.to_sym, v] } || [] # symbolize
  @cloudenvoy_options_hash = Hash[opt_list]
end

#cloudenvoy_options_hashHash

Return the subscriber runtime options.

Returns:

  • (Hash)

    The subscriber runtime options.



93
94
95
# File 'lib/cloudenvoy/subscriber.rb', line 93

def cloudenvoy_options_hash
  @cloudenvoy_options_hash || {}
end

#setupArray<Cloudenvoy::Subscription>

Create the Subscriber subscription in Pub/Sub.

Returns:



128
129
130
131
132
133
134
# File 'lib/cloudenvoy/subscriber.rb', line 128

def setup
  topics.map do |t|
    topic_name = t[:name] || t['name']
    sub_opts = t.reject { |k, _| k.to_sym == :name }
    PubSubClient.upsert_subscription(topic_name, subscription_name(topic_name), sub_opts)
  end
end

#subscription_name(topic) ⇒ String

Return the subscription name used by this subscriber to subscribe to a specific topic.

Returns:

  • (String)

    The subscription name.



115
116
117
118
119
120
121
# File 'lib/cloudenvoy/subscriber.rb', line 115

def subscription_name(topic)
  [
    Cloudenvoy.config.gcp_sub_prefix.tr('.', '-'),
    to_s.underscore,
    topic
  ].join('.')
end

#topicsArray<Hash>

Return the list of topics this subscriber listens to.

Returns:

  • (Array<Hash>)

    The list of subscribed topics.



103
104
105
106
107
# File 'lib/cloudenvoy/subscriber.rb', line 103

def topics
  @topics ||= [cloudenvoy_options_hash[:topic], cloudenvoy_options_hash[:topics]].flatten.compact.map do |t|
    t.is_a?(String) ? { name: t } : t
  end
end