Module: Watobo::Mixin::Transcoders

Defined in:
lib/watobo/mixins/transcoders.rb

Instance Method Summary collapse

Instance Method Details

#b64decodeObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/watobo/mixins/transcoders.rb', line 34

def b64decode
  err_count = 0
    b64string = self
  begin
    rs = Base64.strict_decode64(b64string)
    #rs = Base64.decode64(b64string)
    return rs
  rescue
    b64string.gsub!(/.$/,'')
    err_count += 1
    retry if err_count < 4
    return ""
  end
end

#b64encodeObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/watobo/mixins/transcoders.rb', line 49

def b64encode
  begin
    plain = self
    #rs = Base64.strict_encode64(plain)
    rs = Base64.strict_encode64(plain)
    # we only need a simple string without linebreaks
    #rs.gsub!(/\n/,'')
    #rs.strip!
    return rs
  rescue
    return ""
  end
end

#hex2intObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/watobo/mixins/transcoders.rb', line 63

def hex2int
  begin
    plain = self.strip
    if plain =~ /^[0-9a-fA-F]{1,8}$/ then
    return plain.hex
    else
      return ""
    end
  rescue
    return ""
  end
end

#hexdecodeObject



86
87
88
89
# File 'lib/watobo/mixins/transcoders.rb', line 86

def hexdecode

  [ self ].pack("H*")
end

#hexencodeObject



76
77
78
79
80
81
82
83
84
# File 'lib/watobo/mixins/transcoders.rb', line 76

def hexencode
  begin

    self.unpack("H*")[0]
  rescue
    return ""
  end

end

#url_decodeObject



30
31
32
# File 'lib/watobo/mixins/transcoders.rb', line 30

def url_decode
  CGI::unescape(self)
end

#url_encodeObject



26
27
28
# File 'lib/watobo/mixins/transcoders.rb', line 26

def url_encode
  CGI::escape(self)
end