Skip to content

Commit 9dd0140

Browse files
committed
Implement tags
1 parent b75ea6d commit 9dd0140

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

lib/WWW/Mailgun.pm

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ sub new {
3030
url => $Url . '/',
3131
domain => $Domain,
3232
from => $From,
33-
3433
};
3534

3635
$self->{get} = sub {
@@ -60,35 +59,41 @@ sub _handle_response {
6059

6160
my $rc = $response->code;
6261

63-
return 1 if $rc == 200;
62+
return 1 if $rc == 200;
63+
64+
my $json = from_json($response->decoded_content);
65+
if ($json->{message}) {
66+
die $response->status_line." ".$json->{message};
67+
}
6468

6569
die "Bad Request - Often missing a required parameter" if $rc == 400;
6670
die "Unauthorized - No valid API key provided" if $rc == 401;
6771
die "Request Failed - Parameters were valid but request failed" if $rc == 402;
6872
die "Not Found - The requested item doesn’t exist" if $rc == 404;
6973
die "Server Errors - something is wrong on Mailgun’s end" if $rc >= 500;
70-
7174
}
7275

7376
sub send {
7477
my ($self, $msg) = @_;
7578

76-
$msg->{from} = $msg->{from} // $self->{from};
77-
$msg->{to} = $msg->{to} // die "You must specify an email address to send to";
79+
$msg->{from} = $msg->{from} || $self->{from}
80+
or die "You must specify an email address to send from";
81+
$msg->{to} = $msg->{to}
82+
or die "You must specify an email address to send to";
7883
if (ref $msg->{to} eq 'ARRAY') {
7984
$msg->{to} = join(',',@{$msg->{to}});
8085
}
8186

8287
$msg->{subject} = $msg->{subject} // "";
8388
$msg->{text} = $msg->{text} // "";
8489

85-
my $attachments = delete $msg->{attachments};
86-
my $content = [%$msg];
87-
if ( $attachments && ref $attachments eq 'ARRAY' ) {
88-
for my $a ( @$attachments ) {
89-
push @$content, attachment => $a;
90-
}
91-
}
90+
my $content = _prepare_content(
91+
$msg,
92+
{
93+
attachments => 'attachment',
94+
tags => 'o:tag',
95+
},
96+
);
9297

9398
my $r = $self->{post}->($self, 'messages', $content);
9499

@@ -97,6 +102,32 @@ sub send {
97102
return from_json($r->decoded_content);
98103
}
99104

105+
sub _prepare_content {
106+
my ($msg, $msg_key__content_key) = @_;
107+
108+
my $content = [];
109+
110+
while ( my ( $msg_key, $content_key ) = each %$msg_key__content_key ) {
111+
my $extra_content = _get_extra_content($msg, $msg_key, $content_key);
112+
push @$content, @$extra_content;
113+
}
114+
115+
push @$content, %$msg;
116+
117+
return $content;
118+
}
119+
120+
sub _get_extra_content {
121+
my ($msg, $msg_key, $content_key) = @_;
122+
123+
my $array = delete $msg->{$msg_key} || [];
124+
125+
return [
126+
map { $content_key => $_ }
127+
@$array
128+
];
129+
}
130+
100131
sub _get_route {
101132
my ($self, $path) = @_;
102133

@@ -114,7 +145,7 @@ sub _get_route {
114145
sub unsubscribes {
115146
my ($self, $method, $data) = @_;
116147
$method = $method // 'get';
117-
148+
118149
my $r = $self->{lc($method)}->($self,'unsubscribes',$data);
119150
_handle_response($r);
120151
return from_json($r->decoded_content);

t/send.t

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ my $msg = {
1414
text => "MailGun is a set of powerful APIs that enable you to ".
1515
"send, receive and track email effortlessly.",
1616
attachments => [ 'hello.txt', 'world.xml' ],
17+
tags => [ 'perl', 'mailgun' ],
1718
};
1819

1920
my $ua = new Test::MockModule('LWP::UserAgent');
@@ -37,29 +38,28 @@ $ua->mock(post => sub {
3738
while ( @content ) {
3839
my $key = shift @content;
3940
my $value = shift @content;
40-
$hash->{$key} ||= [];
41-
push @{$hash->{$key}}, $value;
41+
if ($hash->{$key}) {
42+
$hash->{$key} = [$hash->{$key}];
43+
push @{$hash->{$key}}, $value;
44+
}
45+
else {
46+
$hash->{$key} = $value;
47+
}
4248
}
4349

4450
is_deeply(
51+
$hash,
4552
{
46-
from => delete($hash->{from})->[0],
47-
to => delete($hash->{to})->[0],
48-
subject => delete($hash->{subject})->[0],
49-
text => delete($hash->{text})->[0],
53+
'from' => $msg->{from},
54+
'to' => $msg->{to},
55+
'subject' => $msg->{subject},
56+
'text' => $msg->{text},
57+
'attachment' => [ 'hello.txt', 'world.xml' ],
58+
'o:tag' => [ 'perl', 'mailgun' ],
5059
},
51-
$msg,
52-
"Standard fields are correct",
60+
"Content is correct",
5361
);
5462

55-
cmp_bag(
56-
delete $hash->{attachment},
57-
[ 'hello.txt', 'world.xml' ],
58-
"Attachments are correct",
59-
);
60-
61-
is_deeply($hash, {}, "All items accounted for");
62-
6363
return HTTP::Response->new(200, "OK", [], to_json({}));
6464
});
6565

0 commit comments

Comments
 (0)