Module: What::Helpers

Defined in:
lib/what/helpers.rb

Constant Summary collapse

HEALTH =

Take an array of healths and determine overall health, on the principle that overall health == the worst sub-health.

%w(ok warning alert)

Class Method Summary collapse

Class Method Details

.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/what/helpers.rb', line 18

def self.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
  if first_letter_in_uppercase
    lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  else
    lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
  end
end

.curl(uri) {|open(uri).read| ... } ⇒ Object

Yields:

  • (open(uri).read)


36
37
38
# File 'lib/what/helpers.rb', line 36

def self.curl(uri)
  yield(open(uri).read)
end

.overall_health(healths) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/what/helpers.rb', line 6

def self.overall_health(healths)
  worst_health = healths.map do |health|
    HEALTH.index(health) || HEALTH.index('alert')
  end.max

  # No news is null news.
  if worst_health
    HEALTH[worst_health]
  end
end

.process_linesObject

Performs a simple strip of invalid UTF-8 characters on the output of ‘ps aux’



41
42
43
# File 'lib/what/helpers.rb', line 41

def self.process_lines
  `ps aux`.encode('UTF-16', invalid: :replace, undef: :replace).encode('UTF-8').split("\n")
end

.underscore(camel_cased_word) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/what/helpers.rb', line 26

def self.underscore(camel_cased_word)
  word = camel_cased_word.to_s.dup
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end