class PrePlay::Event

Constants

VERSION

Public Class Methods

configuration(&block) click to toggle source
# File lib/preplay/event.rb, line 39
def self.configuration(&block)
  @@configuration ||= Configuration.new(&block)
end
configure(&block) click to toggle source
# File lib/preplay/event.rb, line 35
def self.configure(&block)
  configuration(&block)
end

Private Class Methods

from_hash(hash) click to toggle source
# File lib/preplay/event.rb, line 90
def self.from_hash(hash)
  @data = {
    'data' => {},
    'context' => {},
    'metadata' => {}
  }

  hash.each do |k, v|
    prefix, key = k.to_s.split('.', 2)
    next unless %w(d c m).include? prefix

    event_part = case prefix
                 when 'd' then @data['data']
                 when 'c' then @data['context']
                 when 'm' then @data['metadata']
                 end

    event_part[key] = v
  end

  Event.new(@data['data'], @data['context'], @data['metadata'])
end
parse(log_line) click to toggle source
# File lib/preplay/event.rb, line 85
def self.parse(log_line)
  data = Shellwords.shellsplit(log_line)
  from_hash Hash[data.map{|el| el.split('=')}]
end

Public Instance Methods

to_scrolls(opts = nil) click to toggle source
# File lib/preplay/event.rb, line 45
def to_scrolls(opts = nil)
  event = {
    'd' => data,
    'c' => context,
    'm' => metadata
  }
  flatten_keys(event).merge!(preplay_event: true)
end

Private Instance Methods

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/preplay/event.rb, line 68
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