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



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
46
47
48
# File 'lib/PushRadar.rb', line 16

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 'API secret invalid. You can view your PushRadar API secret at https://dashboard.pushradar.com/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



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/PushRadar.rb', line 300

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



318
319
320
321
322
323
324
325
326
327
328
# File 'lib/PushRadar.rb', line 318

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



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/PushRadar.rb', line 335

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
  end

  # Initialize the hash of data to send to the server
  data_to_send = {
      channel: channel,
      notification: data.to_json,
      target_user_ids: @targeting.instance_variable_get('@target_user_ids').to_json,
      target_actions: @targeting.instance_variable_get('@target_actions').to_json,
      target_not_actions: @targeting.instance_variable_get('@target_not_actions').to_json,
      target_continents: @targeting.instance_variable_get('@target_continents').to_json,
      target_countries: @targeting.instance_variable_get('@target_countries').to_json,
      target_ips: @targeting.instance_variable_get('@target_ips').to_json,
      target_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

#target_action(action_identifier) ⇒ Object

Targets the notification to clients who have taken the given action



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

def target_action(action_identifier)

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

  # Allow method chaining
  self

end

#target_actions(*action_identifiers) ⇒ Object

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



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

def target_actions(*action_identifiers)

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

  # Allow method chaining
  self

end

#target_africaObject

Targets the notification to clients currently located in Africa



122
123
124
125
126
127
128
129
130
# File 'lib/PushRadar.rb', line 122

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



133
134
135
136
137
138
139
140
141
# File 'lib/PushRadar.rb', line 133

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



111
112
113
114
115
116
117
118
119
# File 'lib/PushRadar.rb', line 111

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



55
56
57
58
59
60
61
62
63
# File 'lib/PushRadar.rb', line 55

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



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/PushRadar.rb', line 66

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



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

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



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

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



144
145
146
147
148
149
150
151
152
# File 'lib/PushRadar.rb', line 144

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



192
193
194
195
196
197
198
199
200
# File 'lib/PushRadar.rb', line 192

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



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/PushRadar.rb', line 203

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



155
156
157
158
159
160
161
162
163
# File 'lib/PushRadar.rb', line 155

def target_north_america

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

  # Allow method chaining
  self

end

#target_not_action(action_identifier) ⇒ Object

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



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

def target_not_action(action_identifier)

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

  # Allow method chaining
  self

end

#target_not_actions(*action_identifiers) ⇒ Object

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



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

def target_not_actions(*action_identifiers)

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

  # Allow method chaining
  self

end

#target_oceaniaObject

Targets the notification to clients currently located in Oceania



177
178
179
180
181
182
183
184
185
# File 'lib/PushRadar.rb', line 177

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



166
167
168
169
170
171
172
173
174
# File 'lib/PushRadar.rb', line 166

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)



272
273
274
275
276
277
278
279
280
# File 'lib/PushRadar.rb', line 272

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)



283
284
285
286
287
288
289
290
291
292
293
# File 'lib/PushRadar.rb', line 283

def target_users(*user_ids)

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

  # Allow method chaining
  self

end