blib/lib/SMS/Send/Telenor.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 85 CC 5 SCAR 15.8
CC
5
Cov
0%
send_sms
12.0
new
6.0
_verify_response
6.0
CRAP 4.9
1 package SMS::Send::Telenor;
2 use HTTP::Tiny;
3 use strict;
4 use warnings;
5 our $VERSION = '0.06';
6 use base 'SMS::Send::Driver';
7 use Data::Dumper;
8  
9 sub new {
10 my ($class, %args) = @_;
11 die "$class needs hash_ref with _login and _password. (Optional _sender.)\n" unless $args{'_login'} && $args{'_password'};
12 my $self = bless {%args}, $class;
13 $self->{base_url} = 'http://sms-pro.net/services/' . $self->{_login};
14 $self->{send_url} = $self->{base_url} . '/sendsms';
15 $self->{status_url} = $self->{base_url} . '/status';
16 $self->{_sender} = $self->{_sender} // 'FROM SENDER'; #Add the text that describes who sent the sms if not sent as _sender to new. Max 11 chars.
17 return $self;
18 }
19  
20 sub send_sms {
21 my ($self, %args) = @_;
22 my $xml_args = {
23 customer_id => "$self->{_login}",
24 password => "$self->{_password}",
25 message => "$args{'text'}",
26 to_msisdn => "$args{'to'}",
27 sms_sender => "$self->{_sender}"
28 };
29 my $sms_xml = _build_sms_xml($xml_args);
30 my $response = _post($self->{send_url}, $sms_xml);
31  
32 my $rv = 1;
33 if (defined $response) {
34 $rv = _verify_response("$response->{content}");
35 }
36 return 1 if $rv eq '0';
37 return 0;
38 }
39  
40 # This status subroutine is not used by SMS::Send but can be used directly with the driver.
41 sub sms_status {
42 my ($self, $mobilectrl_id) = @_;
43 my $args = {
44 customer_id => "$self->{_login}",
45 mobilectrl_id => "$mobilectrl_id"
46 };
47 my $xml = _build_status_xml($args);
48 return _post($self->{status_url}, $xml);
49 }
50  
51 sub _post {
52 my ($url, $sms_xml) = @_;
53 return HTTP::Tiny->new->post(
54 $url => {
55 content => $sms_xml,
56 headers => {
57 "Content-Type" => "application/xml",
58 },
59 },
60 );
61 }
62  
63 sub _build_sms_xml {
64 my $args = shift;
65 return '<?xml version="1.0" encoding="ISO-8859-1"?>'
66 . '<mobilectrl_sms>'
67 . '<header>'
68 . '<customer_id>'
69 . $args->{customer_id}
70 . '</customer_id>'
71 . '<password>'
72 . $args->{password}
73 . '</password>'
74 . '<from_alphanumeric>'
75 . $args->{sms_sender}
76 . '</from_alphanumeric>'
77 . '</header>'
78 . '<payload>'
79 . '<sms account="71700">'
80 . '<message><![CDATA['
81 . $args->{message}
82 . ']]></message>'
83 . '<to_msisdn>'
84 . $args->{to_msisdn}
85 . '</to_msisdn>'
86 . '</sms>'
87 . '</payload>'
88 . '</mobilectrl_sms>';
89 }
90  
91 sub _build_status_xml {
92 my $args = shift;
93 return '<?xml version="1.0" encoding="ISO-8859-1"?>'
94 . '<mobilectrl_delivery_status_request>'
95 . '<customer_id>'
96 . $args->{customer_id}
97 . '</customer_id>'
98 . '<status_for type="mobilectrl_id">'
99 . $args->{mobilectrl_id}
100 . '</status_for>'
101 . '</mobilectrl_delivery_status_request>';
102 }
103  
104 sub _verify_response {
105 my $content = shift;
106 if ($content =~ /\<status\>(\d)\<\/status>/) {
107 return $1;
108 }
109 return 1;
110 }
111  
112 1;
113  
114 __END__