Class: Gcloud::Pubsub::Topic::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/pubsub/topic.rb

Overview

Batch object used to publish multiple messages at once.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil, attributes = {}) ⇒ Batch

Create a new instance of the object.



591
592
593
594
595
596
597
# File 'lib/gcloud/pubsub/topic.rb', line 591

def initialize data = nil, attributes = {} #:nodoc:
  @messages = []
  @mode = :batch
  return if data.nil?
  @mode = :single
  publish data, attributes
end

Instance Attribute Details

#messagesObject (readonly)

The messages to publish



587
588
589
# File 'lib/gcloud/pubsub/topic.rb', line 587

def messages
  @messages
end

Instance Method Details

#publish(data, attributes = {}) ⇒ Object

Add multiple messages to the topic. All messages added will be published at once. See Gcloud::Pubsub::Topic#publish



603
604
605
# File 'lib/gcloud/pubsub/topic.rb', line 603

def publish data, attributes = {}
  @messages << [data, attributes]
end

#to_gcloud_messages(message_ids) ⇒ Object

Create Message objects with message ids.



609
610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/gcloud/pubsub/topic.rb', line 609

def to_gcloud_messages message_ids #:nodoc:
  msgs = @messages.zip(Array(message_ids)).map do |arr, id|
    Message.from_gapi "data"       => arr[0],
                      "attributes" => jsonify_hash(arr[1]),
                      "messageId"  => id
  end
  # Return just one Message if a single publish,
  # otherwise return the array of Messages.
  if @mode == :single && msgs.count <= 1
    msgs.first
  else
    msgs
  end
end