Module: JSONModel::Notification

Defined in:
common/jsonmodel_client.rb

Constant Summary collapse

@@notification_handlers =
[]

Class Method Summary collapse

Class Method Details

.add_notification_handler(code = nil, &block) ⇒ Object



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

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

.start_background_threadObject



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
98
# File 'common/jsonmodel_client.rb', line 71

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