Class: Gcpc::Publisher::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/gcpc/publisher/engine.rb,
lib/gcpc/publisher/engine/batch_engine.rb,
lib/gcpc/publisher/engine/chained_interceptor.rb

Defined Under Namespace

Classes: BatchEngine, ChainedInterceptor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(topic:, interceptors:) ⇒ Engine

Returns a new instance of Engine.

Parameters:

  • topic (Google::Cloud::Pubsub::Topic)
  • interceptors (<#publish>)


9
10
11
12
13
# File 'lib/gcpc/publisher/engine.rb', line 9

def initialize(topic:, interceptors:)
  @topic       = topic
  interceptors = interceptors.map { |i| (i.class == Class) ? i.new : i }
  @interceptor = ChainedInterceptor.new(interceptors)
end

Instance Attribute Details

#topicObject (readonly)

Returns the value of attribute topic.



15
16
17
# File 'lib/gcpc/publisher/engine.rb', line 15

def topic
  @topic
end

Instance Method Details

#publish(data, attributes = {}) ⇒ Object

Parameters:

  • data (String)
  • attributes (Hash) (defaults to: {})


19
20
21
22
23
24
25
26
# File 'lib/gcpc/publisher/engine.rb', line 19

def publish(data, attributes = {})
  d = data.dup
  a = attributes.dup

  @interceptor.intercept!(d, a) do |dd, aa|
    do_publish(dd, aa)
  end
end

#publish_async(data, attributes = {}, &block) ⇒ Object

Parameters:

  • data (String)
  • attributes (Hash) (defaults to: {})


37
38
39
40
41
42
43
44
# File 'lib/gcpc/publisher/engine.rb', line 37

def publish_async(data, attributes = {}, &block)
  d = data.dup
  a = attributes.dup

  @interceptor.intercept!(d, a) do |dd, aa|
    do_publish_async(dd, aa, &block)
  end
end

#publish_batch(&block) {|batch_engine| ... } ⇒ Object

Parameters:

  • block (Proc)

Yields:

  • (batch_engine)


29
30
31
32
33
# File 'lib/gcpc/publisher/engine.rb', line 29

def publish_batch(&block)
  batch_engine = BatchEngine.new(topic: @topic, interceptor: @interceptor)
  yield batch_engine
  batch_engine.flush
end