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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: df760f55ce074ae2ec5f8e45d43ba2f0446e94f2
4
- data.tar.gz: f7aef2b712e689775f41d30d55897d27dc9ce3a6
3
+ metadata.gz: f35bac5160121441e8b66065922a0a54374fbaa4
4
+ data.tar.gz: 41711425b6d678ad592eda312944834a62ff86a3
5
5
  SHA512:
6
- metadata.gz: 71ae814f893ee647dc1149219787ba06481e536b35134e94f24ad82f86100e2833da1d5c789c1e70c31bf7080795ed0cb0c336c46ab2e75fedba7635c1ddcfd1
7
- data.tar.gz: b12035cc991df505d0a7196a01e37a18a0574e88a2bd9a823910f54b3937861aefee4830dd9cb1bc279127391f2b2e5020835a666d3627f20a19442edce81c0f
6
+ metadata.gz: c563c3cbb78512e98c4f5a12233531da0318fba0a801c0faf4aa712f87bd8c133c9be520f46ba10d727f9d8bbe27df550f332c0637c3e26866d612fdaf341503
7
+ data.tar.gz: 8af53e10b25c15767a8cf2f35a1a27e2efdb76c085272bfdbd2eb7be4e0fa84e7b65fa160224e46784e7cf5df3bc3d599226aee33d021b679b7391d6bb3236b9
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## Version 0.2.0 - December 12, 2017
2
+
3
+ * Allows Google service account credentials to be set either via environment variables or a path to
4
+ a json file. Previously only the json file option was available.
5
+
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # firebase-cloud-messenger
1
+ # firebase_cloud_messenger
2
2
 
3
- firebase-cloud-messenger wraps Google's API to make sending push notifications to iOS, android, and
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
- In order for google to authenticate requests to Firebase Cloud Messenger, you must have your
28
- credentials file in a place that's accessible. You can tell firebase-cloud-messenger where it is in
29
- two ways:
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. `FirebaseCloudMessenger.credentials_path = "path/to/credentials/file.json"`
32
- 2. `export GOOGLE_APPLICATION_CREDENTIALS = "path/to/credentials/file.json"`
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
- firebase-cloud-messenger provides built-in data classes for each json object type in the FCM API
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
- @authorizer = get_authorizer
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 get_authorizer
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
- Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: file, scope: AUTH_SCOPE)
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)
@@ -1,4 +1,6 @@
1
1
  module FirebaseCloudMessenger
2
+ class SetupError < StandardError; end
3
+
2
4
  class Error < StandardError
3
5
  attr_reader :response
4
6
 
@@ -1,3 +1,3 @@
1
1
  module FirebaseCloudMessenger
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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
- Client.new(credentials_path).send(message, validate_only, conn)
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.1.1
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-07 00:00:00.000000000 Z
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