Class: CampfireChat::Monitor

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/campfire_chat/monitor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_run_timeObject

Returns the value of attribute last_run_time.



4
5
6
# File 'lib/campfire_chat/monitor.rb', line 4

def last_run_time
  @last_run_time
end

Class Method Details

.retry_timeObject



6
7
8
# File 'lib/campfire_chat/monitor.rb', line 6

def self.retry_time
  30
end

.runObject



14
15
16
# File 'lib/campfire_chat/monitor.rb', line 14

def self.run
  instance.run
end

.sleep_timeObject



10
11
12
# File 'lib/campfire_chat/monitor.rb', line 10

def self.sleep_time
  retry_time / 2
end

Instance Method Details

#check_client?Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/campfire_chat/monitor.rb', line 39

def check_client?
  return true if last_run_time.nil?
  last_run_time + self.class.retry_time < Time.now
end

#check_messagesObject



28
29
30
31
32
33
# File 'lib/campfire_chat/monitor.rb', line 28

def check_messages
  client.messages.each do |message|
    notification = prepare_notification(message)
    notifier.push(notification)
  end
end

#clientObject



48
49
50
# File 'lib/campfire_chat/monitor.rb', line 48

def client
  @client ||= CampfireChat::Client.new
end

#notifierObject



52
53
54
# File 'lib/campfire_chat/monitor.rb', line 52

def notifier
  @notifier ||= CampfireChat::Notifier::Growl.instance
end

#prepare_notification(message) ⇒ Object



44
45
46
# File 'lib/campfire_chat/monitor.rb', line 44

def prepare_notification(message)
  CampfireChat::Notification.build(message)
end

#runObject



18
19
20
21
22
23
24
25
26
# File 'lib/campfire_chat/monitor.rb', line 18

def run
  loop do
    if check_client?
      check_messages
      set_last_run_time
      sleep self.class.sleep_time
    end
  end
end

#set_last_run_timeObject



35
36
37
# File 'lib/campfire_chat/monitor.rb', line 35

def set_last_run_time
  self.last_run_time = Time.now
end