File Coverage

blib/lib/SMS/Send/Retarus.pm
Criterion Covered Total %
statement 31 47 65.9
branch 0 2 0.0
condition 2 2 100.0
subroutine 10 14 71.4
pod 2 2 100.0
total 45 67 67.1


line stmt bran cond sub pod time code
1 1     1   55603 use strict;
  1         3  
  1         35  
2 1     1   5 use warnings;
  1         2  
  1         57  
3             package SMS::Send::Retarus;
4             $SMS::Send::Retarus::VERSION = '0.001';
5             # ABSTRACT: SMS::Send driver for the Retarus SMS for Applications webservice
6              
7 1     1   6 use Carp;
  1         2  
  1         83  
8 1     1   867 use HTTP::Tiny;
  1         58852  
  1         49  
9 1     1   606 use URI::Escape qw( uri_escape );
  1         1487  
  1         70  
10 1     1   594 use JSON::MaybeXS qw( decode_json encode_json JSON );
  1         6432  
  1         75  
11 1     1   698 use Try::Tiny;
  1         2327  
  1         77  
12             use Exception::Class (
13 1         11 'SMS::Send::Retarus::Exception' => {
14             fields => [ 'response', 'status' ]
15             },
16 1     1   644 );
  1         9802  
17              
18 1     1   367 use base 'SMS::Send::Driver';
  1         2  
  1         597  
19              
20              
21             sub new {
22 4     4 1 2514 my $class = shift;
23 4         8 my $self = { @_ };
24              
25             $self->{$_}
26             or croak "$_ missing"
27 4   100     50 for qw( _login _password );
28              
29 1         3 return bless $self, $class;
30             }
31              
32              
33             sub send_sms {
34 0     0 1   my ($self, %args) = @_;
35              
36 0           my $http = HTTP::Tiny->new(
37             default_headers => {
38              
39             # to ensure the response is JSON
40             'accept' => 'application/json; charset=utf-8',
41             'content-type' => 'application/json; charset=utf-8',
42             },
43             timeout => 3,
44             verify_ssl => 1,
45             );
46              
47             my %message = (
48             messages => [
49             {
50             text => $args{text},
51             recipients => [
52             {
53             dst => $args{to},
54             }
55 0           ],
56             }
57             ],
58             );
59              
60             # add all underscore args without the underscore
61             $message{$_} = $args{"_$_"}
62 0           for map { $_ =~ s/^_//; $_; }
  0            
  0            
63 0           grep { $_ =~ /^_/ } keys %args;
64              
65             my $response = $http->post(
66             'https://'
67             . uri_escape( $self->{_login} )
68             . ':'
69             . uri_escape( $self->{_password} )
70 0           . '@sms4a.retarus.com/rest/v1/jobs',
71             {
72             content => encode_json(\%message),
73             }
74             );
75              
76 0 0         if ( $response->{success} ) {
77 0           my $content;
78             try {
79 0     0     $content = decode_json( $response->{content} );
80             }
81             catch {
82 0     0     SMS::Send::Retarus::Exception->throw(
83             error => 'decoding of API response failed: ' . $_
84             );
85 0           };
86              
87             # return the API response which is a hashref which is always true and
88             # conforms to the SMS::Send::Driver API but still enables a user to
89             # get at additional data like the jobId
90 0           return $content;
91             }
92              
93             SMS::Send::Retarus::Exception->throw(
94             error => $response->{content},
95             status => $response->{status},
96             # try to decode the body as it might contain an API error
97 0     0     try { ( response => decode_json( $response->{content} ) ) },
98 0           );
99             }
100              
101              
102             1;
103              
104             __END__