blib/lib/SMS/Send/AT/SmsAt.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package SMS::Send::AT::SmsAt;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use XML::XPath;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use XML::Writer;
9 use SMS::Send::Driver;
10  
11 use version; our $VERSION = qv('0.0.5');
12  
13 use base 'SMS::Send::Driver';
14  
15 sub new {
16 my $proto = shift;
17 my $class = ref $proto || $proto;
18 my $self = { @_ };
19  
20 $self->{"_$_"} or croak "no $_ specified" for qw(login password);
21  
22 return bless $self, $class;
23 }
24  
25 sub send_sms {
26 my $self = shift;
27 my %params = @_;
28  
29 croak 'no recipient specified' unless $params{to};
30 $params{to} =~ s/\A\+//;
31 $params{to} =~ s/\A0/43/; # Austria as default country
32  
33 my $text = unpack 'H*', $params{text} or croak 'no message specified';
34  
35 my $ua = LWP::UserAgent->new();
36 my $message;
37 my $writer = XML::Writer->new(OUTPUT => \$message, DATA_MODE => 1, DATA_INDENT => 4);
38  
39 $writer->xmlDecl();
40 $writer->startTag('Request');
41 $writer->dataElement('AccountLogin', $self->{_login}, Type => 'email');
42 $writer->dataElement('AccountPass', $self->{_password});
43  
44 $writer->startTag('Message', Type => 'MTSMS');
45 $writer->startTag('Recipients');
46 $writer->dataElement('Recipient', $params{to}, Type => 'International');
47 $writer->endTag('Recipients');
48  
49 $writer->dataElement('Text', $text, AutoSegment => 'simple');
50 $writer->endTag('Message');
51 $writer->endTag('Request');
52 $writer->end();
53  
54 my $response = $ua->post('http://gateway.sms.at/xml_interface/', 'Content-type' => 'text/xml', Content => $message);
55 if ($response->is_success) {
56 # warn $response->content; # or whatever
57 my $xp = XML::XPath->new(xml => $response->content);
58 my $code = $xp->getNodeText('/Response/Code');
59 # stringify the XML::XPath::Literal object we just got for comparison
60 return 1 if "$code" == 2000 or "$code" == 2001; # success
61 warn $code . ': ' . $xp->getNodeText('/Response/CodeDescription');
62 return 0;
63 }
64 else {
65 Carp::croak($response->status_line);
66 }
67  
68 return 1;
69 }
70  
71  
72 1; # Magic true value required at end of module
73 __END__
↑ Summary