Class: PushRadar::Radar

Inherits:
Object
  • Object
show all
Defined in:
lib/PushRadar.rb

Overview

A realtime push notifications API service for the web, featuring advanced targeting

Instance Method Summary collapse

Constructor Details

#initialize(api_secret) ⇒ Radar

Creates a new instance of PushRadar with the specified API secret



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/PushRadar.rb', line 13

def initialize(api_secret)

  # Initialize data
  @data = {}

  # Reset the targeting options
  reset_targeting

  # Check that the API secret is a string
  unless api_secret.is_a?(String)
    raise 'The API secret must be a string value.'
  end

  # Trim the API secret
  api_secret.strip!

  # If we are running unit tests, set the unit test mode flag and skip the PushRadar API client initialisation
  if api_secret == 'test-secret'
    @is_unit_test_mode = true
    return
  else
    @is_unit_test_mode = false
  end

  # Check that the API secret starts with sk_
  unless api_secret.start_with?('sk_')
    raise 'The PushRadar API secret provided is invalid. You can view your credentials from the API section of your dashboard (https://www.pushradar.com/app/api).'
  end

  # Make a new instance of a PushRadar API client with the specified API secret
  @api_client = APIClient.new(api_secret)

end

Instance Method Details

#add_data_item(key, value) ⇒ Object

Adds a data item to the list of data items



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/PushRadar.rb', line 269

def add_data_item(key, value)

  # Make sure the key is not empty
  if key == ''
    raise 'The key provided cannot be empty.'
  end

  # Add the data item
  unless @data.keys.include?(key)
    @data[key] = value
  end

  # Allow method chaining
  self

end

#add_data_items(data = {}) ⇒ Object

Adds multiple data items to the list of data items



287
288
289
290
291
292
293
294
295
296
297
# File 'lib/PushRadar.rb', line 287

def add_data_items(data = {})

  # Add the data items
  data.keys.each {|x|
    add_data_item x, data[x]
  }

  # Allow method chaining
  self

end

#broadcast(channel, data = {}) ⇒ Object

Broadcasts data on the channel specified



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/PushRadar.rb', line 300

def broadcast(channel, data = {})

  # If we are running unit tests, throw an exception
  if @is_unit_test_mode
    raise 'Unit testing of the broadcast() method is not supported.'
  end

  # Trim the channel name
  channel.strip!

  # Check whether data has been provided
  if data.length == 0 && @data.length == 0
    raise 'There is no data to broadcast.'
  end

  # Check whether the channel name contains spaces
  if channel.include? ' '
    raise "The channel name cannot contain spaces. By convention, channel names are alphanumerical and lowercase, with optional dashes (e.g. 'test-channel')."
  end

  # Use the stored data if the data parameter is empty
  if data.length == 0
    data = @data
  else
    data.keys.each {|x|
      @data[x] = data[x]
    }
    data = @data
  end

  # Initialize the hash of data to send to the server
  data_to_send = {
      notification: {
          channel: channel,
          data: data,
          userIDs: @targeting.instance_variable_get('@target_user_ids'),
          actions: @targeting.instance_variable_get('@target_actions'),
          notActions: @targeting.instance_variable_get('@target_not_actions'),
          continents: @targeting.instance_variable_get('@target_continents'),
          countries: @targeting.instance_variable_get('@target_countries'),
          ipAddresses: @targeting.instance_variable_get('@target_ips'),
          browsers: @targeting.instance_variable_get('@target_browsers'),
      }.to_json
  }

  # Broadcast the notification
  @api_client.post('/broadcast', data_to_send)

  # Reset the targeting options
  reset_targeting

end

#channel_auth(channel) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/PushRadar.rb', line 353

def channel_auth(channel)

  # Trim the channel name
  channel.strip!

  unless (channel.start_with? "private-") || (channel.start_with? "presence-")
    raise "Channel authentication can only be used with private or presence channels."
  end

  # Generate the channel authentication token
  channel_auth_token = "channel_auth_token_" + Digest::MD5.hexdigest(channel) + "." + (0...8).map { (65 + rand(26)).chr }.join + ".0x" + (0...8).map { (65 + rand(26)).chr }.join

  # Broadcast the notification
  @api_client.post('/channel-auth', {
      authToken: channel_auth_token,
      channel: channel
  })

  channel_auth_token

end

#target_action(action) ⇒ Object

Targets the notification to clients who have taken the given action



197
198
199
200
201
202
203
204
205
# File 'lib/PushRadar.rb', line 197

def target_action(action)

  # Add the action to the list of target actions
  @targeting.target_action action

  # Allow method chaining
  self

end

#target_actions(*actions) ⇒ Object

Targets the notification to clients who have taken any of the given actions



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/PushRadar.rb', line 208

def target_actions(*actions)

  # Target each action
  actions.each {|x|
    target_action x
  }

  # Allow method chaining
  self

end

#target_africaObject

