class PrePlaySports::Event

Constants

FIELD_WHITELIST

will filter the hash that was given to keep only the data that we want to display on the logs dependending on the data type

Attributes

context[RW]
data[RW]
event_type[RW]

Public Class Methods

new(event_type, data, context) click to toggle source
# File lib/pre_play_sports/event.rb, line 6
def initialize(event_type, data, context)
  @event_type = event_type
  @data = filter(data)
  @context = context
end

Public Instance Methods

to_scrolls_hash() click to toggle source
# File lib/pre_play_sports/event.rb, line 12
def to_scrolls_hash
  event = {
    d: { type: event_type }.merge!(data),
    c: context,
    m: {
      dyno: $dyno_name,
      created_at: Time.now.to_s
    }
  }
  flatten_keys(event).merge!(preplay_event: true)
end

Private Instance Methods

filter(data) click to toggle source
# File lib/pre_play_sports/event.rb, line 68
def filter(data)
  return {} unless FIELD_WHITELIST[event_type]
  data.select {|k| FIELD_WHITELIST[event_type].include? k.to_s}
end
flatten_keys(hash, namespace = nil) click to toggle source

this “flatten” a hash in order to use it on a Scrolls.log line

{
  data: {
    foo: 'bar',
    baz: 42
  }
}
become :
{
  :'data.foo' => 'bar',
  :'data.baz' => 42
}
# File lib/pre_play_sports/event.rb, line 38
def flatten_keys(hash, namespace = nil)
  namespace = namespace.nil? ? '' : "#{namespace}."
  hash.inject(Hash.new) do |result, key, value|

    hash.each do |k, v|
      if v.is_a? Hash
        # recursive call with the new namespace
        result.merge! flatten_keys(v, namespace + k.to_s)
      else
        # direct set in the hash of the v
        result["#{namespace}#{k}".to_sym] = v
      end
    end
    result
  end
end