PushRadar and Ruby

The latest version of PushRadar's API is v1, released August 21, 2017.

Welcome to PushRadar's API documentation for Ruby. This article covers calling the various methods of the PushRadar object to publish notifications to a channel in realtime.

(Ruby)Prerequisites

You should do either of the following to install PushRadar's Ruby gem:

  • (a)Add gem 'pushradar' to your application's Gemfile and then execute $ bundle or ...
  • (b)Install PushRadar's Ruby gem yourself by running the command $ gem install pushradar
(server)"Hello World!" example

The following example demonstrates broadcasting a "Hello World!" message on a channel called test-channel. Replace your-secret-key with your actual PushRadar secret, which may be obtained from the API page of your dashboard.

  • Ruby
require 'PushRadar'
include PushRadar

radar = Radar.new('your-secret-key')
radar.broadcast('test-channel', {message: 'Hello World!'})
(client)Receiving notifications

To find out how to receive notifications by subscribing to channels, check out the documentation for our JavaScript client library.

(tip)Adding multiple data items

For convenience, PushRadar's Ruby library supports a fluent syntax to structure broadcasts. To add multiple data items, you can do either of the following:

  • Ruby
# ... radar = Radar.new(...)
radar.add_data_items(
    {message: 'Hello World!', item_2: "It's me!"}
).broadcast('test-channel')

# --- or ---

radar.broadcast('test-channel', {message: 'Hello World!', item_2: "It's me!"})
(feature)Action targeting

The following example demonstrates sending a notification to users who have taken a specific action, in this case, users who have purchased item #11.

  • Ruby
require 'PushRadar'
include PushRadar

radar = Radar.new('your-secret-key')
radar.target_action('purchased-item-#11').broadcast(
    'test-channel', {message: 'You have purchased item 11.'})

If you include more than one action by calling target_action() again or target_actions(), PushRadar will send the notification to users who have taken any of the given actions.

With the PushRadar JavaScript client library, you can track actions that users have taken using the action method:

  • JavaScript
// ... var radar = new PushRadar(...)
radar.action("purchased-item-#11");
Reverse action targeting

To send a notification to those who have not taken a certain action, you can use the target_not_action and target_not_actions methods. If you specify more than one action identifier, PushRadar will only send the notification to clients who have not taken any of the given actions.

  • Ruby
require 'PushRadar'
include PushRadar

radar = Radar.new('your-secret-key')
radar.target_not_action('purchased-item-#12').broadcast(
    'test-channel', {message: 'You have not purchased item 12.'})
(feature)User targeting

User targeting allows you to broadcast notifications to specific users, identified by their user ID. For this example, we will send a new message to the user with ID usr100 in our system by calling target_user(). Similarly, to target multiple users, you can call target_users().

  • Ruby
require 'PushRadar'
include PushRadar

radar = Radar.new('your-secret-key')
radar.target_user('usr100').add_data_items({
     message: 'Hey there, from Sam!',
     sender_name: 'Sam'
}).broadcast('user:new-message')

Make sure to set user IDs on each website page load using the PushRadar JavaScript client library:

  • JavaScript
// Set user ID on initialization
var radar = new PushRadar("your-api-key", "usr100");

// ... or ... radar.setUserID("usr100");

radar.subscribe("user:new-message", function(notification) {
    alert("New message from " + notification.sender_name); // "New message from Sam"
});
(feature)Browser targeting

You can target notifications to users who are currently using specific web browsers by calling target_browser() or target_browsers(). In the following example, we broadcast a notification to users currently using Firefox, Chrome or Internet Explorer:

Supported browsers are:   "chrome", "firefox", "ie", "edge", "safari" and "opera"

  • Ruby
require 'PushRadar'
include PushRadar

radar = Radar.new('your-secret-key')
radar.target_browsers('firefox', 'chrome', 'ie').broadcast('test-channel', {
    data: 'This notification is browser-targeted.'})
(feature)Geo-targeting (ISO 3166-1 alpha-2 )

You can easily broadcast notifications to clients located in certain countries by calling target_country() or target_countries(), passing in two-letter ISO country codes. In this example, we broadcast a notification to clients who are currently located in the UK or France:

  • Ruby
require 'PushRadar'
include PushRadar

radar = Radar.new('your-secret-key')
radar.target_countries('GB', 'FR').broadcast('test-channel',{
    data: 'You are in the UK or France!'})

Continent targeting is also supported, with specific target... methods you can call. Below, we add to the previous example and broadcast to all users located in North America, the UK or France:

  • Ruby
require 'PushRadar'
include PushRadar

radar = Radar.new('your-secret-key')
radar.target_north_america.target_countries('GB', 'FR')
    .broadcast('test-channel', {data: 'You are in North America, the UK or France!'})

Each continent has its own dedicated "target" method:

   - target_europe()
   - target_north_america()
   - target_south_america()
   - target_oceania()
   - target_asia()
   - target_africa()
   - target_antarctica()

(feature)IP address targeting

PushRadar supports IP address targeting. When using IP targeting, only clients with the given IP address(es) specified will receive your notification. Below is an example targeting a client with IP address 216.3.128.12. The target_ip and target_ips methods accept both IPv4 and IPv6 addresses.

  • Ruby
require 'PushRadar'
include PushRadar

radar = Radar.new('your-secret-key')
radar.target_ip('216.3.128.12').broadcast('test-channel', {
    data: 'This notification is IP-targeted.'})