pushpad 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +21 -0
- data/README.md +81 -0
- data/lib/pushpad.rb +103 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 90bdda996e91aeb9895cec9561e37ef295f70794
|
4
|
+
data.tar.gz: ab5ce0db6b9a5cf5c6014ce1f9a7a72ca888b881
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c9603df2c24e32a48b4e3d393847edf8125cdf4d96b005bf26444e75082fb355bcb35efb6ed3a2f96e49645ad94a51f8ce7263149f2fdb88f4c1da82944fdc80
|
7
|
+
data.tar.gz: b06c575e6e1b36f2ea5e9ec78c3f4d030f13512b01084cb3a625a13403d51b6802db6e72e82b21c36f0cbd73050f40cf68878a79836de6f2df585f68cb4f8a4f
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Pushpad (https://pushpad.xyz)
|
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,81 @@
|
|
1
|
+
# Pushpad: web push notifications
|
2
|
+
|
3
|
+
Add web push notifications to your web app using third-party service [Pushpad](https://pushpad.xyz).
|
4
|
+
|
5
|
+
Currently push notifications work on the following browsers:
|
6
|
+
|
7
|
+
- Chrome (Desktop and Android)
|
8
|
+
- Firefox (44+)
|
9
|
+
- Safari
|
10
|
+
|
11
|
+
Features:
|
12
|
+
|
13
|
+
- users don't need to install any app or plugin
|
14
|
+
- notifications are delivered even when the user is not on your website
|
15
|
+
- you can target specific users or send bulk notifications
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'pushpad'
|
23
|
+
```
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle install
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install pushpad
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
First you need to sign up to Pushpad and create a project there. It takes 1 minute.
|
36
|
+
|
37
|
+
Then set your authentication credentials:
|
38
|
+
|
39
|
+
```
|
40
|
+
Pushpad.auth_token = '5374d7dfeffa2eb49965624ba7596a09'
|
41
|
+
Pushpad.project_id = 123 # set it here or pass it as a param to methods later
|
42
|
+
```
|
43
|
+
|
44
|
+
`auth_token` can be found in the user account settings.
|
45
|
+
`project_id` can be found in the project settings on Pushpad. A project is a list of subscriptions. You can set it globally or pass it as a param to methods if your app uses multiple lists (e.g. `Pushpad.path_for current_user, project_id: 123`, `notification.deliver_to user, project_id: 123`).
|
46
|
+
|
47
|
+
Let users subscribe to your push notifications:
|
48
|
+
|
49
|
+
```erb
|
50
|
+
<a href="<%= Pushpad.path %>">Subscribe anonymous to push notifications</a>
|
51
|
+
|
52
|
+
<a href="<%= Pushpad.path_for current_user # or current_user_id %>">Subscribe current user to push notifications</a>
|
53
|
+
```
|
54
|
+
|
55
|
+
`current_user` is the user currently logged in on your website.
|
56
|
+
|
57
|
+
When a user clicks the link is sent to Pushpad, automatically asked to receive push notifications and redirected back to your website.
|
58
|
+
|
59
|
+
Then you can send notifications:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
notification = Pushpad::Notification.new({
|
63
|
+
body: "Hello world!",
|
64
|
+
title: "Website Name", # optional, defaults to your project name
|
65
|
+
target_url: "http://example.com" # optional, defaults to your project website
|
66
|
+
})
|
67
|
+
|
68
|
+
notification.deliver_to user # or user_id
|
69
|
+
notification.deliver_to users # or user_ids
|
70
|
+
notification.broadcast # deliver to everyone
|
71
|
+
# => {"scheduled": 12}
|
72
|
+
```
|
73
|
+
|
74
|
+
If no user with that id has subscribed to push notifications, that id is simply ignored.
|
75
|
+
|
76
|
+
The methods above return an hash: `res["scheduled"]` contains the number of notifications that will be sent. For example if you call `notification.deliver_to user` but the user has never subscribed to push notifications the result will be `{"scheduled": 0}`.
|
77
|
+
|
78
|
+
## License
|
79
|
+
|
80
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
81
|
+
|
data/lib/pushpad.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'OpenSSL'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Pushpad
|
6
|
+
@@auth_token = nil
|
7
|
+
@@project_id = nil
|
8
|
+
|
9
|
+
def self.auth_token
|
10
|
+
@@auth_token
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.auth_token=(auth_token)
|
14
|
+
@@auth_token = auth_token
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.project_id
|
18
|
+
@@project_id
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.project_id=(project_id)
|
22
|
+
@@project_id = project_id
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.path(options = {})
|
26
|
+
project_id = options[:project_id] || self.project_id
|
27
|
+
raise "You must set project_id" unless project_id
|
28
|
+
"https://pushpad.xyz/projects/#{self.project_id}/subscription/edit"
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.path_for(user, options = {})
|
32
|
+
raise "You must set Pushpad.auth_token" unless Pushpad.auth_token
|
33
|
+
uid = user.respond_to?(:id) ? user.id : user
|
34
|
+
uid_signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), self.auth_token, uid.to_s)
|
35
|
+
"#{self.path(options)}?uid=#{uid}&uid_signature=#{uid_signature}"
|
36
|
+
end
|
37
|
+
|
38
|
+
class Notification
|
39
|
+
class DeliveryError < RuntimeError
|
40
|
+
end
|
41
|
+
|
42
|
+
attr_accessor :body, :title, :target_url
|
43
|
+
|
44
|
+
def initialize(options)
|
45
|
+
self.body = options[:body]
|
46
|
+
self.title = options[:title]
|
47
|
+
self.target_url = options[:target_url]
|
48
|
+
end
|
49
|
+
|
50
|
+
def broadcast(options = {})
|
51
|
+
deliver req_body, options
|
52
|
+
end
|
53
|
+
|
54
|
+
def deliver_to(users, options = {})
|
55
|
+
uids = if users.respond_to?(:ids)
|
56
|
+
users.ids
|
57
|
+
elsif users.respond_to?(:collect)
|
58
|
+
users.collect {|u| u.respond_to?(:id) ? u.id : u }
|
59
|
+
else
|
60
|
+
[users.respond_to?(:id) ? users.id : users]
|
61
|
+
end
|
62
|
+
deliver req_body(uids), options
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def deliver(req_body, options = {})
|
68
|
+
project_id = options[:project_id] || Pushpad.project_id
|
69
|
+
raise "You must set project_id" unless project_id
|
70
|
+
endpoint = "https://pushpad.xyz/projects/#{project_id}/notifications"
|
71
|
+
uri = URI.parse(endpoint)
|
72
|
+
https = Net::HTTP.new(uri.host, uri.port)
|
73
|
+
https.use_ssl = true
|
74
|
+
req = Net::HTTP::Post.new(uri.path, req_headers)
|
75
|
+
req.body = req_body
|
76
|
+
res = https.request(req)
|
77
|
+
raise DeliveryError, "Response #{res.code} #{res.message}: #{res.body}" unless res.code == '201'
|
78
|
+
JSON.parse(res.body)
|
79
|
+
end
|
80
|
+
|
81
|
+
def req_headers
|
82
|
+
raise "You must set Pushpad.auth_token" unless Pushpad.auth_token
|
83
|
+
{
|
84
|
+
'Authorization' => 'Token token="' + Pushpad.auth_token + '"',
|
85
|
+
'Content-Type' => 'application/json;charset=UTF-8',
|
86
|
+
'Accept' => 'application/json'
|
87
|
+
}
|
88
|
+
end
|
89
|
+
|
90
|
+
def req_body(uids = nil)
|
91
|
+
body = {
|
92
|
+
"notification" => {
|
93
|
+
"body" => self.body,
|
94
|
+
"title" => self.title,
|
95
|
+
"target_url" => self.target_url
|
96
|
+
}
|
97
|
+
}
|
98
|
+
body["uids"] = uids if uids
|
99
|
+
body.to_json
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pushpad
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pushpad
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-22 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- support@pushpad.xyz
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- Gemfile
|
21
|
+
- LICENSE.txt
|
22
|
+
- README.md
|
23
|
+
- lib/pushpad.rb
|
24
|
+
homepage: https://pushpad.xyz
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.0.14
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Web push notifications for Chrome, Firefox and Safari using Pushpad.
|
48
|
+
test_files: []
|