lib/Triggermail.pm

filter: Statementstmt 56.4% 79 / 140 Branchbran 21.8% 7 / 32 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 65.3% 17 / 26 Podpod 90.0% 9 / 10 totaltotal 51.8% 113 / 218 CC 19 SCAR 18.8
CC
19
Cov
49%
setEmail
20.0
_flatten_hash
20.0
send
12.0
CRAP 6.6
↑ Summary
1 package Triggermail;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '1.005';
7  
8 1 ▶use constant API_URI => 'https://api.sailthru.com';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Digest::MD5 qw( md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Params::Validate qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
19 1 my $class = shift;
20 1 my $self = {
21 api_key => shift,
22 secret => shift,
23 timeout => shift,
24 };
25 1 warnings::warnif( 'deprecated', 'The module Triggermail is now deprecated. Use Sailthru::Client instead.' );
26 1 return bless $self, $class;
27 }
28  
29 1 ▶sub getEmail {
Subroutine getEmail: called CC=1 SCAR=0.0
Pod documented
30 1 validate_pos( @_, { type => HASHREF }, { type => SCALAR } );
31 1 my ( $self, $email ) = @_;
32 1 my %data = ( email => $email );
33 1 return $self->_apiCall( 'email', \%data, 'GET' );
34 }
35  
36 0 ▶sub setEmail {
Subroutine setEmail: not called CC=4 SCAR=30.0
Pod documented
37 0 validate_pos( @_, { type => HASHREF }, { type => SCALAR }, 0, 0, 0 );
38 0 my ( $self, $email, $vars_ref, $lists_ref, $templates_ref ) = @_;
39 0 my %data;
40 0 $data{'email'} = $email;
41 0 ▶ $self->_flatten_hash( 'vars', $vars_ref, \%data ) if $vars_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $vars_ref 0 0 0
42 0 ▶ $self->_flatten_hash( 'lists', $lists_ref, \%data ) if $lists_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $lists_ref 0 0 0
43 0 ▶ $self->_flatten_hash( 'templates', $templates_ref, \%data )
Branch: 1 branch
BranchTrueFalseTotal
if $templates_ref 0 0 0
44 if $templates_ref;
45 0 return $self->_apiCall( 'email', \%data, 'POST' );
46 }
47  
48 0 ▶sub send {
Subroutine send: not called CC=3 SCAR=24.8
Pod documented
49 0 validate_pos( @_, { type => HASHREF }, { type => SCALAR }, { type => SCALAR }, 0, 0, 0 );
50 0 my %data;
51 0 my ( $self, $template, $email, $vars_hash, $options_hash, $schedule_time ) = @_;
52 0 $data{'template'} = $template;
53 0 $data{'email'} = $email;
54 0 $data{'schedule_time'} = $schedule_time;
55 0 ▶ $self->_flatten_hash( 'vars', $vars_hash, \%data ) if $vars_hash;
Branch: 1 branch
BranchTrueFalseTotal
if $vars_hash 0 0 0
56 0 ▶ $self->_flatten_hash( 'options', $options_hash, \%data ) if $options_hash;
Branch: 1 branch
BranchTrueFalseTotal
if $options_hash 0 0 0
57 0 return $self->_apiCall( 'send', \%data, 'POST' );
58 }
59  
60 0 ▶sub getSend {
Subroutine getSend: not called CC=1 SCAR=6.9
Pod documented
61 0 validate_pos( @_, { type => HASHREF }, { type => SCALAR } );
62 0 my ( $self, $send_id ) = @_;
63 0 my %data = ( send_id => $send_id );
64 0 return $self->_apiCall( 'send', \%data, 'GET' );
65 }
66  
67 0 ▶sub scheduleBlast {
Subroutine scheduleBlast: not called CC=2 SCAR=17.9
Pod documented
68 0 validate_pos(
69 @_,
70 { type => HASHREF },
71 { type => SCALAR },
72 { type => SCALAR },
73 { type => SCALAR },
74 { type => SCALAR },
75 { type => SCALAR },
76 { type => SCALAR },
77 { type => SCALAR },
78 { type => SCALAR },
79 0
80 );
81 0 my ( $self, $name, $list, $schedule_time, $from_name, $from_email, $subject, $content_html, $content_text,
82 $options ) = @_;
83 0 my %data = (
84 name => $name,
85 list => $list,
86 schedule_time => $schedule_time,
87 from_name => $from_name,
88 from_email => $from_email,
89 subject => $subject,
90 content_html => $content_html,
91 content_text => $content_text
92 );
93 0 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) 0 0 0
94 0 my %merged_hash = ( %data, %{$options} ); #merge in the options hash
95 0 %data = %merged_hash;
96 }
97 0 return $self->_apiCall( 'blast', \%data, 'POST' );
98 }
99  
100 0 ▶sub getBlast {
Subroutine getBlast: not called CC=1 SCAR=6.9
Pod documented
101 0 validate_pos( @_, { type => HASHREF }, { type => SCALAR } );
102 0 my ( $self, $blast_id ) = @_;
103 0 my %data = ( blast_id => $blast_id );
104 0 return $self->_apiCall( 'blast', \%data, 'GET' );
105 }
106  
107 0 ▶sub copyTemplate {
Subroutine copyTemplate: not called CC=2 SCAR=17.9
Pod documented
108 0 validate_pos(
109 @_,
110 { type => HASHREF },
111 { type => SCALAR },
112 { type => SCALAR },
113 { type => SCALAR },
114 { type => SCALAR },
115 { type => SCALAR },
116 { type => SCALAR },
117 0
118 );
119 0 my ( $self, $template, $data_feed, $setup, $subject_line, $schedule_time, $list, $options ) = @_;
120 0 my %data = (
121 copy_template => $template,
122 data_feed_url => $data_feed,
123 setup => $setup,
124 name => $subject_line,
125 schedule_time => $schedule_time,
126 list => $list,
127 );
128 # $self->_flatten_hash( 'options', $options, \%data ) if $options;
129 0 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) 0 0 0
130 # merge in the options hash
131 0 my %merged_hash = ( %data, %{$options} );
132 0 %data = %merged_hash;
133 }
134 0 return $self->_apiCall( 'blast', \%data, 'POST' );
135 }
136  
137 0 ▶sub getTemplate {
Subroutine getTemplate: not called CC=1 SCAR=6.9
Pod documented
138 0 validate_pos( @_, { type => HASHREF }, { type => SCALAR } );
139 0 my ( $self, $template ) = @_;
140 0 my %data = ( template => $template );
141 0 return $self->_apiCall( 'template', \%data, 'GET' );
142 }
143  
144 0 ▶sub importContacts {
Subroutine importContacts: not called CC=2 SCAR=17.9
Pod documented
145 0 validate_pos( @_, { type => HASHREF }, { type => SCALAR }, 0 );
146 0 my ( $self, $email, $password, $include_names ) = @_;
147 0 ▶ $include_names = 0 if ( !$include_names );
Branch: 1 branch
BranchTrueFalseTotal
unless $include_names 0 0 0
148 0 my %data = (
149 email => $email,
150 password => $password,
151 include_names => $include_names
152 );
153 0 return $self->_apiCall( 'contacts', \%data, 'POST' );
154 }
155  
156 1 ▶sub _apiCall {
Subroutine _apiCall: called CC=2 SCAR=6.9
157 1 validate_pos( @_, { type => HASHREF }, { type => SCALAR }, { type => HASHREF }, { type => SCALAR } );
158 1 my ( $self, $action, $data, $method ) = @_;
159 1 $data->{'api_key'} = $self->{api_key};
160 1 $data->{'format'} = 'json';
161 1 $data->{'sig'} = $self->_getSignatureHash($data);
162 1 my $result = $self->_httpRequest( API_URI . "/" . $action, $data, $method );
163  
164 1 my $json = JSON::XS->new->ascii->pretty->allow_nonref;
165 1 my $decoded = $json->decode( $result->content );
166 1 ▶ return $decoded ? $decoded : $result;
Branch: 1 branch
BranchTrueFalseTotal
$decoded ? : 1 0 1
167 }
168  
169 1 ▶sub _httpRequest {
Subroutine _httpRequest: called CC=4 SCAR=14.3
170 1 validate_pos( @_, { type => HASHREF }, { type => SCALAR }, { type => HASHREF }, { type => SCALAR } );
171 1 my ( $self, $url, $data, $method ) = @_;
172 1 my $browser = LWP::UserAgent->new;
173 1 ▶ $browser->timeout( $self->{timeout} ) if $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"timeout"} 0 1 1
174 1 my $response;
175 1 ▶ if ( $method eq 'POST' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'POST') { } 0 1 1
176 0 $response = $browser->post( $url, $data );
177 }
178 else { #GET
179 1 ▶ use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
180 1 $url = URI->new($url);
181 1 $url->query_form( %{$data} );
182 1 $response = $browser->get($url);
183 }
184 1 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 1 0 1
185 1 return $response;
186 }
187 0 return;
188 }
189  
190 2 ▶sub _getSignatureHash {
Subroutine _getSignatureHash: called CC=1 SCAR=0.0
191 2 validate_pos( @_, { type => HASHREF }, { type => HASHREF } );
192 2 my ( $self, $params ) = @_;
193 2 my @values;
194 2 $self->_extractValues( $params, \@values );
195 2 @values = sort @values;
196 2 my $string = $self->{secret} . join( '', @values );
197 2 return md5_hex($string);
198 }
199  
200 0 ▶sub _flatten_hash {
Subroutine _flatten_hash: not called CC=4 SCAR=30.0
201 0 validate_pos( @_, { type => HASHREF }, { type => SCALAR }, { type => HASHREF }, { type => HASHREF } );
202 0 my ( $self, $name, $nested_hash, $mother_hash ) = @_;
203 0 ▶ while ( ( my $key, my $value ) = each %{$nested_hash} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$nested_hash;}) 0 0 0
204 0 ▶ if ( ref( $nested_hash->{$key} ) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $nested_hash->{$key} eq 'HASH' or ref $nested_hash->{$key} eq 'REF') { } 0 0 0
Condition0%0/3
ref $nested_hash->{$key} eq "HASH" or ref $nested_hash->{$key} eq "REF"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $nested_hash->{$key} eq "HASH" or ref $nested_hash->{$key} eq "REF"
ref $nested_hash->{$key} eq "HASH" ref $nested_hash->{$key} eq "REF"
Truth table0%0/3
A or B
A= ref $nested_hash->{$key} eq "HASH"B= ref $nested_hash->{$key} eq "REF"
ABresult
1X1
011
000
205 || ref( $nested_hash->{$key} ) eq 'REF' ) {
206 0 $self->_flatten_hash( $key, $nested_hash->{$key}, $mother_hash );
207 }
208 else {
209 0 $mother_hash->{ $name . "[" . $key . "]" } = $value;
210 }
211 }
212 0 return;
213 }
214  
215 2 ▶sub _extractValues {
Subroutine _extractValues: called CC=4 SCAR=14.5
216 2 validate_pos( @_, { type => HASHREF }, { type => HASHREF }, { type => ARRAYREF } );
217 2 my ( $self, $hash, $array ) = @_;
218 2 ▶ while ( ( my $key, my $value ) = each %{$hash} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$hash;}) 4 2 6
219 4 ▶ if ( ref($value) eq 'HASH' || ref($value) eq 'REF' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'HASH' or ref $value eq 'REF') { } 0 4 4
Condition33%1/3
ref $value eq "HASH" or ref $value eq "REF"
l!l&&r!l&&!r
004
MC/DC0%0/2
ref $value eq "HASH" or ref $value eq "REF"
ref $value eq "HASH" ref $value eq "REF"
Truth table33%1/3
A or B
A= ref $value eq "HASH"B= ref $value eq "REF"
ABresult
1X1
011
000
220 0 $self->_extractValues( $value, $array );
221 }
222 else {
223 4 push @{$array}, $value;
224 }
225 }
226 2 return;
227 }
228  
229 1 1;
230 __END__
↑ Summary