Class: StagedEvent::GooglePubSub::Publisher

Inherits:
Publisher
  • Object
show all
Defined in:
lib/staged_event/google_pub_sub/publisher.rb

Instance Method Summary collapse

Constructor Details

#initializePublisher

Returns a new instance of Publisher.

Raises:

  • (ArgumentError)


6
7
8
9
10
# File 'lib/staged_event/google_pub_sub/publisher.rb', line 6

def initialize
  @google_pubsub = Helper.new_google_pubsub

  raise ArgumentError, "topic_map must be initialized" unless topic_map.is_a?(Hash) && topic_map.any?
end

Instance Method Details

#publish(model) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/staged_event/google_pub_sub/publisher.rb', line 12

def publish(model)
  topic_name = model.topic || topic_map.keys.first
  topic_id = topic_map.fetch(topic_name)
  google_topic = google_pubsub.topic(topic_id, skip_lookup: true)

  # https://github.com/googleapis/google-cloud-ruby/blob/720d14d3641a60c0fab0bf8519bdd730a753a897/google-cloud-pubsub/lib/google/cloud/pubsub/topic.rb#L655
  google_topic.publish(model.data)
end