Class: SimpleSpeaker::Speaker

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

Instance Method Summary collapse

Constructor Details

#initialize(logger_path = nil, logger_error_path = nil) ⇒ Speaker

Returns a new instance of Speaker.



6
7
8
9
10
11
12
# File 'lib/simple_speaker.rb', line 6

def initialize(logger_path = nil, logger_error_path = nil)
  @logger = Logger.new(logger_path) unless logger_path.nil?
  @logger_error = Logger.new(logger_error_path) unless logger_error_path.nil?
  @daemons = []
  @user_input = nil
  @new_line = "\n"
end

Instance Method Details

#ask_if_needed(question, no_prompt = 0, default = 'y', thread = Thread.current) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/simple_speaker.rb', line 14

def ask_if_needed(question, no_prompt = 0, default = 'y', thread = Thread.current)
  ask_if_needed = default
  if no_prompt.to_i == 0
    self.speak_up(question, 0, thread, 1)
    if Daemon.is_daemon?
      wtime = 0
      while @user_input.nil?
        sleep 1
        ask_if_needed = @user_input
        break if (wtime += 1) > USER_INPUT_TIMEOUT
      end
      @user_input = nil
    else
      ask_if_needed = STDIN.gets.strip
    end
  end
  ask_if_needed
end

#daemon_send(str) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/simple_speaker.rb', line 33

def daemon_send(str)
  if Thread.current[:current_daemon]
    Thread.current[:current_daemon].send_data "#{str}\n"
  else
    puts str
  end
end

#email_msg_add(str, in_mail, thread) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/simple_speaker.rb', line 41

def email_msg_add(str, in_mail, thread)
  str = "[*] #{str}" if in_mail.to_i > 0
  thread[:email_msg] << str.to_s.force_encoding("UTF-8") + @new_line if thread[:email_msg]
  if in_mail.to_i > 0
    thread[:send_email] = in_mail.to_i if thread[:send_email]
  end
end

#log(str, error = 0) ⇒ Object



61
62
63
64
# File 'lib/simple_speaker.rb', line 61

def log(str, error = 0)
  @logger.info(str) if @logger
  @logger_error.error(str) if @logger_error && error.to_i > 0
end

#speak_up(str, in_mail = 1, thread = Thread.current, immediate = 0) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/simple_speaker.rb', line 49

def speak_up(str, in_mail = 1, thread = Thread.current, immediate = 0)
  thread[:log_msg] << str + @new_line if thread[:log_msg]
  if immediate.to_i > 0 || thread[:log_msg].nil?
    str.each_line do |l|
      daemon_send(l)
      log("#{'[' + thread[:object].to_s + ']' if thread[:object].to_s != ''}#{l}")
    end
  end
  email_msg_add(str, in_mail, thread) if in_mail.to_i >= 0
  str
end

#tell_error(e, src, in_mail = 1, thread = Thread.current) ⇒ Object



66
67
68
69
70
71
# File 'lib/simple_speaker.rb', line 66

def tell_error(e, src, in_mail = 1, thread = Thread.current)
  @logger_error.error(e) if @logger_error
  speak_up("ERROR in '#{src}'" + @new_line, in_mail, thread)
  speak_up(e.to_s + @new_line, in_mail, thread)
  speak_up(e.backtrace[0..2].join(@new_line) + @new_line, in_mail, thread)
end

#user_input(input) ⇒ Object



73
74
75
# File 'lib/simple_speaker.rb', line 73

def user_input(input)
  @user_input = input
end