Module: Arc::Events

Defined in:
lib/architect/events.rb

Class Method Summary collapse

Class Method Details

.publish(name:, payload:) ⇒ Object

publish a message to an SNS Topic



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/architect/events.rb', line 12

def self.publish(name:, payload:)
  if ENV['NODE_ENV'] == 'testing'
    headers = {'content-type':'application/json'}
    uri = URI('https://localhost:3334/events')
    http = Net::HTTP.new(uri.host, uri.port)
    req = Net::HTTP::Post.new(uri.path, headers)
    req.body = {'name': name, 'payload': payload}.to_json
    http.request(req).read_body
  else
    arc = Arc.reflect
    arn = arc['events'][name]
    sns = Aws::SNS::Client.new
    sns.publish({topic_arn: arn, message: JSON.generate(payload)})
  end
rescue => e
  "arc.events.publish failed #{e}"
end