class SnsHelper

This is the main file of sns_helper gem

Attributes

sns[R]

Public Class Methods

hi() click to toggle source

say hi to the world

Example: >> SnsHelper.hi

> This is hello world from sns_helper!

# File lib/sns_helper.rb, line 13
def self.hi
  'This is hello world from sns_helper!'
end
new(region: 'ap-northeast-1') click to toggle source

By initialize a SnsHelper, you can then simply use the send_message method to deliver your send_message Remember to set up the aws creditical for AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY

Example: >> service = SnsHelper.new

> #<SnsHelper:0x00007fe6ee435e40 @sns=#<Aws::SNS::Client>>

# File lib/sns_helper.rb, line 24
def initialize(region: 'ap-northeast-1')
  @sns = Aws::SNS::Client.new(
    region: region,
    access_key_id: ENV['AWS_ACCESS_KEY_ID'],
    secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
  )
end

Public Instance Methods

send_message(phone_number, message) click to toggle source

Send the message to a mobile phone_number

Example: >> service = SnsHelper.new >> service.send_message('+886912456789', 'This is just a test message')

# File lib/sns_helper.rb, line 51
def send_message(phone_number, message)
  if valid_phone_number(phone_number)
    sns.publish(phone_number: phone_number, message: message)
  else
    false
  end
end
sms_attr() click to toggle source
# File lib/sns_helper.rb, line 32
def sms_attr
  sns.get_sms_attributes
end
sms_type=(type: 'Promotional') click to toggle source
# File lib/sns_helper.rb, line 36
def sms_type=(type: 'Promotional')
  return false unless %w[Promotional Transactional].include?(type)
  sns.set_sms_attributes(
    attributes: {
      DefaultSMSType: type
    }
  )
end
valid_phone_number(phone_number) click to toggle source

The current pattern is only for Taiwan

# File lib/sns_helper.rb, line 60
def valid_phone_number(phone_number)
  return false unless /^\+8869\d{8}/ =~ phone_number
  true
end