Sha256: 01f1787ea3eaa849911e7a9922d463325454cf1947bd22ef53df363dc5d269d6

Contents?: true

Size: 949 Bytes

Versions: 1

Compression:

Stored size: 949 Bytes

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dionysus-0.3.0 lib/dionysus/string.rb