File Coverage

blib/lib/SMS/Matrix.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package SMS::Matrix;
2              
3             # ABSTRACT: Module for the SMS::Matrix API
4              
5 7     7   185775 use warnings;
  7         17  
  7         235  
6 7     7   38 use strict;
  7         12  
  7         326  
7              
8 7     7   10467 use LWP::UserAgent;
  7         438593  
  7         246  
9 7     7   8015 use HTTP::Request::Common;
  7         17023  
  7         580  
10 7     7   9843 use JSON();
  0            
  0            
11             ## use Data::Dumper;
12              
13             our $VERSION = '1.01';
14              
15             ####################################################
16              
17             my @attrs = qw(username password);
18              
19             for my $attr ( @attrs )
20             {
21             no strict 'refs';
22             *{ __PACKAGE__ . '::' . $attr } = sub
23             {
24             my ($self, $value) = @_;
25            
26             my $key = '__' . $attr . '__';
27             $self->{$key} = $value if @_ == 2;
28             return $self->{$key};
29             };
30             }
31              
32             ####################################################
33              
34             sub new
35             {
36             my ($class, %param) = @_;
37            
38             my $self = bless {}, $class;
39             for my $attr ( @attrs )
40             {
41             if (exists $param{$attr}) { $self->$attr( $param{$attr} ); }
42             }
43             return $self;
44             }
45              
46             ####################################################
47              
48             sub send
49             {
50             my ($self, $rqp) = @_;
51              
52             if ($rqp->{txt} eq '')
53             {
54             $self->errstr ('Message text is blank');
55             $self->status (1001);
56             return;
57             }
58              
59             my $json_txt = JSON::to_json ($rqp);
60             if ($json_txt eq '')
61             {
62             $self->errstr ('JSON::to_json() returned NULL');
63             $self->status (1000);
64             return;
65             }
66              
67             my $ua = LWP::UserAgent->new();
68             my $res = $ua->request
69             (
70             POST $rqp->{url},
71             Content_Type => 'application/json',
72             Content => $json_txt
73             );
74              
75             ## print Dumper($rqp) . "\n\n";
76             ## print Dumper($res) . "\n\n";
77              
78             if ($res->is_error)
79             {
80             $self->errstr ($res->{_msg});
81             $self->status ($res->{_rc});
82             return;
83             }
84             my $resp = undef;
85             eval { $resp = JSON::from_json ($res->content); };
86              
87             $self->errstr ($resp->{STATUSTXT});
88             $self->status ($resp->{STATUSCODE});
89              
90             return $resp;
91             }
92              
93             ####################################################
94              
95             sub send_sms
96             {
97             my ($self, %param) = @_;
98              
99             my $rqp =
100             {
101             'username' => $self->{__username__},
102             'password' => $self->{__password__},
103             'url' => 'https://www.smsmatrix.com/matrix.json',
104             };
105             while (my ($key, $value) = each (%param)) { $rqp->{$key} = $value; }
106             return $self->send ($rqp);
107             }
108              
109             ####################################################
110              
111             sub send_tts
112             {
113             my ($self, %param) = @_;
114              
115             my $rqp =
116             {
117             'username' => $self->{__username__},
118             'password' => $self->{__password__},
119             'url' => 'https://www.smsmatrix.com/matrix_tts.json',
120             };
121             while (my ($key, $value) = each (%param)) { $rqp->{$key} = $value; }
122             return $self->send ($rqp);
123             }
124              
125             ####################################################
126              
127             sub get_balance
128             {
129             my ($self, %param) = @_;
130              
131             my $rqp =
132             {
133             'username' => $self->{__username__},
134             'password' => $self->{__password__},
135             'url' => 'https://www.smsmatrix.com/balance.json',
136             'txt' => 'x',
137             };
138             while (my ($key, $value) = each (%param)) { $rqp->{$key} = $value; }
139             return $self->send ($rqp);
140             }
141              
142             ####################################################
143              
144             sub is_success
145             {
146             my ($self) = @_;
147            
148             my $s = $self->{__status__};
149             return (($s >= 0) and ($s < 399));
150             }
151              
152             ####################################################
153              
154             sub is_error
155             {
156             my ($self) = @_;
157            
158             return ! $self->is_success();
159             }
160              
161             ####################################################
162              
163             sub errstr
164             {
165             my ($self,$message) = @_;
166            
167             $self->{__errstr__} = $message if @_ == 2;
168             return $self->{__errstr__};
169             }
170              
171             ####################################################
172              
173             sub status
174             {
175             my ($self, $status) = @_;
176            
177             $self->{__status__} = $status if @_ == 2;
178             return $self->{__status__};
179             }
180              
181             ####################################################
182              
183              
184             1; # End of Matrix::SMS
185              
186             __END__