Module: JSONModel::Notification

Defined in:
common/jsonmodel_client.rb

Constant Summary

@@notification_handlers =
[]

Class Method Summary (collapse)

Class Method Details

+ (Object) add_notification_handler(code = nil, &block)



66
67
68
# File 'common/jsonmodel_client.rb', line 66

def self.add_notification_handler(code = nil, &block)
  @@notification_handlers << {:code => code, :block => block}
end

+ (Object) start_background_thread



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'common/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