Class: Togglehq::Notify::Notification
- Inherits:
-
Object
- Object
- Togglehq::Notify::Notification
- Defined in:
- lib/togglehq/notify/notification.rb
Instance Attribute Summary collapse
-
#category_key ⇒ Object
Returns the value of attribute category_key.
-
#message ⇒ Object
Returns the value of attribute message.
-
#preference_key ⇒ Object
Returns the value of attribute preference_key.
-
#sound ⇒ Object
Returns the value of attribute sound.
Instance Method Summary collapse
-
#batch_send(users) ⇒ Object
Sends this notification to the given set of users.
-
#initialize(params = {}) ⇒ Notification
constructor
A new instance of Notification.
-
#send(user) ⇒ Object
Sends this notification to the given user.
-
#send_global ⇒ Object
Sends this notification as a global notification to all of this app’s users.
Constructor Details
#initialize(params = {}) ⇒ Notification
Returns a new instance of Notification.
6 7 8 9 10 11 |
# File 'lib/togglehq/notify/notification.rb', line 6 def initialize(params = {}) @category_key = params[:category_key] @preference_key = params[:preference_key] @message = params[:message] @sound = params[:sound] end |
Instance Attribute Details
#category_key ⇒ Object
Returns the value of attribute category_key.
4 5 6 |
# File 'lib/togglehq/notify/notification.rb', line 4 def category_key @category_key end |
#message ⇒ Object
Returns the value of attribute message.
4 5 6 |
# File 'lib/togglehq/notify/notification.rb', line 4 def @message end |
#preference_key ⇒ Object
Returns the value of attribute preference_key.
4 5 6 |
# File 'lib/togglehq/notify/notification.rb', line 4 def preference_key @preference_key end |
#sound ⇒ Object
Returns the value of attribute sound.
4 5 6 |
# File 'lib/togglehq/notify/notification.rb', line 4 def sound @sound end |
Instance Method Details
#batch_send(users) ⇒ Object
Sends this notification to the given set of users. You may only send to up to 100 users at a time.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/togglehq/notify/notification.rb', line 38 def batch_send(users) response = Togglehq::Request.new("/notifications", {:notification => {:category => self.category_key, :preference => self.preference_key, :message => self., :sound => self.sound, :users => users.map {|u| u.identifier}}}).post! if response.status == 403 raise "Access denied. You must use your Master OAuth client_id and client_secret to send push notifications." elsif response.status == 404 || response.status == 422 json = JSON.parse(response.body) raise json["message"] elsif response.status == 200 return true else raise "Unexpected error sending batch notification" end end |
#send(user) ⇒ Object
Sends this notification to the given user.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/togglehq/notify/notification.rb', line 16 def send(user) response = Togglehq::Request.new("/notifications", {:notification => {:category => self.category_key, :preference => self.preference_key, :message => self., :sound => self.sound, :user => user.identifier}}).post! if response.status == 403 raise "Access denied. You must use your Master OAuth client_id and client_secret to send push notifications." elsif response.status == 404 || response.status == 422 json = JSON.parse(response.body) raise json["message"] elsif response.status == 200 return true else raise "Unexpected error sending notification" end end |
#send_global ⇒ Object
Sends this notification as a global notification to all of this app’s users.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/togglehq/notify/notification.rb', line 59 def send_global response = Togglehq::Request.new("/notifications", {:notification => {:category => self.category_key, :preference => self.preference_key, :message => self., :sound => self.sound, :global => true}}).post! if response.status == 403 raise "Access denied. You must use your Master OAuth client_id and client_secret to send push notifications." elsif response.status == 404 || response.status == 422 json = JSON.parse(response.body) raise json["message"] elsif response.status == 200 return true else raise "Unexpected error sending global notification" end end |