Class: PushRadar::Targeting
- Inherits:
-
Object
- Object
- PushRadar::Targeting
- Defined in:
- lib/PushRadar/Targeting.rb
Overview
Specifies the target audience of a PushRadar notification
Instance Method Summary collapse
-
#initialize ⇒ Targeting
constructor
Creates a new notification targeting object.
-
#target_action(action_identifier) ⇒ Object
Targets the notification to clients who have taken the given action.
-
#target_browser(browser) ⇒ Object
Adds a browser to the list of target browsers.
-
#target_continent(continent_code) ⇒ Object
Targets a continent by its continent code.
-
#target_country(country_code) ⇒ Object
Adds a country to the list of target countries.
-
#target_ip(ip_address) ⇒ Object
Targets the notification to clients with the given IP address.
-
#target_not_action(action_identifier) ⇒ Object
Targets the notification to clients who have not taken the given action.
-
#target_user(user_id) ⇒ Object
Targets the notification to a specific user (identifier by their user ID).
Constructor Details
#initialize ⇒ Targeting
Creates a new notification targeting object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/PushRadar/Targeting.rb', line 15 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_identifier) ⇒ Object
Targets the notification to clients who have taken the given action
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/PushRadar/Targeting.rb', line 135 def target_action(action_identifier) # Trim the action identifier action_identifier.strip! # Make sure the action identifier is not empty if action_identifier == '' 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_identifier raise "Action '" + action_identifier + "' is already in the 'not' actions list." end # Add the action to the list unless @target_actions.include? action_identifier @target_actions.push action_identifier end end |
#target_browser(browser) ⇒ Object
Adds a browser to the list of target browsers
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/PushRadar/Targeting.rb', line 33 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
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/PushRadar/Targeting.rb', line 84 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
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/PushRadar/Targeting.rb', line 56 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
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/PushRadar/Targeting.rb', line 103 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_identifier) ⇒ Object
Targets the notification to clients who have not taken the given action
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/PushRadar/Targeting.rb', line 158 def target_not_action(action_identifier) # Trim the action identifier action_identifier.strip! # Make sure the action identifier is not empty if action_identifier == '' 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_identifier raise "Action '" + action_identifier + "' is already in the actions list." end # Add the action to the list unless @target_not_actions.include? action_identifier @target_not_actions.push action_identifier end end |
#target_user(user_id) ⇒ Object
Targets the notification to a specific user (identifier by their user ID)
185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/PushRadar/Targeting.rb', line 185 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 |