Module: J7W1::SNSPushClient
- Defined in:
- lib/j7w1/sns_push_client.rb
Constant Summary collapse
- APNS_MAX_MESSAGE_SIZE =
40
- 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
- .message_content(content, platform: nil) ⇒ Object
- .payload_for(message_value, data, platform) ⇒ Object
- .push(endpoint_arn, platform, options = {}) ⇒ Object
Class Method Details
.android_payload_for(message_value, data) ⇒ Object
148 149 150 151 152 153 154 155 |
# File 'lib/j7w1/sns_push_client.rb', line 148 def android_payload_for(, data) .merge!(data: data) { GCM: { data: (, platform: :android), }.to_json } end |
.content_from(argument) ⇒ Object
168 169 170 171 172 173 174 175 |
# File 'lib/j7w1/sns_push_client.rb', line 168 def content_from(argument) case argument when IO argument.read when String argument end end |
.create_device_endpoint(device_identifier, platform, options = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/j7w1/sns_push_client.rb', line 55 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
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/j7w1/sns_push_client.rb', line 28 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
48 49 50 51 52 53 |
# File 'lib/j7w1/sns_push_client.rb', line 48 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
81 82 83 84 85 86 87 88 |
# File 'lib/j7w1/sns_push_client.rb', line 81 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
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/j7w1/sns_push_client.rb', line 132 def ios_payload_for(, data) prefix = J7W1.configuration.ios_endpoint.sandbox? ? :APNS_SANDBOX : :APNS if [:message] && [:message].length > APNS_MAX_MESSAGE_SIZE [:message] = [:message][0...(APNS_MAX_MESSAGE_SIZE - 3)] + '...' end content = { aps: (, platform: :ios), } content.merge! data: data if data {prefix => content.to_json} end |
.message_content(content, platform: nil) ⇒ Object
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/j7w1/sns_push_client.rb', line 157 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
123 124 125 126 127 128 129 130 |
# File 'lib/j7w1/sns_push_client.rb', line 123 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
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/j7w1/sns_push_client.rb', line 90 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 |