Module: GRI::Utils

Included in:
DSList, Graph, InfluxdbWriter, List, Page, Updater
Defined in:
lib/gri/utils.rb

Class Method Summary collapse

Class Method Details

.get_prop(record) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/gri/utils.rb', line 28

def get_prop record
  h = {}
  data_name, index = parse_key record['_key']
  record['_index'] = index
  if data_name and (specs = DEFS.get_specs data_name) and specs[:prop]
    specs[:prop].each {|k1, k2| h[k1] = record[k2] if record[k2]}
    h[:name] ||= specs[:prop][:name]
  end
  h
end

.key_encode(s) ⇒ Object



64
65
66
67
# File 'lib/gri/utils.rb', line 64

def key_encode s
  s.to_s.tr(':/ =', '\-\-__').gsub(/[^-a-zA-Z0-9_.]/n) { #/
    "%02X"%$&.unpack('C').first}
end

.load_records(dir) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/gri/utils.rb', line 7

def load_records dir
  if File.exist?(path = dir + '/.records.txt')
    values = LTSV.load_from_file path
    values.inject({}) {|h, v| h[v['_key']] = v; h}
  else
    nil
  end
end

.parse_host_key(s) ⇒ Object



56
57
58
# File 'lib/gri/utils.rb', line 56

def parse_host_key s
  s.to_s.scan(/\A([-\.A-Za-z0-9]+)_([^_\d]*)(?:_?(.*))/).first
end

.parse_key(s) ⇒ Object



52
53
54
# File 'lib/gri/utils.rb', line 52

def parse_key s
  s.to_s.scan(/\A([^_\d]*)(?:_?(.*))/).first
end

.search_records(dirs, host) ⇒ Object



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

def search_records dirs, host
  for dir in dirs
    path = "#{dir}/#{host}/.records.txt"
    if File.exist? path
      values = LTSV.load_from_file path
      rh = values.inject({}) {|h, v| (k = v['_key']) and h[k] = v; h}
      return [dir, rh]
    end
  end
  nil
end

.update_ltsv_file(path, key, other) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gri/utils.rb', line 39

def update_ltsv_file path, key, other
  if File.exist? path
    values = LTSV.load_from_file path
    nvalues = values.inject({}) {|h, v| h[v[key]] = v; h}
    for k in other.keys
      nvalues[k] ? nvalues[k].merge!(other[k]) : (nvalues[k] = other[k])
    end
  else
    nvalues = other
  end
  LTSV.dump_to_file nvalues, path
end

.url_encode(s) ⇒ Object



60
61
62
# File 'lib/gri/utils.rb', line 60

def url_encode(s)
  s.to_s.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
end