Class: Snm::Fcm::Notification
- Inherits:
-
Object
- Object
- Snm::Fcm::Notification
- Defined in:
- lib/snm/fcm.rb
Class Method Summary collapse
- .configuration ⇒ Object
- .configure {|configuration| ... } ⇒ Object
- .deliver(data = {}) ⇒ Object
- .get_access_token ⇒ Object
- .setup ⇒ Object
Class Method Details
.configuration ⇒ Object
18 19 20 |
# File 'lib/snm/fcm.rb', line 18 def self.configuration @configuration ||= Configuration.new end |
.configure {|configuration| ... } ⇒ Object
22 23 24 |
# File 'lib/snm/fcm.rb', line 22 def self.configure(&block) yield(configuration) end |
.deliver(data = {}) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/snm/fcm.rb', line 26 def self.deliver data={} raise 'Data is missing' if data.nil? response = HTTP.headers("Content-Type"=> "application/json", "Authorization"=> "Bearer #{get_access_token}").post("https://fcm.googleapis.com/v1/projects/#{Notification.configuration.project_id}/messages:send", json: data) JSON.parse(response.body) end |
.get_access_token ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/snm/fcm.rb', line 33 def self.get_access_token access_token = Notification.configuration.snm_fcm_redis.get('access_token') if access_token.nil? scope = 'https://www.googleapis.com/auth/cloud-platform' = Google::Auth::ServiceAccountCredentials.make_creds( json_key_io: File.open(Notification.configuration.credentails_file_path), scope: scope ) access_token = .fetch_access_token!['access_token'] Notification.configuration.snm_fcm_redis.setex('access_token', 3599, access_token) end access_token end |
.setup ⇒ Object
47 48 49 50 51 52 |
# File 'lib/snm/fcm.rb', line 47 def self.setup file = File.read(Notification.configuration.credentails_file_path) data_hash = JSON.parse(file) Notification.configuration.project_id = data_hash["project_id"] Notification.configuration.snm_fcm_redis = Redis.new(url: Notification.configuration.redis_endpoint) end |