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

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

Defined Under Namespace

Classes: Channel

Instance Method Summary collapse

Methods included from Zenv

#env_vars, #i_vars, #lsof_cwd, #pid, #proc_cwd, #process_search, #project_root, #project_root=, #ps_cwd, #system_vars, #zfind, #zselect

Methods included from Helpers

#create_logger, #log_file, #logger

Methods included from PathHelp

#common_delimiter, #expand_path, #file_exists?, #file_search, #filename?, #job_exist?, #job_path, #job_require_path, #path_search, #paths_gcd, #paths_lcd, #to_path, #to_pathname, #to_realpath, #touch, #zlib_path

Methods included from LogicHelp

#attr_map, #called_from, #i_var_hash, #instance_attr_accessor, #smart_retry, #update_message_body

Methods included from LangHelp

#deep_modify_keys_with, #extract!, #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

creds, region

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_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_channelsObject

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