Class: QuickBase::EventNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/QuickBaseEventNotifier.rb

Overview

This class maintains a list of QuickBase table changes to watch for, how to be notified of the changes, how frequently to check for the changes and when to start and stop checking. It can stop checking after a specific number of checks, or stop checking after a specific number of successful checks. It can also check for records changes that only meet certain conditions.

Defined Under Namespace

Classes: EventCheckPolicy, EventNotification, Notification, RecordCondition, TableEvent

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(qbc = nil, username = nil, password = nil) ⇒ EventNotifier

Returns a new instance of EventNotifier.



30
31
32
33
34
35
36
37
38
# File 'lib/QuickBaseEventNotifier.rb', line 30

def initialize(qbc=nil, username=nil,password=nil)
   @username,@password = username,password
   if qbc
      @qbc = qbc
   elsif @username and @password 
      @qbc = QuickBase::Client.new(@username,@password)
   end   
   @eventNotifications = Array.new
end

Instance Attribute Details

#qbcObject

Returns the value of attribute qbc.



27
28
29
# File 'lib/QuickBaseEventNotifier.rb', line 27

def qbc
  @qbc
end

Class Method Details

.checkOnce(username, password, dbid) ⇒ Object

Simple method to check one time only, 15 minutes from now, then stop



553
554
555
556
557
558
# File 'lib/QuickBaseEventNotifier.rb', line 553

def EventNotifier.checkOnce(username,password,dbid)
   en = EventNotifier.new(nil,username,password)
   notification = EventNotification.new(TableEvent.new(dbid),nil,EventCheckPolicy.new(15,nil,nil,1))
   en.addEventNotification(notification)
   en.startChecking
end

.waitOnce(username, password, dbid) ⇒ Object

Simple method to wait for one QuickBase change then stop



561
562
563
564
565
566
# File 'lib/QuickBaseEventNotifier.rb', line 561

def EventNotifier.waitOnce(username,password,dbid)
   en = EventNotifier.new(nil,username,password)
   notification = EventNotification.new(TableEvent.new(dbid),nil,EventCheckPolicy.new(nil,nil,nil,nil,1))
   en.addEventNotification(notification)
   en.startChecking
end

.watch(username, password, dbid) ⇒ Object

Simple method to get messages when records are added or modified in a table



535
536
537
538
539
540
# File 'lib/QuickBaseEventNotifier.rb', line 535

def EventNotifier.watch(username,password,dbid)
   en = EventNotifier.new(nil,username,password)
   notification = EventNotification.new(TableEvent.new(dbid))
   en.addEventNotification(notification)
   en.startChecking
end

.watch2007Changes(username, password, dbid) ⇒ Object

Simple method to check for records created after 2006 and modified today



569
570
571
572
573
574
575
576
# File 'lib/QuickBaseEventNotifier.rb', line 569

def EventNotifier.watch2007Changes(username,password,dbid)
   en = EventNotifier.new(nil,username,password)
   rc = RecordCondition.new("Date Created", "OAF", "01-01-2007") 
   te = TableEvent.new(dbid,"recordModified",nil,rc)
   notification = EventNotification.new(te)
   en.addEventNotification(notification)
   en.startChecking
end

.watchAndRunCode(username, password, dbid) ⇒ Object

Simple method to run code when records are added or modified in a table



543
544
545
546
547
548
549
550
# File 'lib/QuickBaseEventNotifier.rb', line 543

def EventNotifier.watchAndRunCode(username,password,dbid)
   en = EventNotifier.new(nil,username,password)
   notification = EventNotification.new(TableEvent.new(dbid))
   en.addEventNotification(notification)
   en.startChecking { |eventNotification| 
      yield eventNotification 
   }
end

Instance Method Details

#addEventNotification(eventNotification) ⇒ Object

class EventNotification



421
422
423
# File 'lib/QuickBaseEventNotifier.rb', line 421

def addEventNotification(eventNotification)
   @eventNotifications << eventNotification if eventNotification.validate(@qbc)
end

