Class: Util

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

Instance Method Summary collapse

Constructor Details

#initialize(debug) ⇒ Util

Returns a new instance of Util.



2
3
4
# File 'lib/util.rb', line 2

def initialize(debug)
  @debug = debug
end

Instance Method Details

#check_affirmativeObject



32
33
34
35
36
37
# File 'lib/util.rb', line 32

def check_affirmative
  ans = ['y', 'yes', ''].include?(gets.chomp) ? true : false
  dbg("affirmative = #{ans}")
  puts "\n"
  return ans
end

#dbg(msg) ⇒ Object



16
17
18
19
20
# File 'lib/util.rb', line 16

def dbg(msg)
  if @debug
    puts "debug: #{msg}".blue
  end
end

#debug_bottomObject



12
13
14
# File 'lib/util.rb', line 12

def debug_bottom
  "#{('-'*80).blue}"
end

#debug_top(data) ⇒ Object



7
8
9
# File 'lib/util.rb', line 7

def debug_top(data)
  "\n\n\n#{('-'*80).blue}\n#{'raw data:'.yellow}\n#{data.inspect.yellow}\n\n#{'formatted:'.green}\n"
end

#decrypt(s) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/util.rb', line 73

def decrypt(s)
  if s
    carr = []
    s.chomp.each_byte do |c|
      (33..126).to_a.include?(c - 20) ? carr << (c - 20).chr : carr << (c - 20 + 94).chr
    end
    carr.join
  else
    ''
  end
end

#display_data(header, data) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/util.rb', line 22

def display_data(header, data)
  if @debug then puts debug_top(data) end
    
  data.split(/\r\n?/).each do |line|
    puts format_line(header, line)
  end

  if @debug then puts debug_bottom end
end

#display_error(error) ⇒ Object



45
46
47
48
49
50
# File 'lib/util.rb', line 45

def display_error(error)
  if @debug
    puts error.backtrace
    puts error
  end
end

#encrypt(s) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/util.rb', line 59

def encrypt(s)
  if s
    ecarr = []
    s.chomp.each_byte do |c|
      (33..126).to_a.include?(c + 20) ? ecarr << (c + 20).chr : ecarr << (c + 20 - 94).chr
    end
    ecarr.join
  else
    ''
  end
end

#format_line(header, line) ⇒ Object



39
40
41
42
43
# File 'lib/util.rb', line 39

def format_line(header, line)
  if not line.chomp.empty?
    "#{header.blue}#{line}"
  end
end

#show_summary(worker) ⇒ Object



53
54
55
56
57
# File 'lib/util.rb', line 53

def show_summary(worker)
  if @debug
    puts "\n\n#{worker.to_s.blue}\n"
  end
end