Class: Fog::Google::Pubsub::Subscription

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

Overview

Represents a Pubsub subscription resource

Instance Method Summary collapse

Instance Method Details

#acknowledge(messages) ⇒ Object

Acknowledges a list of received messages for this subscription.

Parameters:

See Also:



55
56
57
58
59
60
61
62
# File 'lib/fog/google/models/pubsub/subscription.rb', line 55

def acknowledge(messages)
  return if messages.empty?

  ack_ids = messages.map { |m| m.is_a?(ReceivedMessage) ? m.ack_id : m.to_s }

  service.acknowledge_subscription(name, ack_ids)
  nil
end

#destroyObject

Deletes this subscription on the remote service.



78
79
80
81
82
# File 'lib/fog/google/models/pubsub/subscription.rb', line 78

def destroy
  requires :name

  service.delete_subscription(name)
end

#pull(options = { :return_immediately => true, :max_messages => 10 }) ⇒ Array<Fog::Google::Pubsub::ReceivedMessage>

Pulls from this subscription, returning any available messages. By default, this method returns immediately with up to 10 messages. The option ‘return_immediately’ allows the method to block until a message is received, or the remote service closes the connection.

Note that this method automatically performs a base64 decode on the ‘data’ field.

Parameters:

  • options (Hash) (defaults to: { :return_immediately => true, :max_messages => 10 })

    options to modify the pull request

Options Hash (options):

  • :return_immediately (Boolean)

    If true, method returns after checking for messages. Otherwise the method blocks until one or more messages are available, or the connection is closed.

  • :max_messages (Number)

    maximum number of messages to receive

Returns:

See Also:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fog/google/models/pubsub/subscription.rb', line 30

def pull(options = { :return_immediately => true, :max_messages => 10 })
  requires :name

  data = service.pull_subscription(name, options).to_h

  return [] unless data.key?(:received_messages)
  # Turn into a list of ReceivedMessage, but ensure we perform a base64 decode first
  data[:received_messages].map do |recv_message|
    attrs = {
      :service => service,
      :subscription_name => name
    }.merge(recv_message)

    attrs[:message][:data] = Base64.decode64(recv_message[:message][:data]) if recv_message[:message].key?(:data)
    ReceivedMessage.new(attrs)
  end
end

#saveFog::Google::Pubsub::Subscription

Save this instance on the remove service.



68
69
70
71
72
73
# File 'lib/fog/google/models/pubsub/subscription.rb', line 68

def save
  requires :name, :topic

  data = service.create_subscription(name, topic, push_config, ack_deadline_seconds).to_h
  merge_attributes(data)
end