blib/lib/Sailthru/Client.pm untested

filter: Statementstmt 0.0% 0 / 186 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 263 SCAR 48.8
CC
11
Cov
0%
send
20.0
set_email
20.0
_http_request
20.0
CRAP 132.0
1 package Sailthru::Client;
2  
3 use strict;
4 use warnings;
5  
6 use Carp;
7 use JSON::XS;
8 use LWP::UserAgent;
9 use Digest::MD5 qw( md5_hex );
10 use Params::Validate qw( :all );
11 use Readonly;
12 use URI;
13  
14 our $VERSION = '2.002';
15 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 sub new {
27 my ( $class, $api_key, $secret, $timeout ) = @_;
28 my $self = {
29 api_key => $api_key,
30 secret => $secret,
31 ua => LWP::UserAgent->new,
32 };
33 $self->{ua}->timeout($timeout) if $timeout;
34 $self->{ua}->default_header( 'User-Agent' => "Sailthru API Perl Client $VERSION" );
35 return bless $self, $class;
36 }
37  
38 # args:
39 #
40 # * template_name - scalar
41 # * email - scalar
42 # * vars - hashref (optional)
43 # * options - hashref (optional)
44 # * schedule_time - scalar (optional)
45 sub send {
46 my $self = shift;
47 my @params = validate_pos(
48 @_,
49 { type => SCALAR },
50 { type => SCALAR },
51 { type => HASHREF, default => {} },
52 { type => HASHREF, default => {} },
53 { type => SCALAR, default => undef }
54 );
55 my ( $template_name, $email, $vars, $options, $schedule_time ) = @params;
56 my $data = {};
57 $data->{template} = $template_name;
58 $data->{email} = $email;
59 $data->{vars} = $vars if keys %{$vars};
60 $data->{options} = $options if keys %{$options};
61 $data->{schedule_time} = $schedule_time if $schedule_time;
62 return $self->api_post( 'send', $data );
63 }
64  
65 # args:
66 # * send_id - scalar
67 sub get_send {
68 my $self = shift;
69 my @params = validate_pos( @_, { type => SCALAR } );
70 my ($send_id) = @params;
71 return $self->api_get( 'send', { send_id => $send_id } );
72 }
73  
74 # args:
75 # * email - scalar
76 sub get_email {
77 my $self = shift;
78 my @params = validate_pos( @_, { type => SCALAR } );
79 my ($email) = @params;
80 return $self->api_get( 'email', { email => $email } );
81 }
82  
83 # args:
84 # * email - scalar
85 # * vars - hashref (optional)
86 # * lists - hashref (optional)
87 # * templates - hashref (optional)
88 sub set_email {
89 my $self = shift;
90 my @params = validate_pos(
91 @_,
92 { type => SCALAR },
93 { type => HASHREF, default => {} },
94 { type => HASHREF, default => {} },
95 { type => HASHREF, default => {} }
96 );
97 my ( $email, $vars, $lists, $templates ) = @params;
98 my $data = {};
99 $data->{email} = $email;
100 $data->{vars} = $vars if keys %{$vars};
101 $data->{lists} = $lists if keys %{$lists};
102 $data->{templates} = $templates if keys %{$templates};
103 return $self->api_post( 'email', $data );
104 }
105  
106 # args:
107 # * name - scalar
108 # * list - scalar
109 # * schedule_time - scalar
110 # * from_name - scalar
111 # * from_email - scalar
112 # * subject - scalar
113 # * content_html - scalar
114 # * content_text - scalar
115 # * options - hashref (optional)
116 sub schedule_blast {
117 my $self = shift;
118 my @params = validate_pos(
119 @_,
120 { type => SCALAR },
121 { type => SCALAR },
122 { type => SCALAR },
123 { type => SCALAR },
124 { type => SCALAR },
125 { type => SCALAR },
126 { type => SCALAR },
127 { type => SCALAR },
128 { type => HASHREF, default => {} }
129 );
130 my ( $name, $list, $schedule_time, $from_name, $from_email, $subject, $content_html, $content_text, $options ) =
131 @params;
132 # initialize our data hash by copying the contents of the options hash
133 my $data = { %{$options} };
134 $data->{name} = $name;
135 $data->{list} = $list;
136 $data->{schedule_time} = $schedule_time;
137 $data->{from_name} = $from_name;
138 $data->{from_email} = $from_email;
139 $data->{subject} = $subject;
140 $data->{content_html} = $content_html;
141 $data->{content_text} = $content_text;
142 return $self->api_post( 'blast', $data );
143 }
144  
145 # args:
146 # * template_name - scalar
147 # * list - scalar
148 # * schedule_time - scalar
149 # * options - hashref (optional)
150 sub schedule_blast_from_template {
151 my $self = shift;
152 my @params = validate_pos(
153 @_,
154 { type => SCALAR },
155 { type => SCALAR },
156 { type => SCALAR },
157 { type => HASHREF, default => {} },
158 );
159 my ( $template_name, $list, $schedule_time, $options ) = @params;
160 # initialize our data hash by copying the contents of the options hash
161 my $data = { %{$options} };
162 $data->{copy_template} = $template_name;
163 $data->{list} = $list;
164 $data->{schedule_time} = $schedule_time;
165 return $self->api_post( 'blast', $data );
166  
167 }
168  
169 # args:
170 # * blast_id - scalar
171 sub get_blast {
172 my $self = shift;
173 my @params = validate_pos( @_, { type => SCALAR } );
174 my ($blast_id) = @params;
175 return $self->api_get( 'blast', { blast_id => $blast_id } );
176 }
177  
178 # args:
179 # * template_name - scalar
180 sub get_template {
181 my $self = shift;
182 my @params = validate_pos( @_, { type => SCALAR } );
183 my ($template_name) = @params;
184 return $self->api_get( 'template', { template => $template_name } );
185 }
186  
187 # args:
188 # * action - scalar
189 # * data - hashref
190 sub api_get {
191 my $self = shift;
192 my @params = validate_pos( @_, { type => SCALAR }, { type => HASHREF } );
193 my ( $action, $data ) = @params;
194 return $self->_api_request( $action, $data, 'GET' );
195 }
196  
197 # args:
198 # * action - scalar
199 # * data - hashref
200 # * TODO: optional binary_key arg
201 sub api_post {
202 my $self = shift;
203 my @params = validate_pos( @_, { type => SCALAR }, { type => HASHREF } );
204 my ( $action, $data ) = @params;
205 return $self->_api_request( $action, $data, 'POST' );
206 }
207  
208 # args:
209 # * action - scalar
210 # * data - hashref
211 sub api_delete {
212 my $self = shift;
213 my @params = validate_pos( @_, { type => SCALAR }, { type => HASHREF } );
214 my ( $action, $data ) = @params;
215 return $self->_api_request( $action, $data, 'DELETE' );
216 }
217  
218 #
219 # private helper methods
220 #
221  
222 # args:
223 # * action - scalar
224 # * data - hashref
225 # * request_type - scalar
226 sub _api_request {
227 my $self = shift;
228 my @params = validate_pos( @_, { type => SCALAR }, { type => HASHREF }, { type => SCALAR } );
229 my ( $action, $data, $request_type ) = @params;
230 my $payload = $self->_prepare_json_payload($data);
231 my $action_uri = $API_URI . $action;
232 my $response = $self->_http_request( $action_uri, $payload, $request_type );
233 return decode_json( $response->content );
234 }
235  
236 # args:
237 # * uri - scalar
238 # * data - hashref
239 # * method - scalar
240 sub _http_request {
241 my $self = shift;
242 my @params = validate_pos( @_, { type => SCALAR }, { type => HASHREF }, { type => SCALAR } );
243 my ( $uri, $data, $method ) = @params;
244 $uri = URI->new($uri);
245 my $response;
246 if ( $method eq 'GET' ) {
247 $uri->query_form($data);
248 $response = $self->{ua}->get($uri);
249 }
250 elsif ( $method eq 'POST' ) {
251 $response = $self->{ua}->post( $uri, $data );
252 }
253 elsif ( $method eq 'DELETE' ) {
254 $uri->query_form($data);
255 $response = $self->{ua}->delete($uri);
256 }
257 else {
258 croak "Invalid method: $method";
259 }
260 return $response;
261 }
262  
263 # args:
264 # * data - hashref
265 sub _prepare_json_payload {
266 my $self = shift;
267 my @params = validate_pos( @_, { type => HASHREF } );
268 my ($data) = @params;
269 my $payload = {};
270 $payload->{api_key} = $self->{api_key};
271 $payload->{format} = 'json';
272 # this gives us nice clean utf8 encoded json text
273 $payload->{json} = encode_json($data);
274 $payload->{sig} = $self->_get_signature_hash( $payload, $self->{secret} );
275 return $payload;
276 }
277  
278 # Every request must also generate a signature hash called sig according to the
279 # following rules:
280 #
281 # * take the string values of every parameter, including api_key
282 # * sort the values alphabetically, case-sensitively (i.e. ordered by Unicode code point)
283 # * concatenate the sorted values, and prepend this string with your shared secret
284 # * generate an MD5 hash of this string and use this as sig
285 # * now generate your URL-encoded query string from your parameters plus sig
286  
287 # args:
288 # * params - hashref
289 # * secret - scalar
290 # NOTE This internal method assumes a single level hash with values for only 'api_key', 'format', and 'json'
291 # NOTE Since we pack everything into the 'json' value this is safe and we do not need to recurse down a nested hash.
292 sub _get_signature_hash {
293 my $self = shift;
294 my @params = validate_pos( @_, { type => HASHREF }, { type => SCALAR } );
295 my ( $api_param_hash, $secret ) = @params;
296 my @api_param_values = values %{$api_param_hash};
297 my $sig_string = join '', $secret, sort @api_param_values;
298 # assumes utf8 encoded text, works fine because we use encode_json internally
299 return md5_hex($sig_string);
300 }
301  
302 ### XXX
303 ### DEPRECATED METHODS
304 ### XXX
305  
306 # args:
307 # * email - scalar
308 sub getEmail {
309 my $self = shift;
310 warnings::warnif( 'deprecated', 'getEmail is deprecated, use get_email instead' );
311 return $self->get_email(@_);
312 }
313  
314 # args:
315 # * email - scalar
316 # * vars - hashref (optional)
317 # * lists - hashref (optional)
318 # * templates - hashref (optional)
319 sub setEmail {
320 my $self = shift;
321 warnings::warnif( 'deprecated', 'setEmail is deprecated, use set_email instead' );
322 return $self->set_email(@_);
323 }
324  
325 # args:
326 # * send_id - scalar
327 sub getSend {
328 my $self = shift;
329 warnings::warnif( 'deprecated', 'getSend is deprecated, use get_send instead' );
330 return $self->get_send(@_);
331 }
332  
333 # args:
334 # * name - scalar
335 # * list - scalar
336 # * schedule_time - scalar
337 # * from_name - scalar
338 # * from_email - scalar
339 # * subject - scalar
340 # * content_html - scalar
341 # * content_text - scalar
342 # * options - hashref (optional)
343 sub scheduleBlast {
344 my $self = shift;
345 warnings::warnif( 'deprecated', 'scheduleBlast is deprecated, use schedule_blast instead' );
346 return $self->schedule_blast(@_);
347 }
348  
349 # args:
350 # * blast_id - scalar
351 sub getBlast {
352 my $self = shift;
353 warnings::warnif( 'deprecated', 'getBlast is deprecated, use get_blast instead' );
354 return $self->get_blast(@_);
355 }
356  
357 sub copyTemplate {
358 my $self = shift;
359 my @params = validate_pos(
360 @_,
361 { type => SCALAR },
362 { type => SCALAR },
363 { type => SCALAR },
364 { type => SCALAR },
365 { type => SCALAR },
366 { type => SCALAR },
367 { type => HASHREF, default => {} }
368 );
369 my ( $template, $data_feed, $setup, $subject_line, $schedule_time, $list, $options ) = @params;
370 warnings::warnif( 'deprecated', 'copyTemplate is deprecated, use schedule_blast_from_template instead' );
371 # initialize our data hash by copying the contents of the options hash
372 my $data = { %{$options} };
373 $data->{copy_template} = $template;
374 $data->{data_feed_url} = $data_feed;
375 $data->{setup} = $setup;
376 $data->{name} = $subject_line;
377 $data->{schedule_time} = $schedule_time;
378 $data->{list} = $list;
379 return $self->api_post( 'blast', $data );
380 }
381  
382 # args:
383 # * template_name - scalar
384 sub getTemplate {
385 my $self = shift;
386 warnings::warnif( 'deprecated', 'getTemplate is deprecated, use get_template instead' );
387 return $self->get_template(@_);
388 }
389  
390 # args:
391 # * email - scalar
392 # * password - scalar
393 # * include_names - scalar (optional)
394 sub importContacts {
395 my $self = shift;
396 my @p = validate_pos( @_, { type => SCALAR }, { type => SCALAR }, { type => SCALAR, default => 0 } );
397 my ( $email, $password, $include_names ) = @p;
398 warnings::warnif( 'deprecated',
399 'importContacts is deprecated. The contacts API has been discontinued as of August 1st, 2011.' );
400 my $data = {
401 email => $email,
402 password => $password,
403 include_names => $include_names,
404 };
405 return $self->api_post( 'contacts', $data );
406 }
407  
408 1;
409  
410 __END__
411  
412 =head1 NAME
413  
414 Sailthru::Client - Perl module for accessing Sailthru's API
415  
416 =head1 SYNOPSIS
417  
418 use Sailthru::Client;
419  
420 # instantiate a new Sailthru::Client with an api_key and secret
421 $sc = Sailthru::Client->new('api_key', 'secret');
422  
423 # send an email to a single email address
424 %vars = (
425 name => "Joe Example",
426 from_email => "approved_email@your_domain.com",
427 your_variable => "some_value"
428 );
429 %options = ( reply_to => "your reply_to header");
430 $sc->send('template_name', 'example@example.com', \%vars, \%options);
431  
432 =head1 DESCRIPTION
433  
434 Sailthru::Client is a Perl module for accessing the Sailthru API.
435  
436 Methods return a reference to a hash containing the response values. Dump the
437 hash or read the Sailthru API documentation for which values are returned
438 by which API calls.
439  
440 L<http://docs.sailthru.com/api>
441  
442 Some options might change. Consult the Sailthru API documentation for
443 the latest information.
444  
445 =head1 METHODS
446  
447 =head2 Sailthru::Client->new( $api_key, $secret, [$timeout] )
448  
449 Returns a new Sailthru::Client object.
450  
451 =over
452  
453 =item $api_key
454  
455 Sailthru API key.
456  
457 =item $secret
458  
459 Sailthru API secret.
460  
461 =item $timeout
462  
463 Optional network timeout in seconds.
464  
465 =back
466  
467 =head2 $sc->send( $template_name, $email, [\%vars, \%options, $schedule_time] )
468  
469 Remotely send an email template to a single email address.
470  
471 API docs: L<http://docs.sailthru.com/api/send>
472  
473 =over
474  
475 =item $template_name
476  
477 The name of the template to send.
478  
479 =item $email
480  
481 The email address to send to.
482  
483 =item \%vars
484  
485 An optional hashref of the replacement vars to use in the send. Each var may be referenced as {varname} within the template itself.
486  
487 =item \%options
488  
489 An optional hashref to include a replyto header, test keys, etc. See the API documentation for details.
490  
491 =item $schedule_time
492  
493 Do not send the email immediately, but at some point in the future. Any date recognized by PHP's strtotime function is valid, but be sure to specify timezone or use a UTC time to avoid confusion. You may also use relative time.
494  
495 =back
496  
497 =head2 $sc->get_send( $send_id )
498  
499 Get the status of a send.
500  
501 API docs: L<http://docs.sailthru.com/api/send>
502  
503 =over
504  
505 =item $send_id
506  
507 The unique identifier of the send returned in the response from C<$sc-E<gt>send()>.
508  
509 =back
510  
511 =head2 $sc->get_email( $email )
512  
513 Get information about a user.
514  
515 API docs: L<http://docs.sailthru.com/api/email>
516  
517 =over
518  
519 =item $email
520  
521 The email address to look up.
522  
523 =back
524  
525 =head2 $sc->set_email( $email, [\%vars, \%lists, \%templates] )
526  
527 Update information about a user, including adding and removing the user from lists.
528  
529 API docs: L<http://docs.sailthru.com/api/email>
530  
531 =over
532  
533 =item $email
534  
535 The email address to modify.
536  
537 =item \%vars
538  
539 An optional hashref of replacement variables you want to set or a JSON string
540  
541 =item \%lists
542  
543 An optional hashref. Each key is the name of a list and each value is
544 1 to subscribe the user to that list or 0 to remove the user from the list.
545  
546 =item \%templates
547  
548 An optional hashref. Each key is the name of a template, and each value is 1 to
549 opt the user back in to template delivery or 0 to opt the user out of template
550 delivery.
551  
552 =back
553  
554 =head2 $sc->schedule_blast( $name, $list, $schedule_time, $from_name, $from_email, $subject, $content_html, $content_text, [\%options] )
555  
556 Schedule a mass mail blast.
557  
558 API docs: L<http://docs.sailthru.com/api/blast>
559  
560 =over
561  
562 =item $name
563  
564 The name to give to this new blast.
565  
566 =item $list
567  
568 The mailing list name to send to.
569  
570 =item $schedule_time
571  
572 When the blast should send. Dates in the past will be scheduled for immediate
573 delivery. Any English textual datetime format known to PHP's strtotime function
574 is acceptable, such as 2012-03-18 23:57:22 UTC, now (immediate delivery), +3
575 hours (3 hours from now), or March 18, 9:30 EST. Be sure to specify a timezone
576 if you use an exact time.
577  
578 =item $from_name
579  
580 The name to use in "From" in the email.
581  
582 =item $from_email
583  
584 The email address to use in "From". Choose from any of your verified emails.
585  
586 =item $subject
587  
588 The subject line of the email.
589  
590 =item $content_html
591  
592 The HTML format version of the email.
593  
594 =item $content_text
595  
596 The text format version of the email.
597  
598 =item \%options
599  
600 An optional hashref containing the optional parameters for a blast. See the API documentation for details.
601  
602 =back
603  
604 =head2 $sc->schedule_blast_from_template( $template_name, $list, $schedule_time, [\%options] )
605  
606 Schedule a mass mail blast from a template.
607  
608 API docs: L<http://docs.sailthru.com/api/blast>
609  
610 =over
611  
612 =item $template_name
613  
614 The template to copy from.
615  
616 =item $list
617  
618 The mailing list name to send to.
619  
620 =item $schedule_time
621  
622 When the blast should send. Dates in the past will be scheduled for immediate
623 delivery. Any English textual datetime format known to PHP's strtotime function
624 is acceptable, such as 2012-03-18 23:57:22 UTC, now (immediate delivery), +3
625 hours (3 hours from now), or March 18, 9:30 EST. Be sure to specify a timezone
626 if you use an exact time.
627  
628 =item \%options
629  
630 An optional hashref containing the optional parameters for a blast. See the API documentation for details.
631  
632 =back
633  
634 =head2 $sc->get_blast( $blast_id )
635  
636 Get data on a single blast.
637  
638 API docs: L<http://docs.sailthru.com/api/blast>
639  
640 =over
641  
642 =item $blast_id
643  
644 The blast id returned in the response from C<$sc-E<gt>scheduleBlast()>.
645  
646 =back
647  
648 =head2 $sc->get_template( $template_name )
649  
650 Get information about a template
651  
652 API docs: L<http://docs.sailthru.com/api/template>
653  
654 =over
655  
656 =item $template_name
657  
658 The name of the template.
659  
660 =back
661  
662 =head2 $sc->api_get( $action, \%data )
663  
664 This is a generic HTTP GET call to the API.
665  
666 =over
667  
668 =item $action
669  
670 The name of the API action to call.
671  
672 =item \%data
673  
674 A hashref of arguments to pass to the API.
675  
676 =back
677  
678 For example, you could get information about an email with
679  
680 $sc->api_get('GET', 'email', {email=>'somebody@example.com'});
681  
682 =head2 $sc->api_post( $action, \%data )
683  
684 This is a generic HTTP POST call to the API.
685  
686 =over
687  
688 =item $action
689  
690 The name of the API action to call.
691  
692 =item \%data
693  
694 A hashref of arguments to pass to the API.
695  
696 =back
697  
698 =head2 $sc->api_delete( $action, \%data )
699  
700 This is a generic HTTP DELETE call to the API.
701  
702 =over
703  
704 =item $action
705  
706 The name of the API action to call.
707  
708 =item \%data
709  
710 A hashref of arguments to pass to the API.
711  
712 =back
713  
714 =head1 SEE ALSO
715  
716 See the Sailthru API documentation for more details on their API.
717  
718 L<http://docs.sailthru.com/api>
719  
720 =head1 AUTHOR
721  
722 Finn Smith
723  
724 Steve Sanbeg
725  
726 Steve Miketa
727  
728 Sam Gerstenzang
729  
730 =head1 LICENSE AND COPYRIGHT
731  
732 Copyright (C) 2012 by Finn Smith <finn@timeghost.net>
733  
734 Copyright (C) 2012 by Steve Sanbeg <stevesanbeg@buzzfeed.com>
735  
736 Copyright (C) 2011 by Steve Miketa <steve@sailthru.com>
737  
738 Adapted from the original Sailthru::Client & Triggermail modules created by Sam
739 Gerstenzang and Steve Miketa.
740  
741 This library is free software; you can redistribute it and/or modify it under
742 the same terms as Perl itself, either Perl version 5.10.0 or, at your option,
743 any later version of Perl 5 you may have available.
744  
745 =cut