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



24
25
26
27
28
29
30
31
32
33
# File 'lib/gri/utils.rb', line 24

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



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

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
# File 'lib/gri/utils.rb', line 7

def load_records dir
  values = LTSV.load_from_file dir + '/.records.txt'
  values.inject({}) {|h, v| h[v['_key']] = v; h}
end

.parse_host_key(s) ⇒ Object



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

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

.parse_key(s) ⇒ Object



48
49
50
# File 'lib/gri/utils.rb', line 48

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

.search_records(dirs, host) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gri/utils.rb', line 12

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



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gri/utils.rb', line 35

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



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

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