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.



571
572
573
574
575
576
577
# File 'lib/gcloud/pubsub/topic.rb', line 571

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



567
568
569
# File 'lib/gcloud/pubsub/topic.rb', line 567

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



583
584
585
# File 'lib/gcloud/pubsub/topic.rb', line 583

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

#to_gcloud_messages(message_ids) ⇒ Object

Create Message objects with message ids.



589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'lib/gcloud/pubsub/topic.rb', line 589

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