| 1 | package Tilt::Email::BlueHornet; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # VERSION | ||||||||||||||
| 4 | # ABSTRACT: Client for sending email using BlueHornet (http://www.bluehornet.com/) | ||||||||||||||
| 5 | |||||||||||||||
| 6 | 1 | ▶ | use Encode qw(encode_utf8); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use Function::Parameters ':strict'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use URI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use XML::Simple; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | has 'api_key' => ( | ||||||||||||||
| 14 | is => 'ro', | ||||||||||||||
| 15 | isa => 'Str', | ||||||||||||||
| 16 | required => 1, | ||||||||||||||
| 17 | ); | ||||||||||||||
| 18 | |||||||||||||||
| 19 | has 'api_secret' => ( | ||||||||||||||
| 20 | is => 'ro', | ||||||||||||||
| 21 | isa => 'Str', | ||||||||||||||
| 22 | required => 1, | ||||||||||||||
| 23 | ); | ||||||||||||||
| 24 | |||||||||||||||
| 25 | has 'ua' => ( | ||||||||||||||
| 26 | is => 'ro', | ||||||||||||||
| 27 | default => sub { | ||||||||||||||
| 28 | return LWP::UserAgent->new(); | ||||||||||||||
| 29 | } | ||||||||||||||
| 30 | ); | ||||||||||||||
| 31 | |||||||||||||||
| 32 | # TODO Replace XML::Simple with a better library | ||||||||||||||
| 33 | has '_xml' => ( | ||||||||||||||
| 34 | is => 'ro', | ||||||||||||||
| 35 | default => sub { | ||||||||||||||
| 36 | return XML::Simple->new( | ||||||||||||||
| 37 | NoAttr => 1, | ||||||||||||||
| 38 | RootName => 'api', | ||||||||||||||
| 39 | # XML::Simple escaping of HTML documents weren't accepted by BlueHornet | ||||||||||||||
| 40 | NoEscape => 1, | ||||||||||||||
| 41 | KeyAttr => [] | ||||||||||||||
| 42 | ); | ||||||||||||||
| 43 | } | ||||||||||||||
| 44 | ); | ||||||||||||||
| 45 | |||||||||||||||
| 46 | 0 | ▶ | method _call($data) { | ||||||||||||
|
Subroutine
_call: not called CC=5 SCAR=34.0
Branch: 2 branches
| |||||||||||||||
| 47 | 0 | my $xml_data = { | |||||||||||||
| 48 | authentication => { | ||||||||||||||
| 49 | api_key => $self->api_key, | ||||||||||||||
| 50 | shared_secret => $self->api_secret, | ||||||||||||||
| 51 | response_type => 'xml', | ||||||||||||||
| 52 | no_halt => 0 | ||||||||||||||
| 53 | }, | ||||||||||||||
| 54 | data => { | ||||||||||||||
| 55 | methodCall => $data | ||||||||||||||
| 56 | } | ||||||||||||||
| 57 | }; | ||||||||||||||
| 58 | |||||||||||||||
| 59 | 0 | my $response = $self->ua->post( | |||||||||||||
| 60 | 'https://echo3.bluehornet.com/api/xmlrpc/index.php', | ||||||||||||||
| 61 | Content => $self->_xml->XMLout($xml_data), | ||||||||||||||
| 62 | 'Content-type' => "application/xml; charset='utf8'" | ||||||||||||||
| 63 | ); | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 0 | ▶ | die 'BlueHornet API call failed with status ' . $response->code . ' ' . | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | $response->decoded_content | ||||||||||||||
| 67 | unless $response->is_success; | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 0 | my $xml_response = $self->_xml->XMLin( | |||||||||||||
| 70 | $response->decoded_content, | ||||||||||||||
| 71 | KeyAttr => {}, | ||||||||||||||
| 72 | ForceArray => [] | ||||||||||||||
| 73 | ); | ||||||||||||||
| 74 | |||||||||||||||
| 75 | die 'BlueHornet API call failed with error ' . | ||||||||||||||
| 76 | $xml_response->{'item'}->{'responseText'} | ||||||||||||||
| 77 | 0 | ▶ | if exists($xml_response->{'item'}->{'error'}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | |||||||||||||||
| 79 | 0 | return $xml_response; | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | method rebuild_template(Str :$template_id) { | ||||||||||||||
| 83 | return $self->_call({ | ||||||||||||||
| 84 | methodName => 'transactional.rebuildTemplate', | ||||||||||||||
| 85 | template_id => $template_id | ||||||||||||||
| 86 | }); | ||||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | method send_test(Str :$template_id, Str :$email, %variables) { | ||||||||||||||
| 90 | return $self->_call({ | ||||||||||||||
| 91 | email => $email, | ||||||||||||||
| 92 | methodName => 'transactional.sendTest', | ||||||||||||||
| 93 | template_id => $template_id, | ||||||||||||||
| 94 | %variables | ||||||||||||||
| 95 | }); | ||||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | method send_transaction(Str :$template_id, Str :$email, %variables) { | ||||||||||||||
| 99 | return $self->_call({ | ||||||||||||||
| 100 | email => $email, | ||||||||||||||
| 101 | methodName => 'transactional.sendTransaction', | ||||||||||||||
| 102 | template_id => $template_id, | ||||||||||||||
| 103 | %variables | ||||||||||||||
| 104 | }); | ||||||||||||||
| 105 | } | ||||||||||||||
| 106 | |||||||||||||||
| 107 | method update_template( | ||||||||||||||
| 108 | Str :$template_id, Str :$subject, Str :$html, Str :$plain_text | ||||||||||||||
| 109 | ) { | ||||||||||||||
| 110 | return $self->_call({ | ||||||||||||||
| 111 | methodName => 'transactional.updatetemplate', | ||||||||||||||
| 112 | template_id => $template_id, | ||||||||||||||
| 113 | # NOTE: This is crazy as we are encoding a UTF8 string. But it works. Really | ||||||||||||||
| 114 | # need to understand why XML::Simple and the API call likes double encoded | ||||||||||||||
| 115 | # strings. Apparently template toolkit output is already double encoded so | ||||||||||||||
| 116 | # we don't need to worry about the $htmL_email and $plain_email content. | ||||||||||||||
| 117 | subject => "<![CDATA[" . encode_utf8($subject) . "]]>", | ||||||||||||||
| 118 | template_data => { | ||||||||||||||
| 119 | html => "<![CDATA[$html]]>", | ||||||||||||||
| 120 | plain => "<![CDATA[$plain_text]]>", | ||||||||||||||
| 121 | } | ||||||||||||||
| 122 | }); | ||||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 1; | ||||||||||||||