Module: J7W1::SNSPushClient

Defined in:
lib/j7w1/sns_push_client.rb

Constant Summary collapse

APNS_MAX_PAYLOAD =
256
SHORTEN_REPLACEMENT =
'...'.freeze
SHORTEN_REPLACEMENT_LENGTH =
SHORTEN_REPLACEMENT.length
INPUT_PAYLOAD_TABLE =
{
  ios: {
    message: :alert,
    badge: :badge,
    sound: :sound,
  }.freeze,
  android: {
    message: :message,
    badge: :badge,
    sound: :sound,
    data: :data,
  }.freeze,
}.freeze
DEFAULT_SOUND_VALUE =
{
  android: true,
  ios: 'default',
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.android_payload_for(message_value, data) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/j7w1/sns_push_client.rb', line 186

def android_payload_for(message_value, data)
  message_value.merge!(data: data)
  {
      GCM: {
          data: message_content(message_value, platform: :android),
      }.to_json
  }
end

.content_from(argument) ⇒ Object



206
207
208
209
210
211
212
213
# File 'lib/j7w1/sns_push_client.rb', line 206

def content_from(argument)
  case argument
    when IO
      argument.read
    when String
      argument
  end
end

.create_device_endpoint(device_identifier, platform, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/j7w1/sns_push_client.rb', line 58

def create_device_endpoint(device_identifier, platform, options = {})
  custom_user_data = options[:custom_user_data]
  sns_configuration = options[:sns_configuration] || J7W1.configuration
  sns_client = options[:sns_client]

  sns_client ||= create_sns_client(sns_configuration)

  app_arn =
      case platform
        when :ios
          sns_configuration.ios_endpoint.arn
        when :android
          sns_configuration.android_endpoint.arn
        else

      end

  endpoint =
    sns_client.client.create_platform_endpoint(
      platform_application_arn: app_arn,
      token: device_identifier,
      custom_user_data: custom_user_data
    )
  endpoint[:endpoint_arn]
end

.create_ios_application(name, certs, private_key, options) ⇒ Object



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

def create_ios_application(name, certs, private_key, options)
  sandbox = !(options[:sandbox] == false)
  configuration = options[:sns_configuration] || J7W1.configuration

  private_key = content_from private_key
  certs = content_from certs

  sns_client = options[:sns_client] || create_sns_client(configuration)

  application_endpoint =
      sns_client.client.create_platform_application(
        name: name, platform: (sandbox ? 'APNS_SANDBOX' : 'APNS'),
        attributes: {
          'PlatformCredential' => private_key,
          'PlatformPrincipal' => certs,
        }
      )
  application_endpoint[:platform_application_arn]
end

.create_sns_client(configuration = J7W1.configuration) ⇒ Object



3
4
5
# File 'lib/j7w1/sns_push_client.rb', line 3

def create_sns_client(configuration = J7W1.configuration)
  AWS::SNS.new configuration.
end

.destroy_application_endpoint(arn, options) ⇒ Object



51
52
53
54
55
56
# File 'lib/j7w1/sns_push_client.rb', line 51

def destroy_application_endpoint(arn, options)
  configuration = options[:sns_configuration] || J7W1.configuration
  client = options[:sns_client] || create_sns_client(configuration)

  client.client.delete_platform_application(platform_application_arn: arn)
end

.destroy_device_endpoint(device_endpoint_arn, options = {}) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/j7w1/sns_push_client.rb', line 84

def destroy_device_endpoint(device_endpoint_arn, options = {})
  sns_client = options[:sns_client]
  sns_client ||= create_sns_client(sns_configuration || J7W1.configuration)

  sns_client.client.delete_endpoint(endpoint_arn: device_endpoint_arn)
rescue
  nil
end

.ios_payload_for(message_value, data) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/j7w1/sns_push_client.rb', line 149

def ios_payload_for(message_value, data)
  prefix = J7W1.configuration.ios_endpoint.sandbox? ?
      :APNS_SANDBOX : :APNS

  content = {
      aps: message_content(message_value, platform: :ios),
  }
  content.merge! data: data if data

  {prefix => ios_truncated_payload(content)}
end

.message_content(content, platform: nil) ⇒ Object



195
196
197
198
199
200
201
202
203
204
# File 'lib/j7w1/sns_push_client.rb', line 195

def message_content(content, platform: nil)
  table = INPUT_PAYLOAD_TABLE[platform]

  content[:sound] = DEFAULT_SOUND_VALUE[platform] if content[:sound] == true

  table.keys.reduce({}) do |h, key|
    h[table[key]] = content[key]
    h
  end
end

.payload_for(message_value, data, platform) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/j7w1/sns_push_client.rb', line 140

def payload_for(message_value, data, platform)
  case platform.to_sym
    when :ios
      ios_payload_for(message_value, data)
    when :android
      android_payload_for(message_value, data)
  end
end

.push(endpoint_arn, platform, options = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/j7w1/sns_push_client.rb', line 107

def push(endpoint_arn, platform, options = {})
  return unless endpoint_arn && platform

  message = options[:message]
  badge = options[:badge]
  sound = options[:sound]
  data = options[:data]
  sns_configuration = options[:sns_configuration]
  sns_client = options[:sns_client]

  message_value = {}
  message_value.merge!(message: message) unless message.blank?
  message_value.merge!(badge: badge) unless badge.blank?
  message_value.merge!(sound: sound) unless sound.blank?

  payload = payload_for(message_value, data, platform)

  sns_client ||= create_sns_client(sns_configuration || J7W1.configuration)
  client = sns_client.client

  enabled = (client.get_endpoint_attributes(endpoint_arn: endpoint_arn)[:attributes]['Enabled'] == 'true')
  unless enabled
    client.set_endpoint_attributes endpoint_arn: endpoint_arn, attributes: {'Enabled' => 'true'}
  end

  client.publish(
      target_arn: endpoint_arn,
      message: payload.to_json,
      message_structure: 'json',
  )
end

Instance Method Details

#create_topic(name, options) ⇒ Object



93
94
95
96
97
98
# File 'lib/j7w1/sns_push_client.rb', line 93

def create_topic(name, options)
  sns_client = options[:sns_client]
  sns_client ||= create_sns_client(sns_configuration || J7W1.configuration)

  sns_client.topics.create name
end

#subscribe_topic(topic_name, endpoint_arn, options) ⇒ Object



100
101
102
103
104
105
# File 'lib/j7w1/sns_push_client.rb', line 100

def subscribe_topic(topic_name, endpoint_arn, options)
  sns_client = options[:sns_client]
  sns_client ||= create_sns_client(sns_configuration || J7W1.configuration)

  sns_client.topics[name]
end