Module: Helpers

Includes:
Rack::Utils, Sinatra::ContentFor2, Sinatra::Partials
Defined in:
lib/runit-man/helpers.rb

Constant Summary collapse

KILOBYTE =
1024
MEGABYTE =
1024 * KILOBYTE
GIGABYTE =
1024 * MEGABYTE
TERABYTE =
1024 * GIGABYTE

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#even_or_odd_stateObject

Returns the value of attribute even_or_odd_state.



13
14
15
# File 'lib/runit-man/helpers.rb', line 13

def even_or_odd_state
  @even_or_odd_state
end

Instance Method Details

#addrObject



15
16
17
# File 'lib/runit-man/helpers.rb', line 15

def addr
  env.include?('X_REAL_IP') ? env['X_REAL_IP'] : env['REMOTE_ADDR']
end

#all_files_to_viewObject



43
44
45
46
47
# File 'lib/runit-man/helpers.rb', line 43

def all_files_to_view
  (files_to_view + service_infos.map do |service|
    service.files_to_view
  end.flatten).uniq.sort
end

#even_or_oddObject



83
84
85
86
# File 'lib/runit-man/helpers.rb', line 83

def even_or_odd
  self.even_or_odd_state = !even_or_odd_state
  even_or_odd_state
end

#files_to_viewObject



35
36
37
38
39
40
41
# File 'lib/runit-man/helpers.rb', line 35

def files_to_view
  RunitMan::App.files_to_view.map do |f|
    File.symlink?(f) ? File.expand_path(File.readlink(f), File.dirname(f)) : f
  end.select do |f|
    File.readable?(f)
  end.uniq.sort
end

#host_nameObject



23
24
25
# File 'lib/runit-man/helpers.rb', line 23

def host_name
  Utils.host_name
end

#human_bytes(bytes) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/runit-man/helpers.rb', line 93

def human_bytes(bytes)
  sign = (bytes >= 0) ? '' : '-'
  suffix = 'B'
  bytes = bytes.abs.to_f

  if bytes >= TERABYTE
    bytes /= TERABYTE
    suffix = 'TB'
  elsif bytes >= GIGABYTE
    bytes /= GIGABYTE
    suffix = 'GB'
  elsif bytes >= MEGABYTE
    bytes /= MEGABYTE
    suffix = 'MB'
  elsif bytes >= KILOBYTE
    bytes /= KILOBYTE
    suffix = 'KB'
  end

  bytes = ((bytes * 100 + 0.5).to_i.to_f / 100)

  "#{sign}#{bytes}#{t("runit.services.log.#{suffix}")}"
end

#log(s) ⇒ Object



19
20
21
# File 'lib/runit-man/helpers.rb', line 19

def log(s)
  logger.info s
end


79
80
81
# File 'lib/runit-man/helpers.rb', line 79

def log_downloads_link(name)
  "<a href=\"/#{name}/log-downloads/\">#{h(t('runit.services.log.downloads'))}&hellip;</a>"
end


66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/runit-man/helpers.rb', line 66

def log_link(name, options = {})
  count = (options[:count] || 100).to_i
  title = options[:title].to_s || count
  blank = options[:blank] || false
  hint  = options[:hint].to_s  || ''
  raw   = options[:raw] || false
  id    = options[:id] || false
  hint  = " title=\"#{h(hint)}\"" unless hint.empty?
  blank = blank ? ' target="_blank"' : ''

  "<a#{hint}#{blank} href=\"/#{name}/log#{ (count != 100) ? "/#{count}" : '' }#{ id ? "/#{id}" : ''  }#{ raw ? '.txt' : '' }#footer\">#{h(title)}</a>"
end

#service_action(name, action, label, enabled = true) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/runit-man/helpers.rb', line 49

def service_action(name, action, label, enabled = true)
  partial :service_action, :locals => {
    :name    => name,
    :action  => action,
    :label   => label,
    :enabled => enabled
  }
end

#service_infosObject



31
32
33
# File 'lib/runit-man/helpers.rb', line 31

def service_infos
  ServiceInfo::Base.all
end

#service_signal(name, signal, label) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/runit-man/helpers.rb', line 58

def service_signal(name, signal, label)
  partial :service_signal, :locals => {
    :name   => name,
    :signal => signal,
    :label  => label
  }
end

#stat_subst(s) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/runit-man/helpers.rb', line 117

def stat_subst(s)
  s.split(/\s/).map do |s|
    if s =~ /(\w+)/
      word = $1

      if t("runit.services.table.subst.#{word}") !~ /translation missing/
        s.sub(word, t("runit.services.table.subst.#{word}"))
      else
        s
      end
    else
      s
    end
  end.join(' ')
end

#t(*args) ⇒ Object



27
28
29
# File 'lib/runit-man/helpers.rb', line 27

def t(*args)
  Utils.t(*args)
end