Module: Smash::CloudPowers::Synapse::Broadcast
- Includes:
- AwsResources, Helper, Helpers, Zenv
- Included in:
- Smash::CloudPowers::Synapse
- Defined in:
- lib/cloud_powers/synapse/broadcast.rb,
lib/cloud_powers/synapse/broadcast/channel.rb,
lib/cloud_powers/synapse/broadcast/broadcast.rb
Defined Under Namespace
Classes: Channel
Instance Method Summary collapse
-
#channel_name(arg) ⇒ Object
This method can be used to parse a queue name from its address.
-
#create_channel(name, **config) ⇒ Object
Creates a point to connect to for information about a given topic.
-
#create_channel!(name) ⇒ Object
Creates a point to connect to for information about a given topic.
-
#create_distributor(channel) ⇒ Object
Creates a connection point for 1..N nodes to create a connection with the Broadcast Not Implimented.
-
#delete_channel!(channel) ⇒ Object
Deletes a topic from SNS-land.
-
#listen_on(channel) ⇒ Object
Creates a connection to the Broadcast so that new messages will be picked up.
-
#real_channels ⇒ Object
Lists the created topics in SNS.
-
#send_broadcast(opts = {}) ⇒ Object
Send a message to a Channel using SNS#publish.
Methods included from Zenv
#env_vars, #file_tree_search, #i_vars, #project_root, #project_root=, #system_vars, #zfind
Methods included from Helpers
#create_logger, #log_file, #logger
Methods included from PathHelp
#job_exist?, #job_home, #job_path, #job_require_path
Methods included from LogicHelp
#attr_map, #called_from, #instance_attr_accessor, #smart_retry, #update_message_body
Methods included from LangHelp
#deep_modify_keys_with, #find_and_remove, #format_error_message, #from_json, #modify_keys_with, #to_basic_hash, #to_camel, #to_hyph, #to_i_var, #to_pascal, #to_ruby_file_name, #to_snake, #valid_json?, #valid_url?
Methods included from AwsResources
#ec2, #image, #kinesis, #queue_poller, #region, #s3, #sns, #sqs
Methods included from Auth
Instance Method Details
#channel_name(arg) ⇒ Object
This method can be used to parse a queue name from its address. It can be handy if you need the name of a queue but you don’t want the overhead of creating a QueueResource object.
Parameters
-
url
String
Returns String
Example
board_name('https://sqs.us-west-53.amazonaws.com/001101010010/fooBar')
=> foo_bar_board
24 25 26 27 |
# File 'lib/cloud_powers/synapse/broadcast.rb', line 24 def channel_name(arg) base_name = to_snake(arg.to_s.split('/').last) %r{_channel$} =~ base_name ? base_name : "#{base_name}_channel" end |
#create_channel(name, **config) ⇒ 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
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/cloud_powers/synapse/broadcast.rb', line 48 def create_channel(name, **config) channel_resource = Smash::CloudPowers::Synapse::Broadcast::Channel.create!( name: name, client: sns, **config ) self.attr_map(channel_resource.call_name => channel_resource) do |attribute, resource| instance_attr_accessor attribute resource end channel_resource end |
#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)
37 38 39 |
# File 'lib/cloud_powers/synapse/broadcast.rb', line 37 def create_distributor(channel) sns.create_application_platform() end |
#delete_channel!(channel) ⇒ Object
Deletes a topic from SNS-land
Parameters
-
channel <Broadcast::Channel>
66 67 68 |
# File 'lib/cloud_powers/synapse/broadcast.rb', line 66 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>
73 74 75 76 77 78 79 |
# File 'lib/cloud_powers/synapse/broadcast.rb', line 73 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.
Returns results <Array
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cloud_powers/synapse/broadcast.rb', line 84 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)
-
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/cloud_powers/synapse/broadcast.rb', line 104 def send_broadcast(opts = {}) msg = opts.delete(:message) || "" package = { topic_arn: "topicARN", message: msg.to_json }.merge(opts) sns.publish(package) end |