Class: Fog::Google::Pubsub::Topic

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/google/models/pubsub/topic.rb

Overview

Represents a Pubsub topic resource

Instance Method Summary collapse

Instance Method Details

#createFog::Google::Pubsub::Topic

Creates this topic resource on the service.

Returns:



15
16
17
18
19
20
# File 'lib/fog/google/models/pubsub/topic.rb', line 15

def create
  requires :name

  service.create_topic(name)
  self
end

#destroyFog::Google::Pubsub::Topic

Deletes this topic resource on the service.

Returns:



25
26
27
28
29
30
# File 'lib/fog/google/models/pubsub/topic.rb', line 25

def destroy
  requires :name

  service.delete_topic(name)
  self
end

#publish(messages) ⇒ Array<String>

Publish a message to this topic. This method will automatically encode the message via base64 encoding.

Parameters:

  • messages (Array<Hash{String => String}, #to_s>)

    list of messages to send; if it’s a hash, then the value of key “data” will be sent as the message. Additionally, if the hash contains a value for key “attributes”, then they will be sent as well as attributes on the message.

Returns:

  • (Array<String>)

    list of message ids



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fog/google/models/pubsub/topic.rb', line 41

def publish(messages)
  requires :name

  # Ensure our messages are base64 encoded
  encoded_messages = []

  messages.each do |message|
    encoded_message = {}
    if message.is_a?(Hash)
      if message.key?("data")
        encoded_message[:data] = Base64.strict_encode64(message["data"])
      end
    else
      encoded_message[:data] = Base64.strict_encode64(message.to_s)
    end

    encoded_messages << encoded_message
  end

  service.publish_topic(name, encoded_messages).to_h[:message_ids]
end

#saveFog::Google::Pubsub::Topic

Save the instance (does the same thing as #create)

Returns:



65
66
67
# File 'lib/fog/google/models/pubsub/topic.rb', line 65

def save
  create
end