Targets the notification to clients currently located in Africa



107
108
109
110
111
112
113
114
115
# File 'lib/PushRadar.rb', line 107

def target_africa

  # Target the continent
  @targeting.target_continent('AF')

  # Allow method chaining
  self

end

#target_antarcticaObject

Targets the notification to clients currently located in Antarctica



118
119
120
121
122
123
124
125
126
# File 'lib/PushRadar.rb', line 118

def target_antarctica

  # Target the continent
  @targeting.target_continent('AN')

  # Allow method chaining
  self

end

#target_asiaObject

Targets the notification to clients currently located in Asia



96
97
98
99
100
101
102
103
104
# File 'lib/PushRadar.rb', line 96

def target_asia

  # Target the continent
  @targeting.target_continent('AS')

  # Allow method chaining
  self

end

#target_browser(browser) ⇒ Object

Targets the notification to clients currently using the given browser



48
49
50
51
52
53
54
55
56
# File 'lib/PushRadar.rb', line 48

def target_browser(browser)

  # Add the browser to the list of target browsers
  @targeting.target_browser browser

  # Allow method chaining
  self

end

#target_browsers(*browsers) ⇒ Object

Targets the notification to clients currently using any of the given browsers



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/PushRadar.rb', line 59

def target_browsers(*browsers)

  # Target each browser
  browsers.each {|x|
    target_browser x
  }

  # Allow method chaining
  self

end

#target_countries(*country_codes) ⇒ Object

Targets the notification to clients currently located in any of the given countries



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/PushRadar.rb', line 83

def target_countries(*country_codes)

  # Target each country
  country_codes.each {|x|
    target_country x
  }

  # Allow method chaining
  self

end

#target_country(country_code) ⇒ Object

Targets the notification to clients currently located in the given country



72
73
74
75
76
77
78
79
80
# File 'lib/PushRadar.rb', line 72

def target_country(country_code)

  # Add the country to the list of target countries
  @targeting.target_country country_code

  # Allow method chaining
  self

end

#target_europeObject

Targets the notification to clients currently located in Europe



129
130
131
132
133
134
135
136
137
# File 'lib/PushRadar.rb', line 129

def target_europe

  # Target the continent
  @targeting.target_continent('EU')

  # Allow method chaining
  self

end

#target_ip(ip_address) ⇒ Object

Targets the notification to clients with the given IP address



173
174
175
176
177
178
179
180
181
# File 'lib/PushRadar.rb', line 173

def target_ip(ip_address)

  # Add the IP address to the list of target IP addresses
  @targeting.target_ip ip_address

  # Allow method chaining
  self

end

#target_ips(*ip_addresses) ⇒ Object

Targets the notification to clients with any of the given IP addresses



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/PushRadar.rb', line 184

def target_ips(*ip_addresses)

  # Target each IP address
  ip_addresses.each {|x|
    target_ip x
  }

  # Allow method chaining
  self

end

#target_north_americaObject

Targets the notification to clients currently located in North America



140
141
142
143
144
145
146
147
148
# File 'lib/PushRadar.rb', line 140

def target_north_america

  # Target the continent
  @targeting.target_continent('NA')

  # Allow method chaining
  self

end

#target_not_action(action) ⇒ Object

Targets the notification to clients who have not taken the given action



221
222
223
224
225
226
227
228
229
# File 'lib/PushRadar.rb', line 221

def target_not_action(action)

  # Add the action to the list of target "not" actions
  @targeting.target_not_action action

  # Allow method chaining
  self

end

#target_not_actions(*actions) ⇒ Object

Targets the notification to clients who have not taken any of the given actions



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/PushRadar.rb', line 232

def target_not_actions(*actions)

  # Target each action
  actions.each {|x|
    target_not_action x
  }

  # Allow method chaining
  self

end

#target_oceaniaObject

Targets the notification to clients currently located in Oceania



162
163
164
165
166
167
168
169
170
# File 'lib/PushRadar.rb', line 162

def target_oceania

  # Target the continent
  @targeting.target_continent('OC')

  # Allow method chaining
  self

end

#target_south_americaObject

Targets the notification to clients currently located in South America



151
152
153
154
155
156
157
158
159
# File 'lib/PushRadar.rb', line 151

def target_south_america

  # Target the continent
  @targeting.target_continent('SA')

  # Allow method chaining
  self

end

#target_user(user_id) ⇒ Object

Targets the notification to a specific user (identified by their user ID)



245
246
247
248
249
250
251
252
253
# File 'lib/PushRadar.rb', line 245

def target_user(user_id)

  # Target the user
  @targeting.target_user user_id

  # Allow method chaining
  self

end

#target_users(*user_ids) ⇒ Object

Targets the notification to specific users (identified by their user IDs)



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/PushRadar.rb', line 256

def target_users(*user_ids)

  # Target the user IDs
  user_ids.each {|x|
    target_user x
  }

  # Allow method chaining
  self

end