Class: Capcoauth::Notifications
- Inherits:
-
Object
- Object
- Capcoauth::Notifications
- Includes:
- HTTParty
- Defined in:
- lib/capcoauth/notifications.rb
Class Method Summary collapse
- .add_device_token(user_id, device_token, device_type, environment = 'production') ⇒ Object
- .bearer_token ⇒ Object
- .default_headers ⇒ Object
- .notify(user_id, alert = nil, badge = nil, data = {}) ⇒ Object
- .remove_device_token(device_token) ⇒ Object
- .store ⇒ Object
Class Method Details
.add_device_token(user_id, device_token, device_type, environment = 'production') ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/capcoauth/notifications.rb', line 41 def add_device_token(user_id, device_token, device_type, environment = 'production') body = { data: { type: 'user_devices', attributes: { device_token: device_token, device_type: device_type, }, relationships: { user: { data: { type: 'users', id: user_id } } } } } body[:data][:attributes][:environment] = environment if device_type == 'ios' res = self.post( "#{Capcoauth.configuration.capcoauth_url}/api/v1/user_devices", { body: body.to_json, headers: default_headers } ) store.delete('application_token') if res.code == 401 return true if res.created? return true if res.body.include? 'has already been registered' false end |
.bearer_token ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/capcoauth/notifications.rb', line 13 def bearer_token return store.fetch('application_token') if store.fetch('application_token').present? res = self.post( "#{Capcoauth.configuration.capcoauth_url}/oauth/token", { body: { grant_type: 'client_credentials', }, basic_auth: { username: Capcoauth.configuration.client_id, password: Capcoauth.configuration.client_secret } } ) if res.ok? and res.parsed_response['access_token'] store.write('application_token', res.parsed_response['access_token'], expires_in: res.parsed_response['expires_in']) end store.fetch('application_token') end |
.default_headers ⇒ Object
34 35 36 37 38 39 |
# File 'lib/capcoauth/notifications.rb', line 34 def default_headers { 'Authorization': "Bearer #{bearer_token}", 'Content-Type': 'application/vnd.api+json' } end |
.notify(user_id, alert = nil, badge = nil, data = {}) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/capcoauth/notifications.rb', line 79 def notify(user_id, alert=nil, badge=nil, data={}) data = JSON.generate data res = self.post( "#{Capcoauth.configuration.capcoauth_url}/api/v1/user_notifications", { body: { data: { type: 'user_notifications', attributes: { alert: alert, badge: badge, data: data }, relationships: { user: { data: { type: 'users', id: user_id } } } } }.to_json, headers: default_headers } ) store.delete('application_token') if res.code == 401 return true if res.created? false end |
.remove_device_token(device_token) ⇒ Object
73 74 75 76 77 |
# File 'lib/capcoauth/notifications.rb', line 73 def remove_device_token(device_token) res = self.delete("#{Capcoauth.configuration.capcoauth_url}/api/v1/user_devices/#{device_token}", headers: default_headers) store.delete('application_token') if res.code == 401 res.code == 204 end |
.store ⇒ Object
9 10 11 |
# File 'lib/capcoauth/notifications.rb', line 9 def store Capcoauth.configuration.cache_store end |