Class: EventQ::Amazon::SNS

Inherits:
Object
  • Object
show all
Defined in:
lib/eventq/eventq_aws/sns.rb

Overview

Helper SNS class to handle the API calls

Constant Summary collapse

@@topic_arns =
Concurrent::Hash.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ SNS

Returns a new instance of SNS.



13
14
15
# File 'lib/eventq/eventq_aws/sns.rb', line 13

def initialize(client)
  @sns = client
end

Instance Attribute Details

#snsObject (readonly)

Returns the value of attribute sns.



11
12
13
# File 'lib/eventq/eventq_aws/sns.rb', line 11

def sns
  @sns
end

Instance Method Details

#aws_safe_name(name) ⇒ Object



62
63
64
# File 'lib/eventq/eventq_aws/sns.rb', line 62

def aws_safe_name(name)
  return name[0..79].gsub(/[^a-zA-Z\d_\-]/,'')
end

#create_topic_arn(event_type, region = nil) ⇒ Object

Create a TopicArn. if one already exists, it will return a pre-existing ARN from the cache. Even in the event of multiple threads trying to create one with AWS, AWS is idempotent and won’t create duplicates



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/eventq/eventq_aws/sns.rb', line 20

def create_topic_arn(event_type, region = nil)
  _event_type = EventQ.create_event_type(event_type)
  topic_key = "#{region}:#{_event_type}"

  arn = get_topic_arn(event_type, region)
  unless arn
    response = sns.create_topic(name: aws_safe_name(_event_type))
    arn = response.topic_arn
    @@topic_arns[topic_key] = arn
  end

  arn
end

#drop_topic(event_type, region = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/eventq/eventq_aws/sns.rb', line 51

def drop_topic(event_type, region = nil)
  topic_arn = get_topic_arn(event_type, region)
  sns.delete_topic(topic_arn: topic_arn)

  _event_type = EventQ.create_event_type(event_type)
  topic_key = "#{region}:#{_event_type}"
  @@topic_arns.delete(topic_key)

  true
end

#get_topic_arn(event_type, region = nil) ⇒ Object

Check if a TopicArn exists. This will check with AWS if necessary and cache the results if one is found



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/eventq/eventq_aws/sns.rb', line 37

def get_topic_arn(event_type, region = nil)
  _event_type = EventQ.create_event_type(event_type)
  topic_key = "#{region}:#{_event_type}"

  arn = @@topic_arns[topic_key]
  unless arn
    arn = find_topic(_event_type)

    @@topic_arns[topic_key] = arn if arn
  end

  arn
end