Class: Nibbler::Session

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/nibbler/session.rb

Overview

A parser session

Holds on to data that is not relevant to the parser between calls. For instance, past messages, rejected bytes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Session

Returns a new instance of Session.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :message_lib (Symbol)

    The name of a message library module eg MIDIMessage or Midilib

  • :timestamps (Boolean)

    Whether to report timestamps



25
26
27
28
29
30
# File 'lib/nibbler/session.rb', line 25

def initialize(options = {})
  @timestamps = options[:timestamps] || false
  @callbacks, @processed, @rejected, @messages = [], [], [], []
  @library = MessageLibrary.adapter(options[:message_lib])
  @parser = Parser.new(@library)
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



12
13
14
# File 'lib/nibbler/session.rb', line 12

def messages
  @messages
end

#processedObject (readonly)

Returns the value of attribute processed.



12
13
14
# File 'lib/nibbler/session.rb', line 12

def processed
  @processed
end

#rejectedObject (readonly)

Returns the value of attribute rejected.



12
13
14
# File 'lib/nibbler/session.rb', line 12

def rejected
  @rejected
end

Instance Method Details

#all_messagesArray<Object>

Returns:

  • (Array<Object>)


33
34
35
# File 'lib/nibbler/session.rb', line 33

def all_messages
  @messages | @fragmented_messages
end

#buffer_sString Also known as: buffer_hex

The buffer as a single hex string

Returns:

  • (String)


39
40
41
# File 'lib/nibbler/session.rb', line 39

def buffer_s
  buffer.join
end

#clear_bufferObject

Clear the parser buffer



45
46
47
# File 'lib/nibbler/session.rb', line 45

def clear_buffer
  buffer.clear
end

#clear_messagesObject

Clear the message log



50
51
52
# File 'lib/nibbler/session.rb', line 50

def clear_messages
  @messages.clear
end

#parse(*args) ⇒ Array<Object>, Hash

Parse some input

Parameters:

  • args (*Object)
  • options (Hash)

    (can be included as the last arg)

Returns:

  • (Array<Object>, Hash)


72
73
74
75
76
77
78
79
80
# File 'lib/nibbler/session.rb', line 72

def parse(*args)
  options = args.last.kind_of?(Hash) ? args.pop : {}
  timestamp = options[:timestamp]

  use_timestamps if !timestamp.nil?

  result = process(args)
  log(result, timestamp)
end

#use_timestampsObject

Convert messages to hashes with timestamps



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nibbler/session.rb', line 55

def use_timestamps
  if !@timestamps
    @messages = @messages.map do |message|
      {
        :messages => message,
        :timestamp => nil
      }
    end
    @timestamps = true
  end
end