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.



596
597
598
599
600
601
602
# File 'lib/gcloud/pubsub/topic.rb', line 596

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



592
593
594
# File 'lib/gcloud/pubsub/topic.rb', line 592

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



608
609
610
# File 'lib/gcloud/pubsub/topic.rb', line 608

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

#to_gcloud_messages(message_ids) ⇒ Object

Create Message objects with message ids.



614
615
616
617
618
619
620
621
622
623
624
625
626
627
# File 'lib/gcloud/pubsub/topic.rb', line 614

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