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!, #available_resources, #called_from, #create_logger, #deep_modify_keys_with, #format_error_message, #log_file, #logger, #modify_keys_with, #smart_retry, #task_exist?, #task_home, #task_path, #task_require_path, #to_camel, #to_hyph, #to_i_var, #to_pascal, #to_ruby_file_name, #to_snake, #update_message_body, #valid_json?, #valid_url?

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

Parameters

  • name String - the name of the Channel/Topic to be created

Returns Broadcast::Channel - representing the created channel



50
51
52
53
# File 'lib/cloud_powers/synapse/broadcast/broadcast.rb', line 50

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

#create_distributor(channel) ⇒ Object

Creates a connection point for 1..N nodes to create a connection with the Broadcast Not Implimented

Parameters

  • channel String

Notes This method is not implemented yet (V 0.2.7)



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

def create_distributor(channel)
  sns.create_application_platform()
end

#delete_channel!(channel) ⇒ Object

Deletes a topic from SNS-land

Parameters

  • channel <Broadcast::Channel>



59
60
61
# File 'lib/cloud_powers/synapse/broadcast/broadcast.rb', line 59

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

Parameters channel <Broadcast::Channel>



66
67
68
69
70
71
72
# File 'lib/cloud_powers/synapse/broadcast/broadcast.rb', line 66

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

#real_channelsObject

Lists the created topics in SNS.

Returns results <Array



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cloud_powers/synapse/broadcast/broadcast.rb', line 77

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

Parameters

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

    • :topic_arn - the ARN for the topic in AWS

    • :message - the message that should be broadcasted to whoever is listening on this Channel (AWS Topic)



97
98
99
100
101
102
103
104
105
106
# File 'lib/cloud_powers/synapse/broadcast/broadcast.rb', line 97

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

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

  sns.publish(package)
end