Module: Kennel::Utils

Defined in:
lib/kennel/utils.rb

Class Method Summary collapse

Class Method Details

.ask(question) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/kennel/utils.rb', line 16

def ask(question)
  printf color(:red, "#{question} -  press 'y' to continue: ")
  begin
    STDIN.gets.chomp == "y"
  rescue Interrupt # do not show a backtrace if user decides to Ctrl+C here
    printf "\n"
    exit 1
  end
end

.capture_sh(command) ⇒ Object



43
44
45
46
47
# File 'lib/kennel/utils.rb', line 43

def capture_sh(command)
  result = `#{command} 2>&1`
  raise "Command failed:\n#{command}\n#{result}" unless $CHILD_STATUS.success?
  result
end

.capture_stdoutObject



35
36
37
38
39
40
41
# File 'lib/kennel/utils.rb', line 35

def capture_stdout
  $stdout = StringIO.new
  yield
  $stdout.string
ensure
  $stdout = STDOUT
end

.color(color, text) ⇒ Object



26
27
28
29
# File 'lib/kennel/utils.rb', line 26

def color(color, text)
  code = { red: 31, green: 32, yellow: 33 }.fetch(color)
  "\e[#{code}m#{text}\e[0m"
end

.path_to_url(path) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/kennel/utils.rb', line 49

def path_to_url(path)
  if subdomain = ENV["DATADOG_SUBDOMAIN"]
    "https://#{subdomain}.datadoghq.com#{path}"
  else
    path
  end
end

.presence(value) ⇒ Object



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

def presence(value)
  value.empty? ? nil : value
end

.snake_case(string) ⇒ Object



5
6
7
8
9
10
# File 'lib/kennel/utils.rb', line 5

def snake_case(string)
  string.gsub(/::/, "_") # Foo::Bar -> foo_bar
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') # FOOBar -> foo_bar
    .gsub(/([a-z\d])([A-Z])/, '\1_\2') # fooBar -> foo_bar
    .downcase
end

.strip_shell_control(text) ⇒ Object



31
32
33
# File 'lib/kennel/utils.rb', line 31

def strip_shell_control(text)
  text.gsub(/\e\[\d+m(.*?)\e\[0m/, "\\1").tr("\b", "")
end