Module: NSCA::Helper

Defined in:
lib/nsca.rb

Class Method Summary collapse

Class Method Details

.class_name_gen(label) ⇒ Object



15
16
17
18
19
20
# File 'lib/nsca.rb', line 15

def class_name_gen label
  clname = label.gsub( /\W+/, '_').sub /^[0-9_]+/, ''
  return nil  if clname.empty?
  clname[0] = clname[0].upcase
  clname.to_sym
end

.crc32_streamObject



41
42
43
44
45
46
47
48
49
# File 'lib/nsca.rb', line 41

def crc32_stream
  sum = 0xFFFFFFFF
  lambda do |str|
    sum = str.bytes.inject sum do |r, b|
      8.times.inject( r^b) {|r,_i| (r>>1) ^ (0xEDB88320 * (r&1)) }
    end  if str
    sum ^ 0xFFFFFFFF
  end
end

.xor_stream(key) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/nsca.rb', line 22

def xor_stream key
  key = case key
  when Array  then key
  when String then key.bytes.to_a
  when Enumerable then key.to_a
  end
  return lambda{|x|x}  if [nil, '', []].include? key
  length = key.length
  i = 0
  lambda do |str|
    r = ''
    str.bytes.each_with_index do |c, j|
      r[j] = (c ^ key[i]).chr
      i = (i + 1) % length
    end
    r
  end
end