Skip to content

Commit

Permalink
Remove legacy Ruby 1.9 naming
Browse files Browse the repository at this point in the history
  • Loading branch information
deivid-rodriguez committed Apr 19, 2022
1 parent 6daccac commit 670bb87
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 41 deletions.
3 changes: 1 addition & 2 deletions lib/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ module Mail # :doc:
require 'net/smtp'
require 'mini_mime'

require 'mail/version_specific/ruby_1_9'
RubyVer = Ruby19
require 'mail/version_specific'

require 'mail/version'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# frozen_string_literal: true

module Mail
class Ruby19
class RubyVer
class StrictCharsetEncoder
def encode(string, charset)
case charset
when /utf-?7/i
Mail::Ruby19.decode_utf7(string)
Mail::RubyVer.decode_utf7(string)
else
string.force_encoding(Mail::Ruby19.pick_encoding(charset))
string.force_encoding(Mail::RubyVer.pick_encoding(charset))
end
end
end
Expand All @@ -18,7 +18,7 @@ class BestEffortCharsetEncoder
def encode(string, charset)
case charset
when /utf-?7/i
Mail::Ruby19.decode_utf7(string)
Mail::RubyVer.decode_utf7(string)
else
string.force_encoding(pick_encoding(charset))
end
Expand All @@ -35,7 +35,7 @@ def pick_encoding(charset)
else
charset
end
Mail::Ruby19.pick_encoding(charset)
Mail::RubyVer.pick_encoding(charset)
end
end

Expand All @@ -46,55 +46,55 @@ class << self

# Escapes any parenthesis in a string that are unescaped this uses
# a Ruby 1.9.1 regexp feature of negative look behind
def Ruby19.escape_paren( str )
def RubyVer.escape_paren( str )
re = /(?<!\\)([\(\)])/ # Only match unescaped parens
str.gsub(re) { |s| '\\' + s }
end

def Ruby19.paren( str )
def RubyVer.paren( str )
str = ::Mail::Utilities.unparen( str )
str = escape_paren( str )
'(' + str + ')'
end

def Ruby19.escape_bracket( str )
def RubyVer.escape_bracket( str )
re = /(?<!\\)([\<\>])/ # Only match unescaped brackets
str.gsub(re) { |s| '\\' + s }
end

def Ruby19.bracket( str )
def RubyVer.bracket( str )
str = ::Mail::Utilities.unbracket( str )
str = escape_bracket( str )
'<' + str + '>'
end

def Ruby19.decode_base64(str)
def RubyVer.decode_base64(str)
if !str.end_with?("=") && str.length % 4 != 0
str = str.ljust((str.length + 3) & ~3, "=")
end
str.unpack( 'm' ).first
end

def Ruby19.encode_base64(str)
def RubyVer.encode_base64(str)
[str].pack( 'm' )
end

def Ruby19.has_constant?(klass, string)
def RubyVer.has_constant?(klass, string)
klass.const_defined?( string, false )
end

def Ruby19.get_constant(klass, string)
def RubyVer.get_constant(klass, string)
klass.const_get( string )
end

def Ruby19.transcode_charset(str, from_encoding, to_encoding = Encoding::UTF_8)
def RubyVer.transcode_charset(str, from_encoding, to_encoding = Encoding::UTF_8)
to_encoding = Encoding.find(to_encoding)
replacement_char = to_encoding == Encoding::UTF_8 ? '�' : '?'
charset_encoder.encode(str.dup, from_encoding).encode(to_encoding, :undef => :replace, :invalid => :replace, :replace => replacement_char)
end

# From Ruby stdlib Net::IMAP
def Ruby19.encode_utf7(string)
def RubyVer.encode_utf7(string)
string.gsub(/(&)|[^\x20-\x7e]+/) do
if $1
"&-"
Expand All @@ -105,7 +105,7 @@ def Ruby19.encode_utf7(string)
end.force_encoding(Encoding::ASCII_8BIT)
end

def Ruby19.decode_utf7(utf7)
def RubyVer.decode_utf7(utf7)
utf7.gsub(/&([^-]+)?-/n) do
if $1
($1.tr(",", "/") + "===").unpack("m")[0].encode(Encoding::UTF_8, Encoding::UTF_16BE)
Expand All @@ -115,16 +115,16 @@ def Ruby19.decode_utf7(utf7)
end
end

def Ruby19.b_value_encode(str, encoding = nil)
def RubyVer.b_value_encode(str, encoding = nil)
encoding = str.encoding.to_s
[Ruby19.encode_base64(str), encoding]
[RubyVer.encode_base64(str), encoding]
end

def Ruby19.b_value_decode(str)
def RubyVer.b_value_decode(str)
match = str.match(/\=\?(.+)?\?[Bb]\?(.*)\?\=/m)
if match
charset = match[1]
str = Ruby19.decode_base64(match[2])
str = RubyVer.decode_base64(match[2])
str = charset_encoder.encode(str, charset)
end
transcode_to_scrubbed_utf8(str)
Expand All @@ -133,12 +133,12 @@ def Ruby19.b_value_decode(str)
str.dup.force_encoding(Encoding::UTF_8)
end

def Ruby19.q_value_encode(str, encoding = nil)
def RubyVer.q_value_encode(str, encoding = nil)
encoding = str.encoding.to_s
[Encodings::QuotedPrintable.encode(str), encoding]
end

def Ruby19.q_value_decode(str)
def RubyVer.q_value_decode(str)
match = str.match(/\=\?(.+)?\?[Qq]\?(.*)\?\=/m)
if match
charset = match[1]
Expand All @@ -157,7 +157,7 @@ def Ruby19.q_value_decode(str)
str.dup.force_encoding(Encoding::UTF_8)
end

