module Cramp::Callbacks

Public Instance Methods

before_start(n = 0) click to toggle source
# File lib/cramp/callbacks.rb, line 33
def before_start(n = 0)
  if callback = self.class.before_start_callbacks[n]
    callback_wrapper { send(callback) { before_start(n+1) } }
  else
    continue
  end
end
callback_wrapper() { || ... } click to toggle source
# File lib/cramp/callbacks.rb, line 55
def callback_wrapper
  EM.next_tick do
    begin
      yield
    rescue StandardError, LoadError, SyntaxError => exception
      handle_exception(exception)
    end
  end
end
on_finish() click to toggle source
# File lib/cramp/callbacks.rb, line 49
def on_finish
  self.class.on_finish_callbacks.each do |callback|
    callback_wrapper { send(callback) }
  end
end
on_start() click to toggle source
# File lib/cramp/callbacks.rb, line 41
def on_start
  callback_wrapper { start } if respond_to?(:start)

  self.class.on_start_callback.each do |callback|
    callback_wrapper { send(callback) unless @finished }
  end
end

Private Instance Methods

_invoke_data_callbacks(message) click to toggle source
# File lib/cramp/callbacks.rb, line 81
def _invoke_data_callbacks(message)
  self.class.on_data_callbacks.each do |callback|
    callback_wrapper { send(callback, message) }
  end
end
_receive_protocol10_data(data) click to toggle source
# File lib/cramp/callbacks.rb, line 67
def _receive_protocol10_data(data)
  protocol10_parser.data << data

  messages = @protocol10_parser.process_data
  messages.each do |type, content|
    _invoke_data_callbacks(content) if type == :text
  end
end
_receive_protocol76_data(data) click to toggle source
# File lib/cramp/callbacks.rb, line 76
def _receive_protocol76_data(data)
  data = data.split(Regexp.new('\000([^\377]*)\377')).select{|d| !d.empty? }.collect{|d| d.gsub(Regexp.new('^\x00|\xff$'), '') }
  data.each {|message| _invoke_data_callbacks(message) }
end
handle_exception(exception) click to toggle source
# File lib/cramp/callbacks.rb, line 87
def handle_exception(exception)
  handler = ExceptionHandler.new(@env, exception)

  # Log the exception
  exception_body = handler.dump_exception
  Cramp.logger ? Cramp.logger.error(exception_body) : $stderr.puts(exception_body)

  case @_state
  when :init
    halt 500, {"Content-Type" => 'text/html'}, ENV['RACK_ENV'] == 'development' ? handler.pretty : 'Something went wrong'
  else
    finish
  end
end