module JSONModel::Notification

Public Class Methods

add_notification_handler(code = nil, &block) click to toggle source
# File lib/aspace_client/jsonmodel_client.rb, line 66
def self.add_notification_handler(code = nil, &block)
  @@notification_handlers << {:code => code, :block => block}
end
start_background_thread() click to toggle source
# File lib/aspace_client/jsonmodel_client.rb, line 70
def self.start_background_thread
  Thread.new do
    sequence = 0

    while true
      begin
        notifications = JSONModel::HTTP::get_json('/notifications',
                                                  :last_sequence => sequence)

        notifications.each do |notification|
          @@notification_handlers.each do |handler|
            if handler[:code].nil? or handler[:code] == notification["code"]
              begin
                handler[:block].call(notification["code"], notification["params"])
              rescue
                $stderr.puts("ERROR: Failed to handle notification #{notification.inspect}: #{$!}")
              end
            end
          end
        end

        sequence = notifications.last['sequence']
      rescue
        sleep 5
      end
    end
  end
end