Permalink
Browse files

Change transfer encodings to use a registration mechansim

  to allow for 7bit and 8bit
  • Loading branch information...
1 parent af3ecca commit 72b418cdc2322d9a31627f6e5d11393a4f28c17e @pzb pzb committed Mar 10, 2010
View
@@ -0,0 +1,21 @@
+# encoding: utf-8
+module Mail
+ module Encodings
+ class SevenBit
+
+ # Binary is an identiy encoding, meaning nothing to do
+
+ # Decode the string
+ def self.decode(str)
+ str
+ end
+
+ # Encode the string
+ def self.encode(str)
+ str
+ end
+
+ Encodings.register('7bit', self)
+ end
+ end
+end
View
@@ -0,0 +1,21 @@
+# encoding: utf-8
+module Mail
+ module Encodings
+ class EightBit
+
+ # Binary is an identiy encoding, meaning nothing to do
+
+ # Decode the string
+ def self.decode(str)
+ str
+ end
+
+ # Encode the string
+ def self.encode(str)
+ str
+ end
+
+ Encodings.register('8bit', self)
+ end
+ end
+end
@@ -12,7 +12,8 @@ def self.decode(str)
def self.encode(str)
RubyVer.encode_base64( str )
end
-
+
+ Encodings.register('base64', self)
end
end
-end
+end
@@ -14,7 +14,8 @@ def self.decode(str)
def self.encode(str)
str
end
-
+
+ Encodings.register('binary', self)
end
end
end
@@ -7,15 +7,25 @@ class UnknownEncodingType < StandardError #:nodoc:
module Encodings
include Mail::Patterns
-
+
+ @transfer_encodings = {}
+
+ # Register transfer encoding
+ #
+ # Example
+ #
+ # Encodings.register "base64", Mail::Encodings::Base64
+ def Encodings.register(name, cls)
+ @transfer_encodings[name.to_s.gsub("-", "_").downcase] = cls
+ end
+
# Is the encoding we want defined?
#
# Example:
#
# Encodings.defined?(:base64) #=> true
def Encodings.defined?( str )
- string = str.to_s.split(/[_-]/).map { |v| v.capitalize }.join('')
- RubyVer.has_constant?(Mail::Encodings, string)
+ @transfer_encodings.include? str.to_s.gsub("-", "_").downcase
end
# Gets a defined encoding type, QuotedPrintable or Base64 for now.
@@ -27,8 +37,7 @@ def Encodings.defined?( str )
#
# Encodings.get_encoding(:base64) #=> Mail::Encodings::Base64
def Encodings.get_encoding( str )
- string = str.to_s.split(/[_-]/).map { |v| v.capitalize }.join('')
- RubyVer.get_constant(Mail::Encodings, string)
+ @transfer_encodings[str.to_s.gsub("-", "_").downcase]
end
# Encodes a parameter value using URI Escaping, note the language field 'en' can
@@ -21,6 +21,8 @@ def self.quoted_printable_encode(character)
character.each_byte { |b| result << "=%02X" % b }
result
end
+
+ Encodings.register('quoted-printable', self)
end
end
-end
+end

0 comments on commit 72b418c

Please sign in to comment.