push_to_sns 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +6 -0
- data/.travis.yml +4 -0
- data/Gemfile +6 -0
- data/Guardfile +14 -0
- data/LICENSE.txt +21 -0
- data/README.md +157 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/push_to_sns.rb +25 -0
- data/lib/push_to_sns/android_push_notification.rb +17 -0
- data/lib/push_to_sns/basic_push_notification.rb +13 -0
- data/lib/push_to_sns/configuration.rb +24 -0
- data/lib/push_to_sns/ios_push_notification.rb +20 -0
- data/lib/push_to_sns/messages.rb +43 -0
- data/lib/push_to_sns/push_notifier.rb +58 -0
- data/lib/push_to_sns/send_push_notification.rb +66 -0
- data/lib/push_to_sns/setup_push_notification.rb +23 -0
- data/lib/push_to_sns/version.rb +3 -0
- data/lib/rails/generators/push_to_sns/install_generator.rb +15 -0
- data/lib/rails/generators/push_to_sns/notifier_generator.rb +22 -0
- data/lib/rails/generators/push_to_sns/templates/config/initializers/push_to_sns.rb +11 -0
- data/lib/rails/generators/push_to_sns/templates/notifier.rb +14 -0
- data/push_to_sns.gemspec +30 -0
- metadata +181 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c663593e8fe0ca6b58a4420006c2bda59291d811
|
4
|
+
data.tar.gz: a5496eea94b884869c8b2eb548a2c4981d9e5024
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c9c75d2f9c2337ff69c32657025b1f6aa3d3958912f4f8d562d2f0e0c582aa60b009ef58960caefebc3b5c6216737a90190fa7a61017287a8d82b62775af4641
|
7
|
+
data.tar.gz: 0af4ddc0e713807bbe1213bcfb7e2318c541d78182de83594f29c44ca0660c9a29b5bb91352ccb5bea463736bedce2daf622db12623730fc5f7774c075ae1e16
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
2
|
+
require "guard/rspec/dsl"
|
3
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
4
|
+
|
5
|
+
# RSpec files
|
6
|
+
rspec = dsl.rspec
|
7
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
8
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
9
|
+
watch(rspec.spec_files)
|
10
|
+
|
11
|
+
# Ruby files
|
12
|
+
ruby = dsl.ruby
|
13
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
14
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 juliogarciag
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
# PushToSns
|
2
|
+
|
3
|
+
PushToSns is an opinionated gem to allow pushing push notifications through SNS. It assumes you want to use a type and a message in your payload, that you already configured the AWS gem and that your message is encoded as json. If this is your case, this will make your life easier.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem "push_to_sns"
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Setup
|
22
|
+
|
23
|
+
1. First, configure the [AWS gem](https://github.com/aws/aws-sdk-ruby/tree/aws-sdk-v1).
|
24
|
+
2. Now, run the following command if you use rails:
|
25
|
+
|
26
|
+
```
|
27
|
+
$ rails generate push_to_sns:install
|
28
|
+
```
|
29
|
+
|
30
|
+
This will generate the file `app/initializers/push_to_sns.rb` with the following content: (if you're not using rails, just paste the code in a place to be executed once on startup)
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
PushToSNS.configure do
|
34
|
+
read_device_id { |device| }
|
35
|
+
read_source { |device| }
|
36
|
+
read_endpoint_arn { |device| }
|
37
|
+
read_platform_arn { |device| }
|
38
|
+
read_ios_apns { |device| }
|
39
|
+
|
40
|
+
save_endpoint_arn do |device, endpoint_arn|
|
41
|
+
end
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
3. This initializer is only full of some empty methods. Why? Because we don't want to enforce a `Device` model or couple heavily with `ActiveRecord`, you can specify the way you want us to read and store device identifiers and SNS endpoints in your model. We don't want to be an obstacle in your way. But, anyway, we can suggest the following:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
PushToSNS.configure do
|
49
|
+
read_device_id { |device| device.device_id }
|
50
|
+
read_source { |device| device.source }
|
51
|
+
read_endpoint_arn { |device| device.endpoint_arn }
|
52
|
+
read_platform_arn { |device| ENV["SNS_#{device.source.upcase}_PLATFORM_ARN"] }
|
53
|
+
read_ios_apns { ENV["APNS"] }
|
54
|
+
|
55
|
+
save_endpoint_arn do |device, endpoint_arn|
|
56
|
+
device.update(endpoint_arn: endpoint_arn)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
### Device Setup
|
62
|
+
|
63
|
+
This is up to you in some way. For example, you can have a controller to receive `device_id`s from a mobile device and you want to register the device to be able to receive push notifications. You can do it in this way:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
class DevicesController < ApiController
|
67
|
+
def create
|
68
|
+
device = Device.create(device_params)
|
69
|
+
PushToSNS.setup_device(device)
|
70
|
+
respond_with device
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def device_params
|
76
|
+
params.require(:device).permit(:device_id, :source)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
`PushToSNS.setup_device` will get the device identifier from the object passed and register it on SNS. Then, it will store the endpoint returned using the code specified in `save_endpoint_arn` configuration.
|
82
|
+
|
83
|
+
### Create a Push Notifier
|
84
|
+
|
85
|
+
Now, imagine you have a football match and you want to notify users about a new goal being made. First, create a push notification object. In Rails, you can do this:
|
86
|
+
|
87
|
+
```
|
88
|
+
rails generate push_to_sns:notifier new_goal goal
|
89
|
+
```
|
90
|
+
|
91
|
+
The parameter `new_goal` will be used to set the notifier's name and the next parameters (`goal`, for example) will be used to add some attribute accessors to the new notifier. This will help with the initialization of the notifier.
|
92
|
+
|
93
|
+
It will create the following object in `app/push_notifiers/new_goal_push_notifier.rb`:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
class NewGoalPushNotifier < PushToSNS::PushNotifier
|
97
|
+
message "New Goal"
|
98
|
+
type :new_goal
|
99
|
+
|
100
|
+
attr_accessor :goal
|
101
|
+
|
102
|
+
def devices
|
103
|
+
fail "Method Not Implemented: devices"
|
104
|
+
end
|
105
|
+
|
106
|
+
def notification(device)
|
107
|
+
fail "Method Not Implemented: notification"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
Now, we need to answer three questions:
|
113
|
+
|
114
|
+
- What devices do we need to notify of this goal?
|
115
|
+
- What is the data we need to send?
|
116
|
+
|
117
|
+
To answer these questions, we need to implement the method `devices` and `notification`. For example, we can have these two methods:
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
def devices
|
121
|
+
goal.match.subscribed_devices
|
122
|
+
end
|
123
|
+
|
124
|
+
def notification(device)
|
125
|
+
{
|
126
|
+
scorer: goal.scorer,
|
127
|
+
team: goal.team,
|
128
|
+
match_id: goal.match_id,
|
129
|
+
new_result: goal.new_result,
|
130
|
+
is_favorite: device.user.favorite_teams.include?(goal.team)
|
131
|
+
}
|
132
|
+
end
|
133
|
+
```
|
134
|
+
|
135
|
+
|
136
|
+
### Send the push notification
|
137
|
+
|
138
|
+
Now, in whatever place you want to, deliver the notifier by creating a push notifier with the given arguments (those attribute accesors that we set before) and calling `deliver` after that:
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
NewGoalPushNotifier.new(goal: goal).deliver
|
142
|
+
```
|
143
|
+
|
144
|
+
## Development
|
145
|
+
|
146
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
147
|
+
|
148
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
149
|
+
|
150
|
+
## Contributing
|
151
|
+
|
152
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/push_to_sns.
|
153
|
+
|
154
|
+
|
155
|
+
## License
|
156
|
+
|
157
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "push_to_sns"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/push_to_sns.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "json"
|
2
|
+
require "deep_merge/rails_compat"
|
3
|
+
require_relative "./push_to_sns/version"
|
4
|
+
require_relative "./push_to_sns/messages"
|
5
|
+
require_relative "./push_to_sns/configuration"
|
6
|
+
require_relative "./push_to_sns/setup_push_notification"
|
7
|
+
require_relative "./push_to_sns/push_notifier"
|
8
|
+
require_relative "./push_to_sns/send_push_notification"
|
9
|
+
require_relative "./push_to_sns/basic_push_notification"
|
10
|
+
require_relative "./push_to_sns/ios_push_notification"
|
11
|
+
require_relative "./push_to_sns/android_push_notification"
|
12
|
+
|
13
|
+
module PushToSNS
|
14
|
+
def self.configure(&block)
|
15
|
+
configuration.instance_eval(&block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.configuration
|
19
|
+
@configuration ||= PushToSNS::Configuration.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.setup_device(device)
|
23
|
+
PushToSNS::SetupPushNotification.new(device).perform
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module PushToSNS
|
2
|
+
class AndroidPushNotification < BasicPushNotification
|
3
|
+
DEFAULT_MESSAGE = "Android Push Notification"
|
4
|
+
|
5
|
+
def message
|
6
|
+
{ GCM: { data: default_payload.deeper_merge(payload) }.to_json }
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def default_payload
|
12
|
+
{
|
13
|
+
message: payload[:message] || DEFAULT_MESSAGE
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module PushToSNS
|
2
|
+
class BasicPushNotification
|
3
|
+
def initialize(device, payload = {}, configuration = PushToSNS.configuration)
|
4
|
+
self.device = device
|
5
|
+
self.payload = payload
|
6
|
+
self.configuration = configuration
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
attr_accessor :device, :payload, :configuration
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module PushToSNS
|
2
|
+
class Configuration
|
3
|
+
PROC_PROPERTIES = %i(
|
4
|
+
read_device_id
|
5
|
+
read_source
|
6
|
+
read_endpoint_arn
|
7
|
+
read_platform_arn
|
8
|
+
read_ios_apns
|
9
|
+
save_endpoint_arn
|
10
|
+
)
|
11
|
+
|
12
|
+
PushToSNS::Configuration::PROC_PROPERTIES.each do |method_name|
|
13
|
+
attr_accessor :"#{method_name}_proc"
|
14
|
+
|
15
|
+
define_method(method_name) do |&block|
|
16
|
+
public_send(:"#{method_name}_proc=", block)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def apply(proc_property, *arguments)
|
21
|
+
public_send(:"#{proc_property}_proc").call(*arguments)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module PushToSNS
|
2
|
+
class IosPushNotification < BasicPushNotification
|
3
|
+
DEFAULT_MESSAGE = "IOS Push Notification"
|
4
|
+
|
5
|
+
def message
|
6
|
+
{
|
7
|
+
apns => {
|
8
|
+
aps: { alert: payload[:message] || DEFAULT_MESSAGE },
|
9
|
+
data: payload
|
10
|
+
}.to_json
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def apns
|
17
|
+
configuration.apply(:read_ios_apns, device).to_sym
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module PushToSNS
|
2
|
+
module Messages
|
3
|
+
def self.not_implemented_config(method_name, &message)
|
4
|
+
"Not Implemented Configuration `#{method_name.to_s}`: #{message.call}"
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.not_implemented_method(method_name, &message)
|
8
|
+
"Not Implemented Method `#{method_name.to_s}`: #{message.call}"
|
9
|
+
end
|
10
|
+
|
11
|
+
READ_DEVICE_ID_NOT_IMPLEMENTED = not_implemented_config(:read_device_id) do
|
12
|
+
"How to read the device's id from a device object?"
|
13
|
+
end
|
14
|
+
|
15
|
+
READ_SOURCE_NOT_IMPLEMENTED = not_implemented_config(:read_source) do
|
16
|
+
"How to read the device's source (ios or android) from a device object?"
|
17
|
+
end
|
18
|
+
|
19
|
+
READ_ENDPOINT_ARN_NOT_IMPLEMENTED = not_implemented_config(:read_endpoint_arn) do
|
20
|
+
"How to read the device's endpoint arn that was saved while registering?"
|
21
|
+
end
|
22
|
+
|
23
|
+
READ_PLATFORM_ARN_NOT_IMPLEMENTED = not_implemented_config(:read_platform_arn) do
|
24
|
+
"How to read platform ARN that should be configured in AWS?"
|
25
|
+
end
|
26
|
+
|
27
|
+
READ_IOS_APNS_NOT_IMPLEMENTED = not_implemented_config(:read_ios_apns) do
|
28
|
+
"How to read the IOS's wrapper object (APNS or APNS_SANDBOX)?"
|
29
|
+
end
|
30
|
+
|
31
|
+
SAVE_ENDPOINT_ARN_NOT_IMPLEMENTED = not_implemented_config(:save_endpoint_arn) do
|
32
|
+
"How to save the endpoint_arn in the device?"
|
33
|
+
end
|
34
|
+
|
35
|
+
DEVICES_METHOD_NOT_IMPLEMENTED = not_implemented_method(:devices) do
|
36
|
+
"What devices are going to be notified?"
|
37
|
+
end
|
38
|
+
|
39
|
+
NOTIFICATION_METHOD_NOT_IMPLEMENTED = not_implemented_method(:notification) do
|
40
|
+
"What payload should we send to this specific device?"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module PushToSNS
|
2
|
+
class PushNotifier
|
3
|
+
def initialize(params = {})
|
4
|
+
params.each do |attr, value|
|
5
|
+
public_send("#{attr}=", value)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def deliver
|
10
|
+
devices.each do |device|
|
11
|
+
SendPushNotification.new(device).perform(full_notification_for_device(device))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def devices
|
16
|
+
raise "Please implement the method :devices to be able to load devices to notify."
|
17
|
+
end
|
18
|
+
|
19
|
+
def notification(device)
|
20
|
+
raise "Please implement the method :notification with the message to send"
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def full_notification_for_device(device)
|
26
|
+
defaults = { type: type, message: message }
|
27
|
+
defaults.merge(notification(device))
|
28
|
+
end
|
29
|
+
|
30
|
+
def type
|
31
|
+
@type ||= self.class.type
|
32
|
+
end
|
33
|
+
|
34
|
+
def message
|
35
|
+
@message ||= self.class.message
|
36
|
+
end
|
37
|
+
|
38
|
+
class << self
|
39
|
+
attr_accessor :input_type, :input_message
|
40
|
+
|
41
|
+
def type(input_type = nil)
|
42
|
+
if input_type.nil?
|
43
|
+
self.input_type
|
44
|
+
else
|
45
|
+
self.input_type = input_type
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def message(input_message = nil)
|
50
|
+
if input_message.nil?
|
51
|
+
self.input_message
|
52
|
+
else
|
53
|
+
self.input_message = input_message
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module PushToSNS
|
2
|
+
class SendPushNotification
|
3
|
+
def initialize(device, configuration = PushToSNS.configuration)
|
4
|
+
self.device = device
|
5
|
+
self.configuration = configuration
|
6
|
+
end
|
7
|
+
|
8
|
+
def perform(payload)
|
9
|
+
ensure_endpoint_arn_is_enabled
|
10
|
+
notification = build_push_notification(payload)
|
11
|
+
|
12
|
+
response = AWS.sns.client.publish(
|
13
|
+
message_structure: "json",
|
14
|
+
message: notification.message.to_json,
|
15
|
+
target_arn: configuration.apply(:read_endpoint_arn, device)
|
16
|
+
)
|
17
|
+
|
18
|
+
response[:message_id]
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
attr_accessor :device, :configuration
|
24
|
+
|
25
|
+
def build_push_notification(payload)
|
26
|
+
case configuration.apply(:read_source, device)
|
27
|
+
when "ios"
|
28
|
+
IosPushNotification.new(device, payload, configuration)
|
29
|
+
when "android"
|
30
|
+
AndroidPushNotification.new(device, payload, configuration)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def ensure_endpoint_arn_is_enabled
|
35
|
+
attributes = get_endpoint_attributes
|
36
|
+
|
37
|
+
if attributes["Enabled"].downcase == "false" || attributes["Token"] != device_id
|
38
|
+
enable_endpoint_arn
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def get_endpoint_attributes
|
43
|
+
AWS.sns.client.get_endpoint_attributes(
|
44
|
+
endpoint_arn: endpoint_arn
|
45
|
+
)[:attributes]
|
46
|
+
end
|
47
|
+
|
48
|
+
def enable_endpoint_arn
|
49
|
+
AWS.sns.client.set_endpoint_attributes(
|
50
|
+
endpoint_arn: endpoint_arn,
|
51
|
+
attributes: {
|
52
|
+
"Enabled" => "True",
|
53
|
+
"Token" => device_id
|
54
|
+
}
|
55
|
+
)[:endpoint_arn]
|
56
|
+
end
|
57
|
+
|
58
|
+
def device_id
|
59
|
+
@device_id ||= configuration.apply(:read_device_id, device)
|
60
|
+
end
|
61
|
+
|
62
|
+
def endpoint_arn
|
63
|
+
@endpoint_arn ||= configuration.apply(:read_endpoint_arn, device)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module PushToSNS
|
2
|
+
class SetupPushNotification
|
3
|
+
def initialize(device, configuration = PushToSNS.configuration)
|
4
|
+
self.device = device
|
5
|
+
self.configuration = configuration
|
6
|
+
end
|
7
|
+
|
8
|
+
def perform
|
9
|
+
configuration.apply(:save_endpoint_arn, device, create_endpoint_arn)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
attr_accessor :device, :configuration
|
15
|
+
|
16
|
+
def create_endpoint_arn
|
17
|
+
AWS.sns.client.create_platform_endpoint({
|
18
|
+
platform_application_arn: configuration.apply(:read_platform_arn, device),
|
19
|
+
token: configuration.apply(:read_device_id, device)
|
20
|
+
})[:endpoint_arn]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module PushToSns # We need this name to help Rails generators lookup
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
desc "Create PushToSNS configuration initializer"
|
5
|
+
|
6
|
+
def self.source_root
|
7
|
+
@source_root ||= File.expand_path("../templates", __FILE__)
|
8
|
+
end
|
9
|
+
|
10
|
+
def create_config_initializer
|
11
|
+
copy_file "config/initializers/push_to_sns.rb"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module PushToSns # We need this name to help Rails generators lookup
|
2
|
+
module Generators
|
3
|
+
# rails generate push_to_sns:notifier new_goal goal
|
4
|
+
class NotifierGenerator < Rails::Generators::NamedBase
|
5
|
+
desc "Create a new PushToSNS notifier"
|
6
|
+
argument :attributes,
|
7
|
+
type: :array,
|
8
|
+
default: [],
|
9
|
+
banner: "attribute attribute ..."
|
10
|
+
|
11
|
+
def self.source_root
|
12
|
+
@source_root ||= File.expand_path("../templates", __FILE__)
|
13
|
+
end
|
14
|
+
|
15
|
+
def create_notifier
|
16
|
+
template "notifier.rb", File.join(
|
17
|
+
"app/push_notifiers", "#{file_name}_notifier.rb"
|
18
|
+
)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
PushToSNS.configure do
|
2
|
+
read_device_id { |device| fail PushToSNS::Messages::READ_DEVICE_ID_NOT_IMPLEMENTED }
|
3
|
+
read_source { |device| fail PushToSNS::Messages::READ_SOURCE_NOT_IMPLEMENTED }
|
4
|
+
read_endpoint_arn { |device| fail PushToSNS::Messages::READ_ENDPOINT_ARN_NOT_IMPLEMENTED }
|
5
|
+
read_platform_arn { |device| fail PushToSNS::Messages::READ_PLATFORM_ARN_NOT_IMPLEMENTED }
|
6
|
+
read_ios_apns { |device| fail PushToSNS::Messages::READ_IOS_APNS_NOT_IMPLEMENTED }
|
7
|
+
|
8
|
+
save_endpoint_arn do |device, endpoint_arn|
|
9
|
+
fail PushToSNS::Messages::SAVE_ENDPOINT_ARN_NOT_IMPLEMENTED
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class <%= class_name %>Notifier < PushToSNS::PushNotifier
|
2
|
+
message "<%= human_name %>"
|
3
|
+
type :<%= singular_name %>
|
4
|
+
|
5
|
+
attr_accessor <%= attributes.map { |attribute| ":#{attribute.name}" }.join(", ") %>
|
6
|
+
|
7
|
+
def devices
|
8
|
+
fail PushToSNS::Messages::DEVICES_METHOD_NOT_IMPLEMENTED
|
9
|
+
end
|
10
|
+
|
11
|
+
def notification(device)
|
12
|
+
fail PushToSNS::Messages::NOTIFICATION_METHOD_NOT_IMPLEMENTED
|
13
|
+
end
|
14
|
+
end
|
data/push_to_sns.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'push_to_sns/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "push_to_sns"
|
8
|
+
spec.version = PushToSNS::VERSION
|
9
|
+
spec.authors = ["juliogarciag"]
|
10
|
+
spec.email = ["julioggonz@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Organized SNS Push Notifications for Ruby.}
|
13
|
+
spec.description = %q{Organized SNS Push Notifications for Ruby.}
|
14
|
+
spec.homepage = "https://github.com/platanus/push-to-sns"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.3"
|
25
|
+
spec.add_development_dependency "guard", "~> 2.13"
|
26
|
+
spec.add_development_dependency "guard-rspec", "~> 4.6"
|
27
|
+
spec.add_development_dependency "rspec-nc", "~> 0.2"
|
28
|
+
spec.add_development_dependency "rspec-legacy_formatters", "~> 1.0"
|
29
|
+
spec.add_runtime_dependency "deep_merge","~> 1.0"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: push_to_sns
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- juliogarciag
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.13'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.13'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '4.6'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '4.6'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec-nc
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.2'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.2'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec-legacy_formatters
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: deep_merge
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '1.0'
|
125
|
+
description: Organized SNS Push Notifications for Ruby.
|
126
|
+
email:
|
127
|
+
- julioggonz@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".travis.yml"
|
135
|
+
- Gemfile
|
136
|
+
- Guardfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- bin/console
|
141
|
+
- bin/setup
|
142
|
+
- lib/push_to_sns.rb
|
143
|
+
- lib/push_to_sns/android_push_notification.rb
|
144
|
+
- lib/push_to_sns/basic_push_notification.rb
|
145
|
+
- lib/push_to_sns/configuration.rb
|
146
|
+
- lib/push_to_sns/ios_push_notification.rb
|
147
|
+
- lib/push_to_sns/messages.rb
|
148
|
+
- lib/push_to_sns/push_notifier.rb
|
149
|
+
- lib/push_to_sns/send_push_notification.rb
|
150
|
+
- lib/push_to_sns/setup_push_notification.rb
|
151
|
+
- lib/push_to_sns/version.rb
|
152
|
+
- lib/rails/generators/push_to_sns/install_generator.rb
|
153
|
+
- lib/rails/generators/push_to_sns/notifier_generator.rb
|
154
|
+
- lib/rails/generators/push_to_sns/templates/config/initializers/push_to_sns.rb
|
155
|
+
- lib/rails/generators/push_to_sns/templates/notifier.rb
|
156
|
+
- push_to_sns.gemspec
|
157
|
+
homepage: https://github.com/platanus/push-to-sns
|
158
|
+
licenses:
|
159
|
+
- MIT
|
160
|
+
metadata: {}
|
161
|
+
post_install_message:
|
162
|
+
rdoc_options: []
|
163
|
+
require_paths:
|
164
|
+
- lib
|
165
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
166
|
+
requirements:
|
167
|
+
- - ">="
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
version: '0'
|
170
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: '0'
|
175
|
+
requirements: []
|
176
|
+
rubyforge_project:
|
177
|
+
rubygems_version: 2.2.2
|
178
|
+
signing_key:
|
179
|
+
specification_version: 4
|
180
|
+
summary: Organized SNS Push Notifications for Ruby.
|
181
|
+
test_files: []
|