Class: MetaEvents::TestReceiver

Inherits:
Object
  • Object
show all
Defined in:
lib/meta_events/test_receiver.rb

Overview

A MetaEvents::TestReceiver is a very simple object that conforms to the call signature required by the MetaEvents::Tracker for event receivers. It writes each event as human-readable text to a target, which can be:

  • A block (or any object that responds to #call), which will be passed a String;

  • A Logger (or any object that responds to #info), which will be passed a String;

  • An IO (like STDOUT or STDERR, or any object that responds to #puts), which will be passed a String.

This object is useful for watching and debugging events in development environments.

Instance Method Summary collapse

Constructor Details

#initialize(target = nil, &block) ⇒ TestReceiver

Returns a new instance of TestReceiver.



11
12
13
# File 'lib/meta_events/test_receiver.rb', line 11

def initialize(target = nil, &block)
  @target = target || block || lambda { |string| ::Rails.logger.info(string) }
end

Instance Method Details

#say(string) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/meta_events/test_receiver.rb', line 27

def say(string)
  if @target.respond_to?(:call)
    @target.call "#{string}\n"
  elsif @target.respond_to?(:info)
    @target.info "#{string}"
  else
    @target.puts string
  end
end

#track(distinct_id, event_name, properties) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/meta_events/test_receiver.rb', line 15

def track(distinct_id, event_name, properties)
  string = "Tracked event: #{event_name.inspect} for user #{distinct_id.inspect}"
  properties.keys.sort.each do |k|
    value = properties[k]
    unless value == nil
      string << "\n    %30s: %s" % [ k, properties[k] ]
    end
  end

  say(string)
end