#launchURL(url) ⇒ Object



525
526
527
528
529
530
531
532
# File 'lib/QuickBaseEventNotifier.rb', line 525

def launchURL(url)
   url.gsub!("&","^&")
   url = "start #{url}" if RUBY_PLATFORM.split("-")[1].include?("mswin")  
   if !system(url)
      message = "Error launching browser at #{url}."
      Tk.messageBox({"icon"=>"error","title"=>"QuickBase Event Notifier", "message" => message})      
   end
end

#notify(notification, url) ⇒ Object

Event occurred: beep and/or show a message on separate thread



472
473
474
475
476
477
# File 'lib/QuickBaseEventNotifier.rb', line 472

def notify(notification,url)
      Tk.bell if notification.beep
      if notification.message
         showMessage(notification.title,notification.message,url,notification.launchBrowser)
      end
end

#showMessage(messageTitle, message, url, launchBrowser) ⇒ Object



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/QuickBaseEventNotifier.rb', line 479

def showMessage(messageTitle,message,url,launchBrowser)

   message = "#{message}\n\nLaunch QuickBase?" if url and launchBrowser
   
   root = TkRoot.new{ title messageTitle }
   frame = TkFrame.new(root){
       pack "side" => "top"
       borderwidth 8 
   }
   messageLabel = TkLabel.new(frame){
       text message
       font "Arial 10 bold"
       pack "side"=>"top"
   }
    if url and launchBrowser      
      buttonFrame = TkFrame.new(frame){
          pack "side" => "bottom"
          width 50 
      }
      yesButton = TkButton.new(buttonFrame){
          text "Yes" 
          font "Arial 10 bold"
          pack "side"=>"left", "padx"=>5, "pady"=>5
      }
      yesButton.command { 
         launchURL(url) 
         Tk.exit
      }
      noButton = TkButton.new(buttonFrame){
          text "No" 
          font "Arial 10 bold"
          pack "side"=>"right","padx"=>5, "pady"=>5
      }
      noButton.command { Tk.exit }
   else
      okButton = TkButton.new(frame){
          text "OK" 
          font "Arial 10 bold"
          pack "side"=>"bottom"
      }
      okButton.command { Tk.exit }
   end
   Tk.mainloop
   Tk.restart
end

#startCheckingObject

Call with a block to run code when an event occurs, or call without a block to show a message and/or beep. The loop stops if all event checks are beyond their stop time.



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/QuickBaseEventNotifier.rb', line 428

def startChecking
   startTime = Time.now
   firstLoop = true
   loop {
      timeNow = Time.now
      stopChecking = true
      @eventNotifications.each { |eventNotification|
         unless eventNotification.stopChecking?
            stopChecking = false
            title,message=eventNotification.tableEventNotificationMessage
            if timeNow > eventNotification.nextCheckTime
               puts "Checking for changes in QuickBase. (#{title} - #{timeNow})"
               checkSucceeded = false
               if eventNotification.eventOccurred?
                  checkSucceeded = true
                  if block_given?
                     yield eventNotification
                  else
                     eventNotification.notification.overrideDefaultTitleAndMessage(title,message)
                     notify(eventNotification.notification,eventNotification.url)
                  end
               end
               eventNotification.setNextCheckTime(false,checkSucceeded)
               unless eventNotification.stopChecking?
                  checkTimeMessage = "Next check time for '#{title}' will be #{eventNotification.nextCheckTime}." 
                  if eventNotification.numSuccessfulChecks > 0
                     checkTimeMessage << " (#{eventNotification.numSuccessfulChecks } more successful checks will be performed)."
                  elsif eventNotification.numChecks > 0
                     checkTimeMessage << " (#{eventNotification.numChecks} more checks will be performed)."
                  end
                  puts checkTimeMessage
               end
            elsif firstLoop
               checkTimeMessage = "Next check time for '#{title}' will be #{eventNotification.nextCheckTime}." 
               puts checkTimeMessage
            end   
         end
      }
      firstLoop = false
      break if stopChecking
   }
end