Module: What::Helpers

Defined in:
lib/what/helpers.rb

Class Method Summary collapse

Class Method Details

.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object



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

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) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/what/helpers.rb', line 37

def self.curl(uri)
  curl = Curl::Easy.new(uri)
  curl.on_complete do |easy|
    yield(easy.body_str)
  end
  curl.perform
end

.overall_health(healths) ⇒ Object

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



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

def self.overall_health(healths)
  healths.reduce('ok') do |overall, current|
    case current
    when 'ok'
      overall
    when 'warning'
      'warning' if overall != 'alert'
    else
      'alert'
    end
  end
end

.underscore(camel_cased_word) ⇒ Object



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

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