blib/lib/SMS/Send/Twilio.pm

filter: Statementstmt 69.0% 29 / 42 Branchbran 25.0% 3 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 100% 2 / 2 totaltotal 64.6% 42 / 65 CC 8 SCAR 23.8
CC
8
Cov
59%
send_sms
30.0
new
4.0
BEGIN
1.0
CRAP 10.8
↑ Summary
1 package SMS::Send::Twilio;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use WWW::Twilio::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use parent qw(SMS::Send::Driver);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '0.20';
15  
16 =encoding utf-8
17  
18 =head1 NAME
19  
20 SMS::Send::Twilio - SMS::Send backend for Twilio API
21  
22 =head1 SYNOPSIS
23  
24 use SMS::Send;
25 # Create an object. There are three required values:
26 my $sender = SMS::Send->new('Twilio',
27 _accountsid => 'ACb657bdcb16f06893fd127e099c070eca',
28 _authtoken => 'b857f7afe254fa86c689648447e04cff',
29 _from => '+15005550006',
30 );
31  
32 # Send a message to me
33 my $sent = $sender->send_sms(
34 text => 'Messages can be up to 1600 characters',
35 to => '+31645742418',
36 );
37  
38 # Did it send?
39 if ( $sent ) {
40 print "Sent test message\n";
41 } else {
42 print "Test message failed\n";
43 }
44  
45 =head1 DESCRIPTION
46  
47 SMS::Send::Twilio is an SMS::Send driver for the Twilio web service.
48  
49 =pod
50  
51 =head2 new
52  
53 # Create a new sender using this driver
54 my $sender = SMS::Send->new('Twilio',
55 _accountsid => 'ACb657bdcb16f06893fd127e099c070eca',
56 _authtoken => 'b857f7afe254fa86c689648447e04cff',
57 _from => '+15005550006',
58 );
59  
60 The C<new> constructor takes three parameters, which should be passed
61 through from the L<SMS::Send> constructor.
62  
63 =head2 send_sms
64  
65 It's really easy; if it returns a true value, sending the message was OK.
66 If not we'd see an error message on STDERR.
67  
68 # Send a message to me
69 my $sent = $sender->send_sms(
70 text => 'Messages can be up to 1600 characters',
71 to => '+31645742418',
72 );
73  
74 =cut
75  
76 2 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
77 2 my $class = shift;
78 2 my %params = @_;
79  
80 # check required parameters
81 2 for my $param (qw ( _accountsid _from _authtoken )) {
82 4 ▶ exists $params{$param}
Branch: 1 branch
BranchTrueFalseTotal
exists $params{$param} or croak($class . "->new requires $param parameter") 3 1 4
83 or croak $class . "->new requires $param parameter";
84 }
85  
86 1 my $self = \%params;
87 1 bless $self, $class;
88  
89 # Create twilio object
90 $self->{twilio} = WWW::Twilio::API->new(
91 AccountSid => $self->{_accountsid},
92 AuthToken => $self->{_authtoken},
93 1 ▶ ) or croak $class . "->new can't set up connection: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"twilio"} = "WWW::Twilio::API"->new("AccountSid", $self->{"_accountsid"}, "AuthToken", $self->{"_authtoken"}) or croak($class . "->new can't set up connection: $!") 1 0 1
94  
95 1 return $self;
96 }
97  
98 0 ▶sub send_sms {
Subroutine send_sms: not called CC=5 SCAR=34.0
Pod documented
99 0 my $self = shift;
100 0 my %params = @_;
101  
102 # Get the message and destination
103 0 my $message = delete $params{text};
104 0 my $recipient = delete $params{to};
105  
106 my $response = $self->{twilio}->POST(
107 'Messages.json',
108 From => $self->{_from},
109 0 To => $recipient,
110 Body => $message,
111 );
112  
113 0 ▶ if ( $response->{code} == '201' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($response->{'code'} == '201') { } 0 0 0
elsif ($response->{'code'} == '400') { } 0 0 0
114 0 my $result = JSON::PP->new->utf8->decode( $response->{content} );
115 0 ▶ if ( $result->{sid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"sid"}) 0 0 0
116 0 return $result->{sid};
117 }
118 }
119 elsif ( $response->{code} == '400' ) {
120 0 my $result = JSON::PP->new->utf8->decode( $response->{content} );
121 0 ▶ if ( $result->{message} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"message"}) 0 0 0
122 0 croak "$result->{message}";
123 }
124 }
125  
126 0 croak "Can't send SMS: $response->{code} $response->{message}";
127 }
128  
129 =head1 AUTHOR
130  
131 Michiel Beijen E<lt>michiel.beijen@gmail.comE<gt>
132  
133 =head1 COPYRIGHT
134  
135 Copyright 2013-2015 Michiel Beijen
136  
137 =head1 LICENSE
138  
139 This library is free software; you can redistribute it and/or modify
140 it under the same terms as Perl itself.
141  
142 =head1 SEE ALSO
143  
144 L<SMS::Send>
145 L<WWW::Twilio::API>
146  
147 =cut
↑ Summary