def Ruby19.param_decode(str, encoding)
def RubyVer.param_decode(str, encoding)
str = uri_parser.unescape(str)
str = charset_encoder.encode(str, encoding) if encoding
transcode_to_scrubbed_utf8(str)
Expand All @@ -166,13 +166,13 @@ def Ruby19.param_decode(str, encoding)
str.dup.force_encoding(Encoding::UTF_8)
end

def Ruby19.param_encode(str)
def RubyVer.param_encode(str)
encoding = str.encoding.to_s.downcase
language = Configuration.instance.param_encode_language
"#{encoding}'#{language}'#{uri_parser.escape(str)}"
end

def Ruby19.uri_parser
def RubyVer.uri_parser
URI::DEFAULT_PARSER
end

Expand All @@ -182,7 +182,7 @@ def Ruby19.uri_parser
# TODO: add this as a test somewhere:
# Encoding.list.map { |e| [e.to_s.upcase == pick_encoding(e.to_s.downcase.gsub("-", "")), e.to_s] }.select {|a,b| !b}
# Encoding.list.map { |e| [e.to_s == pick_encoding(e.to_s), e.to_s] }.select {|a,b| !b}
def Ruby19.pick_encoding(charset)
def RubyVer.pick_encoding(charset)
charset = charset.to_s
encoding = case charset.downcase

Expand Down Expand Up @@ -242,7 +242,7 @@ def Ruby19.pick_encoding(charset)
convert_to_encoding(encoding)
end

def Ruby19.string_byteslice(str, *args)
def RubyVer.string_byteslice(str, *args)
str.byteslice(*args)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/mail/encoding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
describe "#pick_encoding" do
it "picks binary for nil" do
expect { ::Encoding.find(nil) }.to raise_error(TypeError)
expect(Mail::Ruby19.pick_encoding(nil)).to eq(Encoding::BINARY)
expect(Mail::RubyVer.pick_encoding(nil)).to eq(Encoding::BINARY)
end

{
Expand All @@ -210,7 +210,7 @@
}.each do |from, to|
it "should support #{from}" do
expect { ::Encoding.find(from) }.to raise_error(ArgumentError)
expect(Mail::Ruby19.pick_encoding(from)).to eq(to)
expect(Mail::RubyVer.pick_encoding(from)).to eq(to)
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions spec/mail/encodings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -851,10 +851,10 @@ def encode(str, charset)
end

def with_encoder(encoder)
old, Mail::Ruby19.charset_encoder = Mail::Ruby19.charset_encoder, encoder
old, Mail::RubyVer.charset_encoder = Mail::RubyVer.charset_encoder, encoder
yield
ensure
Mail::Ruby19.charset_encoder = old
Mail::RubyVer.charset_encoder = old
end

it "can use a custom encoder" do
Expand All @@ -871,7 +871,7 @@ def with_encoder(encoder)
end

it "can convert ansi with best effort" do
with_encoder Mail::Ruby19::BestEffortCharsetEncoder.new do
with_encoder Mail::RubyVer::BestEffortCharsetEncoder.new do
expect(Mail::Encodings.value_decode("=?windows-1258?Q?SV=3A_Spr=F8sm=E5l_om_tilbod?=")).to eq "SV: Sprøsmål om tilbod"
end
end
Expand Down Expand Up @@ -925,11 +925,11 @@ def convert(from, to)

describe ".pick_encoding" do
it "finds encoding" do
expect(Mail::Ruby19.pick_encoding("Windows-1252")).to eq Encoding::Windows_1252
expect(Mail::RubyVer.pick_encoding("Windows-1252")).to eq Encoding::Windows_1252
end

it "uses binary for unfound" do
expect(Mail::Ruby19.pick_encoding("ISO-Foo")).to eq Encoding::BINARY
expect(Mail::RubyVer.pick_encoding("ISO-Foo")).to eq Encoding::BINARY
end
end
end
8 changes: 4 additions & 4 deletions spec/mail/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1656,10 +1656,10 @@ def message_with_iso_8859_1_charset
end

def with_encoder(encoder)
old, Mail::Ruby19.charset_encoder = Mail::Ruby19.charset_encoder, encoder
old, Mail::RubyVer.charset_encoder = Mail::RubyVer.charset_encoder, encoder
yield
ensure
Mail::Ruby19.charset_encoder = old
Mail::RubyVer.charset_encoder = old
end

let(:message){
Expand All @@ -1678,8 +1678,8 @@ def with_encoder(encoder)
expect(message.inspect_structure).to eq message.inspect
end

it "uses the Ruby19 charset encoder" do
with_encoder(Mail::Ruby19::BestEffortCharsetEncoder.new) do
it "uses the RubyVer charset encoder" do
with_encoder(Mail::RubyVer::BestEffortCharsetEncoder.new) do
message = Mail.new("Content-Type: text/plain;\r\n charset=windows-1258\r\nContent-Transfer-Encoding: base64\r\n\r\nSGkglg==\r\n")
expect(message.decoded).to eq("Hi –")
end
Expand Down
2 changes: 1 addition & 1 deletion spec/mail/version_specific/ruby_1_9_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

describe '.decode_base64' do
it "handles unpadded base64 correctly" do
decoded = Mail::Ruby19.decode_base64("YQ")
decoded = Mail::RubyVer.decode_base64("YQ")
expect(decoded).to eq "a"
end
end

0 comments on commit 670bb87

Please sign in to comment.