firebase_cloud_messenger 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +26 -8
- data/lib/firebase_cloud_messenger/auth_client.rb +21 -4
- data/lib/firebase_cloud_messenger/client.rb +6 -1
- data/lib/firebase_cloud_messenger/error.rb +2 -0
- data/lib/firebase_cloud_messenger/version.rb +1 -1
- data/lib/firebase_cloud_messenger.rb +22 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f35bac5160121441e8b66065922a0a54374fbaa4
|
4
|
+
data.tar.gz: 41711425b6d678ad592eda312944834a62ff86a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c563c3cbb78512e98c4f5a12233531da0318fba0a801c0faf4aa712f87bd8c133c9be520f46ba10d727f9d8bbe27df550f332c0637c3e26866d612fdaf341503
|
7
|
+
data.tar.gz: 8af53e10b25c15767a8cf2f35a1a27e2efdb76c085272bfdbd2eb7be4e0fa84e7b65fa160224e46784e7cf5df3bc3d599226aee33d021b679b7391d6bb3236b9
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# firebase_cloud_messenger
|
2
2
|
|
3
|
-
|
3
|
+
firebase_cloud_messenger wraps Google's API to make sending push notifications to iOS, android, and
|
4
4
|
web push notifications from your server easy.
|
5
5
|
|
6
6
|
NB: Google released the [FCM HTTP v1 API](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages)
|
@@ -24,19 +24,37 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
$ gem install firebase_cloud_messenger
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
## Setup
|
28
|
+
In order for google to authenticate requests to Firebase Cloud Messenger, you must either have your
|
29
|
+
service account credentials file in a place that's accessible, or provide credentials as env vars.
|
30
30
|
|
31
|
-
1
|
32
|
-
|
31
|
+
#### Setup Method 1: Service Account JSON Path Supplied As Env Var
|
32
|
+
```bash
|
33
|
+
$ export GOOGLE_APPLICATION_CREDENTIALS = "path/to/credentials/file.json"`
|
34
|
+
```
|
35
|
+
|
36
|
+
#### Setup Method 2: Service Account JSON Path Supplied To FirebaseCloudMessenger
|
37
|
+
```ruby
|
38
|
+
FirebaseCloudMessenger.credentials_path = "path/to/credentials/file.json"
|
39
|
+
```
|
40
|
+
|
41
|
+
#### Setup Method 3: Service Account Credentials Set as Env Vars
|
42
|
+
``` bash
|
43
|
+
$ export GOOGLE_PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----..."
|
44
|
+
$ export GOOGLE_CLIENT_EMAIL = "firebase-admin-sdk...@iam.gserviceaccount.com"
|
45
|
+
```
|
46
|
+
Also set the `project_id`, which is otherwise read from the json service account file:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
FirebaseCloudMessenger.project_id = "1234567"
|
50
|
+
```
|
33
51
|
|
34
52
|
## Usage
|
35
53
|
|
36
54
|
### Sending a Message
|
37
55
|
|
38
56
|
You can see how your message should be structured [here](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages)
|
39
|
-
|
57
|
+
firebase_cloud_messenger provides built-in data classes for each json object type in the FCM API
|
40
58
|
specification, but you can also build up a hash message on your own, or use some combination of the
|
41
59
|
two.
|
42
60
|
|
@@ -4,9 +4,12 @@ module FirebaseCloudMessenger
|
|
4
4
|
|
5
5
|
AUTH_SCOPE = "https://www.googleapis.com/auth/firebase.messaging".freeze
|
6
6
|
|
7
|
-
def initialize(credentials_path)
|
7
|
+
def initialize(credentials_path = nil)
|
8
8
|
@credentials_path = credentials_path
|
9
|
-
|
9
|
+
|
10
|
+
raise_credentials_not_supplied if !credentials_supplied?
|
11
|
+
|
12
|
+
@authorizer = Google::Auth::ServiceAccountCredentials.make_creds(cred_args)
|
10
13
|
end
|
11
14
|
|
12
15
|
def fetch_access_token_info
|
@@ -17,10 +20,24 @@ module FirebaseCloudMessenger
|
|
17
20
|
|
18
21
|
attr_reader :authorizer
|
19
22
|
|
20
|
-
def
|
23
|
+
def credentials_supplied?
|
24
|
+
credentials_path || (ENV['GOOGLE_CLIENT_EMAIL'] && ENV['GOOGLE_PRIVATE_KEY'])
|
25
|
+
end
|
26
|
+
|
27
|
+
def raise_credentials_not_supplied
|
28
|
+
msg = "Either a path to a service account credentials json must be supplied, "\
|
29
|
+
"or the `GOOGLE_CLIENT_EMAIL` and `GOOGLE_PRIVATE_KEY` env vars must be set."
|
30
|
+
|
31
|
+
raise ArgumentError, msg
|
32
|
+
end
|
33
|
+
|
34
|
+
def cred_args
|
35
|
+
args = { scope: AUTH_SCOPE }
|
36
|
+
return args unless credentials_path
|
37
|
+
|
21
38
|
begin
|
22
39
|
file = File.open(credentials_path)
|
23
|
-
|
40
|
+
args.merge(json_key_io: file)
|
24
41
|
ensure
|
25
42
|
file.close
|
26
43
|
end
|
@@ -5,8 +5,13 @@ module FirebaseCloudMessenger
|
|
5
5
|
attr_writer :max_retry_count, :project_id, :access_token
|
6
6
|
attr_accessor :credentials_path
|
7
7
|
|
8
|
-
def initialize(credentials_path = nil)
|
8
|
+
def initialize(credentials_path = nil, project_id = nil)
|
9
9
|
@credentials_path = credentials_path || ENV['GOOGLE_APPLICATION_CREDENTIALS']
|
10
|
+
@project_id = project_id
|
11
|
+
|
12
|
+
if !(@credentials_path || @project_id)
|
13
|
+
raise ArgumentError, "Either a project_id or a credentials_path must be supplied"
|
14
|
+
end
|
10
15
|
end
|
11
16
|
|
12
17
|
def send(message, validate_only, conn)
|
@@ -15,11 +15,13 @@ require 'firebase_cloud_messenger/webpush'
|
|
15
15
|
|
16
16
|
module FirebaseCloudMessenger
|
17
17
|
class << self
|
18
|
-
attr_accessor :credentials_path
|
18
|
+
attr_accessor :credentials_path, :project_id
|
19
19
|
end
|
20
20
|
|
21
21
|
def self.send(message: {}, validate_only: false, conn: nil)
|
22
|
-
|
22
|
+
check_setup_complete!
|
23
|
+
|
24
|
+
Client.new(credentials_path, project_id).send(message, validate_only, conn)
|
23
25
|
end
|
24
26
|
|
25
27
|
def self.validate_message(message, conn = nil, against_api: false)
|
@@ -27,4 +29,22 @@ module FirebaseCloudMessenger
|
|
27
29
|
|
28
30
|
message.valid?(conn, against_api: against_api)
|
29
31
|
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def self.check_setup_complete!
|
36
|
+
if !(credentials_path || project_id)
|
37
|
+
msg = <<-ERROR_MSG
|
38
|
+
Either a credentials_path or project_id must be supplied. Add one of them like this:
|
39
|
+
|
40
|
+
`FirebaseCloudMessenger.credentials_path = "path/to/credentials.json"`
|
41
|
+
|
42
|
+
or:
|
43
|
+
|
44
|
+
`FirebaseCloudMessenger.project_id = "12345678"`
|
45
|
+
ERROR_MSG
|
46
|
+
|
47
|
+
raise FirebaseCloudMessenger::SetupError, msg
|
48
|
+
end
|
49
|
+
end
|
30
50
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: firebase_cloud_messenger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vince DeVendra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-12-
|
11
|
+
date: 2017-12-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: googleauth
|
@@ -102,6 +102,7 @@ extensions: []
|
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
104
|
- ".gitignore"
|
105
|
+
- CHANGELOG.md
|
105
106
|
- Gemfile
|
106
107
|
- LICENSE.txt
|
107
108
|
- README.md
|