class RisingDragon::SNS::Publisher

Attributes

use_cache[RW]

Public Class Methods

new(sns_client) click to toggle source
# File lib/rising_dragon/sns/publisher.rb, line 5
def initialize(sns_client)
  @sns_client = sns_client
  @topic_cache = {}
  @use_cache = true # default true :)
end

Public Instance Methods

publish(topic_name, event_type, data) click to toggle source
# File lib/rising_dragon/sns/publisher.rb, line 11
def publish(topic_name, event_type, data)
  event = create_event(event_type, data)

  topic = get_topic(topic_name)
  @sns_client.publish(topic_arn: topic.topic_arn, message: event.to_json)
end

Private Instance Methods

create_event(type, data) click to toggle source
# File lib/rising_dragon/sns/publisher.rb, line 20
def create_event(type, data)
  ::RisingDragon::Event.new(id: uuid, timestamp: unixtime, type: type, data: data)
end
create_topic(topic_name) click to toggle source
# File lib/rising_dragon/sns/publisher.rb, line 38
def create_topic(topic_name)
  @sns_client.create_topic(name: topic_name)
end
get_topic(topic_name) click to toggle source
# File lib/rising_dragon/sns/publisher.rb, line 32
def get_topic(topic_name)
  return create_topic(topic_name) unless @use_cache

  @topic_cache[topic_name] ||= create_topic(topic_name)
end
unixtime() click to toggle source
# File lib/rising_dragon/sns/publisher.rb, line 28
def unixtime
  (Time.now.to_f * 1000).to_i
end
uuid() click to toggle source
# File lib/rising_dragon/sns/publisher.rb, line 24
def uuid
  SecureRandom.uuid
end