Class: PushRadar::Targeting

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

Overview

Specifies the target audience of a PushRadar notification

Instance Method Summary collapse

Constructor Details

#initializeTargeting

Creates a new notification targeting object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/PushRadar/Targeting.rb', line 11

def initialize

  # Initialize variables

  @target_user_ids = []
  @target_actions = []
  @target_not_actions = []
  @target_browsers = []
  @target_continents = []
  @target_countries = []
  @target_ips = []

end

Instance Method Details

#target_action(action) ⇒ Object

Targets the notification to clients who have taken the given action



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/PushRadar/Targeting.rb', line 111

def target_action(action)

  # Trim the action identifier

  action.strip!

  # Make sure the action identifier is not empty

  if action == ''
    raise 'The action identifier cannot be empty'
  end

  # Make sure that the action is not in the target "not" actions list

  if @target_not_actions.include? action
    raise "Action '" + action + "' is already in the 'not' actions list."
  end

  # Add the action to the list

  unless @target_actions.include? action
    @target_actions.push action
  end

end

#target_browser(browser) ⇒ Object

Adds a browser to the list of target browsers



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/PushRadar/Targeting.rb', line 25

def target_browser(browser)

  # Trim the browser and convert it to lowercase

  browser.strip!
  browser = browser.downcase

  # Validate the browser for being one of the supported types

  unless %w(chrome ie edge safari opera firefox).include? browser
    raise "The browser must be one of the following: 'chrome', 'ie', 'edge', 'safari', 'opera', 'firefox'."
  end

  # Add the browser to the list of target browser

  unless @target_browsers.include? browser
    @target_browsers.push browser
  end

end

#target_continent(continent_code) ⇒ Object

Targets a continent by its continent code



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/PushRadar/Targeting.rb', line 68

def target_continent(continent_code)

  # Target the countries located in this continent

  Utils.get_countries_from_continent(continent_code).each {|country|
    target_country country
  }

  # Add the continent code to the list

  unless @target_continents.include? continent_code
    @target_continents.push continent_code
  end

end

#target_country(country_code) ⇒ Object

Adds a country to the list of target countries



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/PushRadar/Targeting.rb', line 44

def target_country(country_code)

  # Trim the country code and convert it to uppercase

  country_code.strip!
  country_code = country_code.upcase

  # Ensure that the country code is not empty

  if country_code == ''
    raise 'The country code provided cannot be empty.'
  end

  # Validate the country code

  unless Utils.get_countries.keys.include? country_code.to_sym
    raise 'The country code provided must be a valid two-letter (ISO 3166-1 alpha-2) code.'
  end

  # Add the country to the list

  unless @target_countries.include? country_code
    @target_countries.push country_code
  end

end

#target_ip(ip_address) ⇒ Object

Targets the notification to clients with the given IP address



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/PushRadar/Targeting.rb', line 83

def target_ip(ip_address)

  # Trim the IP address

  ip_address.strip!

  # Make sure that the IP address is not empty

  if ip_address == ''
    raise 'The IP address provided cannot be empty.'
  end

  # Check for wildcard IPs

  if ip_address.include? '*'
    raise 'Wildcard IP address targeting is not supported.'
  end

  # Validate the IP address

  unless Utils.is_valid_ip ip_address
    raise 'The IP address provided must be a valid IPv4 or IPv6 address.'
  end

  # Add the IP address to the list

  unless @target_ips.include? ip_address
    @target_ips.push ip_address
  end

end

#target_not_action(action) ⇒ Object

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/PushRadar/Targeting.rb', line 134

def target_not_action(action)

  # Trim the action identifier

  action.strip!

  # Make sure the action identifier is not empty

  if action == ''
    raise 'The action identifier cannot be empty'
  end

  # Make sure that the action is not in the target actions list

  if @target_actions.include? action
    raise "Action '" + action + "' is already in the actions list."
  end

  # Add the action to the list

  unless @target_not_actions.include? action
    @target_not_actions.push action
  end

end

#target_user(user_id) ⇒ Object

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



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/PushRadar/Targeting.rb', line 157

def target_user(user_id)

  # Make sure that the user ID is not empty

  if user_id == nil
    raise 'The user ID cannot be nil.'
  end

  # Add the user ID to the list

  unless @target_user_ids.include? user_id
    @target_user_ids.push user_id
  end

end