class PushToSNS::SendPushNotification

Attributes

configuration[RW]
device[RW]

Public Class Methods

new(device, configuration = PushToSNS.configuration) click to toggle source
# File lib/push_to_sns/send_push_notification.rb, line 3
def initialize(device, configuration = PushToSNS.configuration)
  self.device = device
  self.configuration = configuration
end

Public Instance Methods

perform(payload) click to toggle source
# File lib/push_to_sns/send_push_notification.rb, line 8
def perform(payload)
  ensure_endpoint_arn_is_enabled
  notification = build_push_notification(payload)

  response = AWS.sns.client.publish(
    message_structure: "json",
    message: notification.message.to_json,
    target_arn: configuration.apply(:read_endpoint_arn, device)
  )

  response[:message_id]
rescue AWS::SNS::Errors::EndpointDisabled => _exception
  perform(payload)
end

Private Instance Methods

build_push_notification(payload) click to toggle source
# File lib/push_to_sns/send_push_notification.rb, line 27
def build_push_notification(payload)
  case configuration.apply(:read_source, device)
  when "ios"
    IosPushNotification.new(device, payload, configuration)
  when "android"
    AndroidPushNotification.new(device, payload, configuration)
  end
end
device_token() click to toggle source
# File lib/push_to_sns/send_push_notification.rb, line 60
def device_token
  @device_token ||= configuration.apply(:read_device_token, device)
end
enable_endpoint_arn() click to toggle source
# File lib/push_to_sns/send_push_notification.rb, line 50
def enable_endpoint_arn
  AWS.sns.client.set_endpoint_attributes(
    endpoint_arn: endpoint_arn,
    attributes: {
      "Enabled" => "True",
      "Token" => device_token
    }
  )[:endpoint_arn]
end
endpoint_arn() click to toggle source
# File lib/push_to_sns/send_push_notification.rb, line 64
def endpoint_arn
  @endpoint_arn ||= configuration.apply(:read_endpoint_arn, device)
end
ensure_endpoint_arn_is_enabled() click to toggle source
# File lib/push_to_sns/send_push_notification.rb, line 36
def ensure_endpoint_arn_is_enabled
  attributes = get_endpoint_attributes

  if attributes["Enabled"].downcase == "false" || attributes["Token"] != device_token
    enable_endpoint_arn
  end
end
get_endpoint_attributes() click to toggle source
# File lib/push_to_sns/send_push_notification.rb, line 44
def get_endpoint_attributes
  AWS.sns.client.get_endpoint_attributes(
    endpoint_arn: endpoint_arn
  )[:attributes]
end