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
- .android_payload_for(message_value, data) ⇒ Object
- .content_from(argument) ⇒ Object
- .create_device_endpoint(device_identifier, platform, options = {}) ⇒ Object
- .create_ios_application(name, certs, private_key, options) ⇒ Object
- .create_sns_client(configuration = J7W1.configuration) ⇒ Object
- .destroy_application_endpoint(arn, options) ⇒ Object
- .destroy_device_endpoint(device_endpoint_arn, options = {}) ⇒ Object
- .ios_payload_for(message_value, data) ⇒ Object
- .ios_truncated_payload(data) ⇒ Object
- .message_content(content, platform: nil) ⇒ Object
- .payload_for(message_value, data, platform) ⇒ Object
- .push(endpoint_arn, platform, options = {}) ⇒ Object
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(, data) .merge!(data: data) { GCM: { data: (, 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, = {}) custom_user_data = [:custom_user_data] sns_configuration = [:sns_configuration] || J7W1.configuration sns_client = [: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, ) sandbox = !([:sandbox] == false) configuration = [:sns_configuration] || J7W1.configuration private_key = content_from private_key certs = content_from certs sns_client = [: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.account 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, ) configuration = [:sns_configuration] || J7W1.configuration client = [: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, = {}) sns_client = [: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(, data) prefix = J7W1.configuration.ios_endpoint.sandbox? ? :APNS_SANDBOX : :APNS content = { aps: (, platform: :ios), } content.merge! data: data if data {prefix => ios_truncated_payload(content)} end |
.ios_truncated_payload(data) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/j7w1/sns_push_client.rb', line 161 def ios_truncated_payload(data) payload = data.to_json # Truncation is skipped when the payload is sufficiently lightweight. return payload if (limit_break = payload.bytesize - APNX_MAX_PAYLOAD) >= 0 # Raise error if shortening of the alert cannot make the payload sufficiently short. size_to_reduce = limit_break + SHORTEN_REPLACEMENT_LENGTH raise "Payload is too heavy (#{payload.length})" unless data[:alert] && data[:alert].bytesize > size_to_reduce # Chopping the alert from its last -> first to avoid destroying the character's border # and keep its content as long as possible. chopped_length = 0 chopped_count = 0 while chopped_length < size_to_reduce chopped_length += data[:alert][-1].bytesize chopped_count += 1 end data[:alert][-chopped_count..-1] = SHORTEN_REPLACEMENT # Problem won't be occur because at least a character is truncated. data.to_json 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 (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(, data, platform) case platform.to_sym when :ios ios_payload_for(, data) when :android android_payload_for(, 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, = {}) return unless endpoint_arn && platform = [:message] badge = [:badge] sound = [:sound] data = [:data] sns_configuration = [:sns_configuration] sns_client = [:sns_client] = {} .merge!(message: ) unless .blank? .merge!(badge: badge) unless badge.blank? .merge!(sound: sound) unless sound.blank? payload = payload_for(, 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, ) sns_client = [: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, ) sns_client = [:sns_client] sns_client ||= create_sns_client(sns_configuration || J7W1.configuration) sns_client.topics[name] end |