Class: Gcpc::Publisher::TopicClient

Inherits:
Object
  • Object
show all
Defined in:
lib/gcpc/publisher/topic_client.rb

Constant Summary collapse

DEFAULT_CONNECT_TIMEOUT =
5

Instance Method Summary collapse

Constructor Details

#initialize(project_id:, topic_name:, credentials:, emulator_host:, connect_timeout: DEFAULT_CONNECT_TIMEOUT) ⇒ TopicClient

Returns a new instance of TopicClient.

Parameters:

  • project_id (String)
  • topic_name (String)
  • (Google::Cloud::Pubsub::Credentials, String, nil)
  • emulator_host (String, nil)


10
11
12
13
14
15
16
17
18
19
# File 'lib/gcpc/publisher/topic_client.rb', line 10

def initialize(project_id:, topic_name:, credentials:, emulator_host:, connect_timeout: DEFAULT_CONNECT_TIMEOUT)
  project = Google::Cloud::Pubsub.new(
    project_id:    project_id,
    credentials:   credentials,
    emulator_host: emulator_host,
  )
  @project         = project
  @topic_name      = topic_name
  @connect_timeout = connect_timeout
end

Instance Method Details

#getGoogle::Cloud::Pubsub::Topic

Returns:

  • (Google::Cloud::Pubsub::Topic)


22
23
24
25
26
27
28
29
30
# File 'lib/gcpc/publisher/topic_client.rb', line 22

def get
  t = nil
  Timeout.timeout(@connect_timeout) do
    t = @project.topic(@topic_name)
  end
  t
rescue Timeout::Error => e
  raise "Getting topic \"#{@topic_name}\" from project \"#{@project.project_id}\" timed out!"
end