Module: RHCP::EncodingHelper

Defined in:
lib/rhcp/encoding_helper.rb

Class Method Summary collapse

Class Method Details

.change_string_values(thing, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rhcp/encoding_helper.rb', line 7

def self.change_string_values(thing, &block)
  result = nil
  case thing.class.to_s
  when "Array"
    result = []
    thing.each do |t|
      result << change_string_values(t, &block)
    end
  when "Hash"
    result = {}
    thing.each do |k,v|
      result[k] = change_string_values(v, &block)
    end
  #when "String","Fixnum","Boolean","TrueClass","FalseClass"
  when "String"
    result = block.call(thing.to_s)
  when "Fixnum","TrueClass","FalseClass"
    result = thing
  when "Proc","NilClass","Binding"
    # ignore
  else
    $logger.debug("don't know how to encode #{thing.class} - skipping")
  end
  result
end

.from_base64(thing) ⇒ Object



39
40
41
42
43
# File 'lib/rhcp/encoding_helper.rb', line 39

def self.from_base64(thing)
  change_string_values(thing) do |x|
    Base64.decode64(x)
  end
end

.to_base64(thing) ⇒ Object



33
34
35
36
37
# File 'lib/rhcp/encoding_helper.rb', line 33

def self.to_base64(thing)
  change_string_values(thing) do |x|
    Base64.encode64(x)
  end
end