Class: Messaging::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/messaging/client.rb

Class Method Summary collapse

Class Method Details

.post_message(key, data = {}) ⇒ Object

Parameters:

  • key (String)
  • data (Hash) (defaults to: {})


20
21
22
23
24
25
26
27
28
29
# File 'lib/messaging/client.rb', line 20

def self.post_message(key,data={})
  begin
    publish_to_sns(key,data)
  rescue
    # on transition ignore errors. 
  end
  
  m = Message.new(key_name: key, data: data.to_json)
  m.create
end

.publish_to_sns(key, data) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/messaging/client.rb', line 31

def self.publish_to_sns(key,data)
  sns = Aws::SNS::Resource.new(
    region: 'us-east-1',
    access_key_id: SNS_KEY_ID,
    secret_access_key: SNS_SECRET_ACCESS_KEY
  )
  topic_arn = sns.topics.select{|t| t.arn.match(/([^:]*)$/)[0] == key.to_s }.first.try(:arn)
  topic = sns.topic(topic_arn)
  
  msg = { message: data.to_json }
  if data['account_name']
    msg[:message_attributes] = {
      account_name: {
        data_type: 'String',
        string_value: data['account_name']
      }
    }
  end
  
  topic.publish(msg)
end

.subscribe_to(key_name, url, secret_key = "") ⇒ Object

Parameters:

  • key_name (String)
  • url (String)
  • secret_key (String) (defaults to: "")


9
10
11
# File 'lib/messaging/client.rb', line 9

def self.subscribe_to(key_name,url,secret_key="")

end

.unsubscribe_from(key_name) ⇒ Object

Parameters:

  • key_name (String)


14
15
16
# File 'lib/messaging/client.rb', line 14

def self.unsubscribe_from(key_name)

end