class Nibbler::Session
A parser session
Holds on to data that is not relevant to the parser between calls. For instance, past messages, rejected bytes
Attributes
Public Class Methods
@param [Hash] options @option options [Symbol] :message_lib The name of a message library module eg MIDIMessage
or Midilib
@option options [Boolean] :timestamps Whether to report timestamps
# 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
Public Instance Methods
@return [Array<Object>]
# File lib/nibbler/session.rb, line 33 def all_messages @messages | @fragmented_messages end
The buffer as a single hex string @return [String]
# File lib/nibbler/session.rb, line 39 def buffer_s buffer.join end
Clear the parser buffer
# File lib/nibbler/session.rb, line 45 def clear_buffer buffer.clear end
Clear the message log
# File lib/nibbler/session.rb, line 50 def clear_messages @messages.clear end
Parse some input @param [*Object] args @param [Hash] options (can be included as the last arg) @option options [Time] :timestamp A timestamp to store with the messages that result @return [Array<Object>, Hash]
# 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
Convert messages to hashes with timestamps
# 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
Private Instance Methods
A report on the given number of most recent messages
If timestamps are being used, will be a hash of messages and timestamp, otherwise just the messages
The messages type will vary depending on the number of messages that were parsed: 0 messages: nil 1 message: the message >1 message: an array of messages
@param [Integer] num The number of new messages to report @return [Array<Object>, Hash]
# File lib/nibbler/session.rb, line 129 def get_output(num) messages = @messages.last(num) messages.count < 2 ? messages.first : messages end
@param [Hash] parser_report @param [Time] timestamp @return [Array<Object>, Hash]
# File lib/nibbler/session.rb, line 95 def log(parser_report, timestamp) num = log_message(parser_report[:messages], :timestamp => timestamp) @processed += parser_report[:processed] @rejected += parser_report[:rejected] get_output(num) end
@param [Array<Object>] messages The MIDI messages to log @return [Integer] The number of MIDI messages logged
# File lib/nibbler/session.rb, line 104 def log_message(messages, options = {}) if @timestamps messages_for_log = messages.count == 1 ? messages.first : messages @messages << { :messages => messages_for_log, :timestamp => options[:timestamp] } else @messages += messages end messages.count end
Process the input @param [Array<Object>] input @return [Hash]
# File lib/nibbler/session.rb, line 87 def process(input) queue = DataProcessor.process(input) @parser.process(queue) end