Permalink
Browse files

Merge pull request #237 from urbanautomaton/master

Mail::Message#to_yaml - include all necessary instance variables
  • Loading branch information...
2 parents 2d490ff + 3989021 commit ebebdd4cec945413a37061edcf0be8ed6ac39214 @mikel mikel committed May 23, 2011
Showing with 78 additions and 25 deletions.
  1. +27 −7 lib/mail/message.rb
  2. +51 −18 spec/mail/message_spec.rb
View
@@ -1711,19 +1711,39 @@ def encoded
buffer
end
- def to_yaml
- ready_to_send!
+ def to_yaml(opts = {})
hash = {}
+ hash['headers'] = {}
header.fields.each do |field|
- hash[field.name] = field.value
+ hash['headers'][field.name] = field.value
+ end
+ hash['delivery_handler'] = delivery_handler.to_s if delivery_handler
+ hash['transport_encoding'] = transport_encoding.to_s
+ special_variables = [:@header, :@delivery_handler, :@transport_encoding]
+ (instance_variables - special_variables).each do |var|
+ hash[var.to_s] = instance_variable_get(var)
end
- hash['subject'] = subject
- hash['body'] = body.encoded(content_transfer_encoding)
- hash.to_yaml
+ hash.to_yaml(opts)
end
def self.from_yaml(str)
- from_hash(YAML::load(str))
+ hash = YAML.load(str)
+ m = Mail::Message.new(:headers => hash['headers'])
+ hash.delete('headers')
+ hash.each do |k,v|
+ case
+ when k == 'delivery_handler'
+ begin
+ m.delivery_handler = Object.const_get(v) unless v.blank?
+ rescue NameError
+ end
+ when k == 'transport_encoding'
+ m.transport_encoding(v)
+ when k =~ /^@/
+ m.instance_variable_set(k.to_sym, v)
+ end
+ end
+ m
end
def self.from_hash(hash)
@@ -116,24 +116,57 @@ def basic_email
mail.in_reply_to.should be_blank
end
- it "should allow serializing to YAML" do
- yaml = Mail::Message.new(:to => 'someone@somewhere.com', :cc => 'someoneelse@somewhere.com', :bcc => 'someonesecret@somewhere.com', :body => 'body', :subject => 'subject').to_yaml
- yaml_output = YAML.load(yaml)
- yaml_output['Mime-Version'].should == "1.0"
- yaml_output['Cc'].should == "someoneelse@somewhere.com"
- yaml_output['Subject'].should == "subject"
- yaml_output['body'].should == "body"
- yaml_output['Bcc'].should == "someonesecret@somewhere.com"
- yaml_output['Content-Transfer-Encoding'].should == "7bit"
- yaml_output['Message-ID'].should =~ /<.+@.+>/
- yaml_output['Content-Type'].should == "text/plain"
- yaml_output['To'].should == "someone@somewhere.com"
- yaml_output['Date'].should_not == ''
- end
-
- it "should deserialize after serializing" do
- message = Mail::Message.new(:to => 'someone@somewhere.com', :cc => 'someoneelse@somewhere.com', :bcc => 'someonesecret@somewhere.com', :body => 'body', :subject => 'subject')
- Mail::Message.from_yaml(message.to_yaml).should == message
+ describe "YAML serialization" do
+ before(:each) do
+ @yaml_mail = Mail::Message.new(:to => 'someone@somewhere.com',
+ :cc => 'someoneelse@somewhere.com',
+ :bcc => 'someonesecret@somewhere.com',
+ :body => 'body',
+ :subject => 'subject')
+ @smtp_settings = { :address=>"smtp.somewhere.net",
+ :port=>"587", :domain=>"somewhere.net", :user_name=>"someone@somewhere.net",
+ :password=>"password", :authentication=>:plain, :enable_starttls_auto => true,
+ :openssl_verify_mode => nil }
+ @yaml_mail.delivery_method :smtp, @smtp_settings
+ end
+
+ it "should serialize the basic information to YAML" do
+ yaml = @yaml_mail.to_yaml
+ yaml_output = YAML.load(yaml)
+ yaml_output['headers']['To'].should == "someone@somewhere.com"
+ yaml_output['headers']['Cc'].should == "someoneelse@somewhere.com"
+ yaml_output['headers']['Subject'].should == "subject"
+ yaml_output['headers']['Bcc'].should == "someonesecret@somewhere.com"
+ yaml_output['@body_raw'].should == "body"
+ yaml_output['@delivery_method'].should_not be_blank
+ end
+
+ it "should deserialize after serializing" do
+ deserialized = Mail::Message.from_yaml(@yaml_mail.to_yaml)
+ deserialized.should == @yaml_mail
+ deserialized.delivery_method.settings.should == @smtp_settings
+ end
+
+ it "should serialize a Message with a custom delivery_handler" do
+ @yaml_mail.delivery_handler = DeliveryAgent
+ yaml = @yaml_mail.to_yaml
+ yaml_output = YAML.load(yaml)
+ yaml_output['delivery_handler'].should == "DeliveryAgent"
+ end
+
+ it "should load a serialized delivery handler" do
+ @yaml_mail.delivery_handler = DeliveryAgent
+ deserialized = Mail::Message.from_yaml(@yaml_mail.to_yaml)
+ deserialized.delivery_handler.should == DeliveryAgent
+ end
+
+ it "should not deserialize a delivery_handler that does not exist" do
+ yaml = @yaml_mail.to_yaml
+ yaml_hash = YAML.load(yaml)
+ yaml_hash['delivery_handler'] = "NotARealClass"
+ deserialized = Mail::Message.from_yaml(yaml_hash.to_yaml)
+ deserialized.delivery_handler.should be_nil
+ end
end
end

0 comments on commit ebebdd4

Please sign in to comment.