Class: Stenotype::Adapters::GoogleCloud

Inherits:
Base
  • Object
show all
Defined in:
lib/stenotype/adapters/google_cloud.rb

Overview

An adapter implementing method #publish to send data to Google Cloud PubSub

Examples:

A general usage within some method in the class

class EventEmittingClass
  def method_emitting_enent
    result_of_calculations = collect_some_data
    gc_adapter.publish(result_of_calculation, additional: :data, more: :data)
    result_of_calculations
  end

  def gc_adapter
    Stenotype::Adapters::GoogleCloud.new
  end
end

Overriding a client

class EventEmittingClass
  def method_emitting_enent
    result_of_calculations = collect_some_data
    gc_adapter.publish(result_of_calculation, additional: :data, more: :data)
    result_of_calculations
  end

  def gc_adapter
    Stenotype::Adapters::GoogleCloud.new(client: CustomGcClient.new)
  end
end

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Stenotype::Adapters::Base

Instance Method Details

#auto_initialize!Object

If not called both client and topic are lazy initialized on first call (if not passed to #initialize). #auto_initialize! is going to explicitly initialize both client and topic.



74
75
76
77
# File 'lib/stenotype/adapters/google_cloud.rb', line 74

def auto_initialize!
  client
  topic
end

#flush!Object

Flushes the topic's async queue



62
63
64
65
66
67
# File 'lib/stenotype/adapters/google_cloud.rb', line 62

def flush!
  # a publisher might be uninitialized until the first event is published
  return unless topic.async_publisher

  topic.async_publisher.stop.wait!
end

#publish(event_data, **additional_attrs) ⇒ Object

Examples:

With default client

google_cloud_adapter = Stenotype::Adapters::GoogleCloud.new
# publishes to default client
google_cloud_adapter.publish({ event: :data }, { additional: :data })

With client override

google_cloud_adapter = Stenotype::Adapters::GoogleCloud.new(CustomGCClient.new)
# publishes to default CustomGCClient
google_cloud_adapter.publish({ event: :data }, { additional: :data })

Raises:



51
52
53
54
55
56
57
# File 'lib/stenotype/adapters/google_cloud.rb', line 51

def publish(event_data, **additional_attrs)
  if config.async
    publish_async(event_data, **additional_attrs)
  else
    publish_sync(event_data, **additional_attrs)
  end
end