Module: Smash::CloudPowers::Synapse::Broadcast

Includes:
AwsResources, Helper, Zenv
Included in:
Smash::CloudPowers::Synapse
Defined in:
lib/cloud_powers/synapse/broadcast/broadcast.rb

Defined Under Namespace

Classes: Channel

Instance Method Summary collapse

Methods included from Zenv

#env_vars, #file_tree_search, #i_vars, #project_root, #project_root=, #system_vars, #zfind

Methods included from Helper

#attr_map!, #called_from, #create_logger, #errors, #format_error_message, #log_file, #logger, #smart_retry, #task_path, #task_require_path, #to_camel, #to_i_var, #to_pascal, #to_ruby_file_name, #to_snake, #update_message_body, #valid_json?

Methods included from AwsResources

#ec2, #image, #kinesis, #region, #s3, #sns, #sqs

Methods included from Auth

creds, region

Instance Method Details

#create_channel!(name) ⇒ Object

Creates a point to connect to for information about a given topic @params: name : the name of the Channel/Topic to be created @returns: Broadcast::Channel representing the created channel



33
34
35
36
# File 'lib/cloud_powers/synapse/broadcast/broadcast.rb', line 33

def create_channel!(name)
  resp = sns.create_topic(name: name)
  Channel.new(nil, resp.topic_arn)
end

#delete_channel!(channel) ⇒ Object

Deletes a topic from SNS-land @params: channel Broadcast::Channel



40
41
42
# File 'lib/cloud_powers/synapse/broadcast/broadcast.rb', line 40

def delete_channel!(channel)
  sns.delete_topic(topic_arn: channel.arn)
end

#listen_on(channel) ⇒ Object

Creates a connection to the Broadcast so that new messages will be picked up @params: channel Broadcast::Channel



46
47
48
49
50
51
52
# File 'lib/cloud_powers/synapse/broadcast/broadcast.rb', line 46

def listen_on(channel)
  sns.subscribe(
    topic_arn:    channel.arn,
    protocol:     'application',
    endpoint:     channel.endpoint
  )
end

#real_channels ⇒ Object

Lists the created topics in SNS.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cloud_powers/synapse/broadcast/broadcast.rb', line 56

def real_channels
  results = []
  next_token = ''
  loop do
    resp = sns.list_topics((next_token.empty? ? {} : { next_token: next_token }))
    results.concat(resp.topics.map(&:topic_arn))
    next_token = (resp.next_token.empty? ? '' : resp.next_token)
    break if next_token.empty?
  end
  results
end

#send_broadcast(opts = {}) ⇒ Object

Send a message to a Channel using SNS#publish @params: [opts ]:

this includes all the keys AWS uses but for now it only has defaults
for topic_arn and the message


72
73
74
75
76
77
78
79
80
81
# File 'lib/cloud_powers/synapse/broadcast/broadcast.rb', line 72

def send_broadcast(opts = {})
  msg = opts.delete(:message) || ""

  package = {
    topic_arn:            "topicARN",
    message:              msg.to_json
  }.merge(opts)

  sns.publish(package)
end