Class: Togglehq::Notify::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/togglehq/notify/notification.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_keyObject

Returns the value of attribute category_key.



4
5
6
# File 'lib/togglehq/notify/notification.rb', line 4

def category_key
  @category_key
end

#messageObject

Returns the value of attribute message.



4
5
6
# File 'lib/togglehq/notify/notification.rb', line 4

def message
  @message
end

#preference_keyObject

Returns the value of attribute preference_key.



4
5
6
# File 'lib/togglehq/notify/notification.rb', line 4

def preference_key
  @preference_key
end

#soundObject

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.

Parameters:

  • users (array of Togglehq::Notify::User)

    the users to send the notification to

Raises:

  • (RuntimeError)

    raised if an error occurs sending the notification



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.message,
                                                      :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.

Parameters:

  • user (Togglehq::Notify::User)

    the user to send the notification to

Raises:

  • (RuntimeError)

    raised if an error occurs sending the notification



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.message,
                                                      :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_globalObject

Sends this notification as a global notification to all of this app’s users.

Raises:

  • (RuntimeError)

    raised if an error occurs sending the notification



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.message,
                                                      :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