Class: Circuitry::Publisher

Inherits:
Object
  • Object
show all
Includes:
Concerns::Async, Services::SNS
Defined in:
lib/circuitry/publisher.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
    async: false,
    timeout: 15,
}.freeze

Instance Attribute Summary collapse

Attributes included from Concerns::Async

#async

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Services::SNS

#sns

Methods included from Concerns::Async

#async?, included, #process_asynchronously

Constructor Details

#initialize(options = {}) ⇒ Publisher

Returns a new instance of Publisher.



21
22
23
24
25
26
# File 'lib/circuitry/publisher.rb', line 21

def initialize(options = {})
  options = DEFAULT_OPTIONS.merge(options)

  self.async = options[:async]
  self.timeout = options[:timeout]
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



19
20
21
# File 'lib/circuitry/publisher.rb', line 19

def timeout
  @timeout
end

Class Method Details

.default_async_strategyObject



47
48
49
# File 'lib/circuitry/publisher.rb', line 47

def self.default_async_strategy
  Circuitry.config.publish_async_strategy
end

Instance Method Details

#publish(topic_name, object) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/circuitry/publisher.rb', line 28

def publish(topic_name, object)
  raise ArgumentError.new('topic_name cannot be nil') if topic_name.nil?
  raise ArgumentError.new('object cannot be nil') if object.nil?
  raise PublishError.new('AWS configuration is not set') unless can_publish?

  process = -> do
    Timeout.timeout(timeout) do
      topic = TopicCreator.find_or_create(topic_name)
      sns.publish(topic_arn: topic.arn, message: object.to_json)
    end
  end

  if async?
    process_asynchronously(&process)
  else
    process.call
  end
end