require 'dionysus' require 'active_support/base64' ## # Adds string encoding convenience methods. # # require 'dionysus/string' class String HEX_REGEXP = /^[a-f0-9]+$/ BASE64_REGEXP = /^[A-Za-z0-9\+\/\=]+$/ ## # Encode the Base64 (without newlines) def encode64s() Base64.encode64s(self); end ## # Encode to Base 64 def encode64() Base64.encode64(self); end ## # Decode from Base 64 def decode64() Base64.decode64(self); end ## # Encode to hexidecimal def encode_hexidecimal() self.unpack('H*').first; end alias_method :encode_hex, :encode_hexidecimal ## # Decode from hexidecimal def decode_hexidecimal() [self].pack('H*'); end alias_method :decode_hex, :decode_hexidecimal ## # Detect the encoding of the string def detect_encoding return nil if blank? if match(HEX_REGEXP) :hex elsif match(BASE64_REGEXP) :base64 else :binary end end end