lib/Sailthru/Client.pm

filter: Statementstmt 93.7% 180 / 192 Branchbran 62.5% 15 / 24 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 97.0% 33 / 34 Podpod 61.9% 13 / 21 totaltotal 85.2% 243 / 285 CC 16 SCAR 10.6
CC
16
Cov
88%
get_last_rate_limit_info
12.0
_http_request
5.7
_api_request
4.7
CRAP 2.9
↑ Summary
1 package Sailthru::Client;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Digest::MD5 qw( md5_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Params::Validate qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 our $VERSION = '2.3.0';
15 5 Readonly my $API_URI => 'https://api.sailthru.com/';
16  
17 #
18 # public api
19 #
20  
21 # args:
22 #
23 # * api_key - scalar
24 # * secret - scalar
25 # * timeout - scalar (optional)
26 5 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
27 5 my ( $class, $api_key, $secret, $timeout ) = @_;
28 5 my $self = {
29 api_key => $api_key,
30 secret => $secret,
31 ua => LWP::UserAgent->new,
32 last_rate_limit_info_ref => {}
33 };
34 5 ▶ $self->{ua}->timeout($timeout) if $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout 0 5 5
35 5 $self->{ua}->default_header( 'User-Agent' => "Sailthru API Perl Client $VERSION" );
36 5 return bless $self, $class;
37 }
38  
39 # args:
40 #
41 # * template_name - scalar
42 # * email - scalar
43 # * vars - hashref (optional)
44 # * options - hashref (optional)
45 # * schedule_time - scalar (optional)
46 2 ▶sub send {
Subroutine send: called CC=4 SCAR=13.9
Pod documented
47 2 my $self = shift;
48 2 my @params = validate_pos(
49 @_,
50 { type => SCALAR },
51 { type => SCALAR },
52 { type => HASHREF, default => {} },
53 { type => HASHREF, default => {} },
54 { type => SCALAR, default => undef }
55 );
56 2 my ( $template_name, $email, $vars, $options, $schedule_time ) = @params;
57 2 my $data = {};
58 2 $data->{template} = $template_name;
59 2 $data->{email} = $email;
60 2 ▶ $data->{vars} = $vars if keys %{$vars};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$vars;} 1 1 2
61 2 ▶ $data->{options} = $options if keys %{$options};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$options;} 1 1 2
62 2 ▶ $data->{schedule_time} = $schedule_time if $schedule_time;
Branch: 1 branch
BranchTrueFalseTotal
if $schedule_time 1 1 2
63 2 return $self->api_post( 'send', $data );
64 }
65  
66 # args:
67 # * send_id - scalar
68 2 ▶sub get_send {
Subroutine get_send: called CC=1 SCAR=0.0
Pod documented
69 2 my $self = shift;
70 2 my @params = validate_pos( @_, { type => SCALAR } );
71 2 my ($send_id) = @params;
72 2 return $self->api_get( 'send', { send_id => $send_id } );
73 }
74  
75 # args:
76 # * email - scalar
77 3 ▶sub get_email {
Subroutine get_email: called CC=1 SCAR=0.0
Pod documented
78 3 my $self = shift;
79 3 my @params = validate_pos( @_, { type => SCALAR } );
80 3 my ($email) = @params;
81 3 return $self->api_get( 'email', { email => $email } );
82 }
83  
84 # args:
85 # * email - scalar
86 # * vars - hashref (optional)
87 # * lists - hashref (optional)
88 # * templates - hashref (optional)
89 3 ▶sub set_email {
Subroutine set_email: called CC=4 SCAR=13.9
Pod documented
90 3 my $self = shift;
91 3 my @params = validate_pos(
92 @_,
93 { type => SCALAR },
94 { type => HASHREF, default => {} },
95 { type => HASHREF, default => {} },
96 { type => HASHREF, default => {} }
97 );
98 3 my ( $email, $vars, $lists, $templates ) = @params;
99 3 my $data = {};
100 3 $data->{email} = $email;
101 3 ▶ $data->{vars} = $vars if keys %{$vars};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$vars;} 2 1 3
102 3 ▶ $data->{lists} = $lists if keys %{$lists};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$lists;} 2 1 3
103 3 ▶ $data->{templates} = $templates if keys %{$templates};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$templates;} 2 1 3
104 3 return $self->api_post( 'email', $data );
105 }
106  
107 # args:
108 # * name - scalar
109 # * list - scalar
110 # * schedule_time - scalar
111 # * from_name - scalar
112 # * from_email - scalar
113 # * subject - scalar
114 # * content_html - scalar
115 # * content_text - scalar
116 # * options - hashref (optional)
117 3 ▶sub schedule_blast {
Subroutine schedule_blast: called CC=1 SCAR=0.0
Pod documented
118 3 my $self = shift;
119 3 my @params = validate_pos(
120 @_,
121 { type => SCALAR },
122 { type => SCALAR },
123 { type => SCALAR },
124 { type => SCALAR },
125 { type => SCALAR },
126 { type => SCALAR },
127 { type => SCALAR },
128 { type => SCALAR },
129 { type => HASHREF, default => {} }
130 );
131 3 my ( $name, $list, $schedule_time, $from_name, $from_email, $subject, $content_html, $content_text, $options ) =
132 @params;
133 # initialize our data hash by copying the contents of the options hash
134 3 my $data = { %{$options} };
135 3 $data->{name} = $name;
136 3 $data->{list} = $list;
137 3 $data->{schedule_time} = $schedule_time;
138 3 $data->{from_name} = $from_name;
139 3 $data->{from_email} = $from_email;
140 3 $data->{subject} = $subject;
141 3 $data->{content_html} = $content_html;
142 3 $data->{content_text} = $content_text;
143 3 return $self->api_post( 'blast', $data );
144 }
145  
146 # args:
147 # * template_name - scalar
148 # * list - scalar
149 # * schedule_time - scalar
150 # * options - hashref (optional)
151 2 ▶sub schedule_blast_from_template {
Subroutine schedule_blast_from_template: called CC=1 SCAR=0.0
Pod documented
152 2 my $self = shift;
153 2 my @params = validate_pos(
154 @_,
155 { type => SCALAR },
156 { type => SCALAR },
157 { type => SCALAR },
158 { type => HASHREF, default => {} },
159 );
160 2 my ( $template_name, $list, $schedule_time, $options ) = @params;
161 # initialize our data hash by copying the contents of the options hash
162 2 my $data = { %{$options} };
163 2 $data->{copy_template} = $template_name;
164 2 $data->{list} = $list;
165 2 $data->{schedule_time} = $schedule_time;
166 2 return $self->api_post( 'blast', $data );
167  
168 }
169  
170 # args:
171 # * blast_id - scalar
172 2 ▶sub get_blast {
Subroutine get_blast: called CC=1 SCAR=0.0
Pod documented
173 2 my $self = shift;
174 2 my @params = validate_pos( @_, { type => SCALAR } );
175 2 my ($blast_id) = @params;
176 2 return $self->api_get( 'blast', { blast_id => $blast_id } );
177 }
178  
179 # args:
180 # * template_name - scalar
181 2 ▶sub get_template {
Subroutine get_template: called CC=1 SCAR=0.0
Pod documented
182 2 my $self = shift;
183 2 my @params = validate_pos( @_, { type => SCALAR } );
184 2 my ($template_name) = @params;
185 2 return $self->api_get( 'template', { template => $template_name } );
186 }
187  
188 # args:
189 # * action - scalar
190 # * data - hashref
191 10 ▶sub api_get {
Subroutine api_get: called CC=1 SCAR=0.0
Pod documented
192 10 my $self = shift;
193 10 my @params = validate_pos( @_, { type => SCALAR }, { type => HASHREF } );
194 10 my ( $action, $data ) = @params;
195 10 return $self->_api_request( $action, $data, 'GET' );
196 }
197  
198 # args:
199 # * action - scalar
200 # * data - hashref
201 # * TODO: optional binary_key arg
202 15 ▶sub api_post {
Subroutine api_post: called CC=1 SCAR=0.0
Pod documented
203 15 my $self = shift;
204 15 my @params = validate_pos( @_, { type => SCALAR }, { type => HASHREF } );
205 15 my ( $action, $data ) = @params;
206 15 return $self->_api_request( $action, $data, 'POST' );
207 }
208  
209 # args:
210 # * action - scalar
211 # * data - hashref
212 1 ▶sub api_delete {
Subroutine api_delete: called CC=1 SCAR=0.0
Pod documented
213 1 my $self = shift;
214 1 my @params = validate_pos( @_, { type => SCALAR }, { type => HASHREF } );
215 1 my ( $action, $data ) = @params;
216 1 return $self->_api_request( $action, $data, 'DELETE' );
217 }
218  
219 # args:
220 #
221 # * action - scalar
222 # * method - scalar
223 0 ▶sub get_last_rate_limit_info {
Subroutine get_last_rate_limit_info: not called CC=3 SCAR=24.8
Pod documented
224 0 my $self = shift;
225 0 my @params = validate_pos(
226 @_,
227 { type => SCALAR },
228 { type => SCALAR }
229 );
230 0 my ( $action, $method ) = @params;
231 0 $method = uc($method);
232  
233 0 ▶ if (exists $self->{last_rate_limit_info_ref}->{$action} and $self->{last_rate_limit_info_ref}->{$action}->{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"last_rate_limit_info_ref"}{$action} and $self->{"last_rate_limit_info_ref"}{$action}{$method}) 0 0 0
Condition0%0/3
exists $self->{"last_rate_limit_info_ref"}{$action} and $self->{"last_rate_limit_info_ref"}{$action}{$method}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"last_rate_limit_info_ref"}{$action} and $self->{"last_rate_limit_info_ref"}{$action}{$method}
exists $self->{"last_rate_limit_info_ref"}{$action} $self->{"last_rate_limit_info_ref"}{$action}{$method}
Truth table0%0/3
A and B
A= exists $self->{"last_rate_limit_info_ref"}{$action}B= $self->{"last_rate_limit_info_ref"}{$action}{$method}
ABresult
0X0
100
111
234 0 return $self->{last_rate_limit_info_ref}->{$action}->{$method};
235 }
236  
237 0 return;
238 }
239  
240 #
241 # private helper methods
242 #
243  
244 # args:
245 # * action - scalar
246 # * data - hashref
247 # * request_type - scalar
248 4 ▶sub _api_request {
Subroutine _api_request: called CC=4 SCAR=15.5
249 4 my $self = shift;
250 4 my @params = validate_pos( @_, { type => SCALAR }, { type => HASHREF }, { type => SCALAR } );
251 4 my ( $action, $data, $request_type ) = @params;
252 4 my $payload = $self->_prepare_json_payload($data);
253 4 my $action_uri = $API_URI . $action;
254 4 my $response = $self->_http_request( $action_uri, $payload, $request_type );
255  
256 # update rate limit information
257 4 ▶ if ( defined $response->header('x-rate-limit-limit') and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response->header("x-rate-limit-limit") and defined $response->header("x-rate-limit-remaining") and defined $response->header("x-rate-limit-reset")) 0 4 4
Condition33%2/6
defined $response->header("x-rate-limit-limit") and defined $response->header("x-rate-limit-remaining") and defined $response->header("x-rate-limit-reset")
!ll&&!rl&&r
400
defined $response->header("x-rate-limit-limit") and defined $response->header("x-rate-limit-remaining")
!ll&&!rl&&r
400
MC/DC0%0/3
defined $response->header("x-rate-limit-limit") and defined $response->header("x-rate-limit-remaining") and defined $response->header("x-rate-limit-reset")
defined $response->header("x-rate-limit-limit") defined $response->header("x-rate-limit-remaining") defined $response->header("x-rate-limit-reset")
Truth table25%1/4
A and B and C
A= defined $response->header("x-rate-limit-limit")B= defined $response->header("x-rate-limit-remaining")C= defined $response->header("x-rate-limit-reset")
ABCresult
0XX0
10X0
1100
1111
258 defined $response->header('x-rate-limit-remaining') and
259 defined $response->header('x-rate-limit-reset') ) {
260 0 $self->{last_rate_limit_info_ref}->{$action}->{$request_type} = {
261 limit => $response->header('x-rate-limit-limit'),
262 remaining => $response->header('x-rate-limit-remaining'),
263 reset => $response->header('x-rate-limit-reset')
264 };
265 }
266  
267 4 return decode_json( $response->content );
268 }
269  
270 # args:
271 # * uri - scalar
272 # * data - hashref
273 # * method - scalar
274 1 ▶sub _http_request {
Subroutine _http_request: called CC=4 SCAR=17.4
275 1 my $self = shift;
276 1 my @params = validate_pos( @_, { type => SCALAR }, { type => HASHREF }, { type => SCALAR } );
277 1 my ( $uri, $data, $method ) = @params;
278 1 $uri = URI->new($uri);
279 1 my $response;
280 1 ▶ if ( $method eq 'GET' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($method eq 'GET') { } 1 0 1
elsif ($method eq 'POST') { } 0 0 0
elsif ($method eq 'DELETE') { } 0 0 0
281 1 $uri->query_form($data);
282 1 $response = $self->{ua}->get($uri);
283 }
284 elsif ( $method eq 'POST' ) {
285 0 $response = $self->{ua}->post( $uri, $data );
286 }
287 elsif ( $method eq 'DELETE' ) {
288 0 $uri->query_form($data);
289 0 $response = $self->{ua}->delete($uri);
290 }
291 else {
292 0 croak "Invalid method: $method";
293 }
294 1 return $response;
295 }
296  
297 # args:
298 # * data - hashref
299 4 ▶sub _prepare_json_payload {
Subroutine _prepare_json_payload: called CC=1 SCAR=0.0
300 4 my $self = shift;
301 4 my @params = validate_pos( @_, { type => HASHREF } );
302 4 my ($data) = @params;
303 4 my $payload = {};
304 4 $payload->{api_key} = $self->{api_key};
305 4 $payload->{format} = 'json';
306 # this gives us nice clean utf8 encoded json text
307 4 $payload->{json} = encode_json($data);
308 4 $payload->{sig} = $self->_get_signature_hash( $payload, $self->{secret} );
309 4 return $payload;
310 }
311  
312 # Every request must also generate a signature hash called sig according to the
313 # following rules:
314 #
315 # * take the string values of every parameter, including api_key
316 # * sort the values alphabetically, case-sensitively (i.e. ordered by Unicode code point)
317 # * concatenate the sorted values, and prepend this string with your shared secret
318 # * generate an MD5 hash of this string and use this as sig
319 # * now generate your URL-encoded query string from your parameters plus sig
320  
321 # args:
322 # * params - hashref
323 # * secret - scalar
324 # NOTE This internal method assumes a single level hash with values for only 'api_key', 'format', and 'json'
325 # NOTE Since we pack everything into the 'json' value this is safe and we do not need to recurse down a nested hash.
326 6 ▶sub _get_signature_hash {
Subroutine _get_signature_hash: called CC=1 SCAR=0.0
327 6 my $self = shift;
328 6 my @params = validate_pos( @_, { type => HASHREF }, { type => SCALAR } );
329 6 my ( $api_param_hash, $secret ) = @params;
330 6 my @api_param_values = values %{$api_param_hash};
331 6 my $sig_string = join '', $secret, sort @api_param_values;
332 # assumes utf8 encoded text, works fine because we use encode_json internally
333 6 return md5_hex($sig_string);
334 }
335  
336 ### XXX
337 ### DEPRECATED METHODS
338 ### XXX
339  
340 # args:
341 # * email - scalar
342 1 ▶sub getEmail {
Subroutine getEmail: called CC=1 SCAR=0.0
Pod undocumented
343 1 my $self = shift;
344 1 warnings::warnif( 'deprecated', 'getEmail is deprecated, use get_email instead' );
345 1 return $self->get_email(@_);
346 }
347  
348 # args:
349 # * email - scalar
350 # * vars - hashref (optional)
351 # * lists - hashref (optional)
352 # * templates - hashref (optional)
353 1 ▶sub setEmail {
Subroutine setEmail: called CC=1 SCAR=0.0
Pod undocumented
354 1 my $self = shift;
355 1 warnings::warnif( 'deprecated', 'setEmail is deprecated, use set_email instead' );
356 1 return $self->set_email(@_);
357 }
358  
359 # args:
360 # * send_id - scalar
361 1 ▶sub getSend {
Subroutine getSend: called CC=1 SCAR=0.0
Pod undocumented
362 1 my $self = shift;
363 1 warnings::warnif( 'deprecated', 'getSend is deprecated, use get_send instead' );
364 1 return $self->get_send(@_);
365 }
366  
367 # args:
368 # * name - scalar
369 # * list - scalar
370 # * schedule_time - scalar
371 # * from_name - scalar
372 # * from_email - scalar
373 # * subject - scalar
374 # * content_html - scalar
375 # * content_text - scalar
376 # * options - hashref (optional)
377 1 ▶sub scheduleBlast {
Subroutine scheduleBlast: called CC=1 SCAR=0.0
Pod undocumented
378 1 my $self = shift;
379 1 warnings::warnif( 'deprecated', 'scheduleBlast is deprecated, use schedule_blast instead' );
380 1 return $self->schedule_blast(@_);
381 }
382  
383 # args:
384 # * blast_id - scalar
385 1 ▶sub getBlast {
Subroutine getBlast: called CC=1 SCAR=0.0
Pod undocumented
386 1 my $self = shift;
387 1 warnings::warnif( 'deprecated', 'getBlast is deprecated, use get_blast instead' );
388 1 return $self->get_blast(@_);
389 }
390  
391 2 ▶sub copyTemplate {
Subroutine copyTemplate: called CC=1 SCAR=0.0
Pod undocumented
392 2 my $self = shift;
393 2 my @params = validate_pos(
394 @_,
395 { type => SCALAR },
396 { type => SCALAR },
397 { type => SCALAR },
398 { type => SCALAR },
399 { type => SCALAR },
400 { type => SCALAR },
401 { type => HASHREF, default => {} }
402 );
403 2 my ( $template, $data_feed, $setup, $subject_line, $schedule_time, $list, $options ) = @params;
404 2 warnings::warnif( 'deprecated', 'copyTemplate is deprecated, use schedule_blast_from_template instead' );
405 # initialize our data hash by copying the contents of the options hash
406 2 my $data = { %{$options} };
407 2 $data->{copy_template} = $template;
408 2 $data->{data_feed_url} = $data_feed;
409 2 $data->{setup} = $setup;
410 2 $data->{name} = $subject_line;
411 2 $data->{schedule_time} = $schedule_time;
412 2 $data->{list} = $list;
413 2 return $self->api_post( 'blast', $data );
414 }
415  
416 # args:
417 # * template_name - scalar
418 1 ▶sub getTemplate {
Subroutine getTemplate: called CC=1 SCAR=0.0
Pod undocumented
419 1 my $self = shift;
420 1 warnings::warnif( 'deprecated', 'getTemplate is deprecated, use get_template instead' );
421 1 return $self->get_template(@_);
422 }
423  
424 # args:
425 # * email - scalar
426 # * password - scalar
427 # * include_names - scalar (optional)
428 2 ▶sub importContacts {
Subroutine importContacts: called CC=1 SCAR=0.0
Pod undocumented
429 2 my $self = shift;
430 2 my @p = validate_pos( @_, { type => SCALAR }, { type => SCALAR }, { type => SCALAR, default => 0 } );
431 2 my ( $email, $password, $include_names ) = @p;
432 2 warnings::warnif( 'deprecated',
433 'importContacts is deprecated. The contacts API has been discontinued as of August 1st, 2011.' );
434 2 my $data = {
435 email => $email,
436 password => $password,
437 include_names => $include_names,
438 };
439 2 return $self->api_post( 'contacts', $data );
440 }
441  
442 5 1;
443  
444 __END__
↑ Summary