PowerTrackV2

A Ruby client for managing PowerTrack 2.0 rules, as well as streaming.

Authentication

First, authentication is managed through environment variables. If you're unfamiliar with this practice, check out The Rubyist's Guide to Environment Variables.

In your shell:

export POWERTRACK_USERNAME=<your_username>
export POWERTRACK_PASSWORD=<your_password>

Rule Management

To add rules to your stream:

require 'powertrack_v2'

url = PowerTrackV2.rules_url(publisher, , stream_label)

rules = [
  PowerTrackV2::Rule.new('rule_value', 'optional_rule_tag'),
  PowerTrackV2::Rule.new('another_value')
]

response_body = PowerTrackV2.add_rules(url, rules)

To remove rules from your stream:

require 'powertrack_v2'

url = PowerTrackV2.rules_url(publisher, , stream_label)

rules = [
  Rule.new('rule_value', 'optional_rule_tag'),
  Rule.new('another_value')
]

response_body = PowerTrackV2.remove_rules(url, rules)

To get all rules for your stream:

require 'powertrack_v2'

url = PowerTrackV2.rules_url(publisher, , stream_label)

response_body = PowerTrackV2.get_rules(url, rules)

Streaming

To stream from powertrack:

require 'powertrack_v2'

url = PowerTrackV2.stream_url(publisher, , stream_label)

PowerTrackV2.stream(url) do |activity|
  puts activity # or whatever you want to do with it.
end

Some things to note about streaming:

  1. The stream function is infinitely blocking, so your streamer should be a separate process from your rule manager process.
  2. Your block is called for every published activity.
  3. The published activity is a raw, unprocessed response from PowerTrack. This library makes no assumptions about what you want to do with the data being streamed in, and leaves processing up to you.
  4. If the connection drops, this library will automatically reconnect using an exponential backoff algorithm.
  5. PowerTrack sends a "\r\n" heartbeat every few seconds. By default, the streamer discards it so you don't have to worry about it. If you want it to pass you the heartbeat, simply add an option to the stream function:
require 'powertrack_v2'

url = PowerTrackV2.stream_url(publisher, , stream_label)

PowerTrackV2.stream(url, ignore_heartbeat: false) do |activity|
  puts activity # or whatever you want to do with it.
end