Permalink
Browse files

Add create method

  • Loading branch information...
1 parent a5eced8 commit e255d7e9c8c86903863d99a149605db66592f9e8 @retupmoca retupmoca committed Feb 14, 2012
Showing with 23 additions and 7 deletions.
  1. +1 −3 README
  2. +8 −3 lib/Email/Simple.pm6
  3. +14 −1 lib/Email/Simple/Header.pm6
View
4 README
@@ -10,7 +10,5 @@ $mail.header-pairs now returns an array of arrays instead of a flat array.
TODO
----
* write more tests
-* implement the following functions:
-** Email::Simple
-*** create
+* Email::Simple.create doesn't automatically create a Date header yet
* Allow for user to specify their own header class
View
@@ -23,14 +23,19 @@ grammar Message {
}
}
-method new (Str $text) {
+multi method new (Str $text) {
my $parsed = Message.parse($text);
my $header-object = Email::Simple::Header.new(~$parsed<headers>, crlf => ~$parsed<newline>);
self.bless(*, body => $parsed<body>, header => $header-object, crlf => ~$parsed<newline>);
}
-method create (:$header, :$body) {
- die "Stub!";
+multi method new (Array $header, Str $body) {
+ self.create(header => $header, body => $body);
+}
+
+method create (Array :$header, Str :$body) {
+ my $header-object = Email::Simple::Header.new($header, crlf => "\r\n");
+ self.bless(*, body => $body, header => $header-object, crlf => "\r\n");
}
submethod BUILD (:$!body, :$!header, :$!crlf) { }
@@ -22,7 +22,20 @@ grammar Headers {
}
}
-method new ($header-text, :$crlf = "\r\n") {
+multi method new (Array @headers, Str :$crlf = "\r\n") {
+ if @headers[0] ~~ Array {
+ self.bless(*, crlf => $crlf, headers => @headers);
+ } else {
+ my @folded-headers;
+ loop (my $x=0;$x < +@headers;$x+=2) {
+ @folded-headers.push([@headers[$x], @headers[$x+1]]);
+ }
+
+ self.bless(*, crlf => $crlf, headers => @folded-headers);
+ }
+}
+
+multi method new (Str $header-text, Str :$crlf = "\r\n") {
my $parsed = Headers.parse($header-text);
my @entries = $parsed<entry>;
my @headers;

0 comments on commit e255d7e

Please sign in to comment.