blib/lib/SMS/Send/KR/Aligo.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 136 CC 14 SCAR 41.0
CC
14
Cov
0%
send_sms
110.0
new
30.0
BEGIN
2.0
CRAP 60.1
1 package SMS::Send::KR::Aligo;
2 # ABSTRACT: An SMS::Send driver for the smartsms.aligo.in SMS service
3  
4 use utf8;
5 use strict;
6 use warnings;
7  
8 our $VERSION = '0.005';
9  
10 use parent qw( SMS::Send::Driver );
11  
12 use HTTP::Tiny;
13 use JSON;
14  
15 our $URL = "https://apis.aligo.in";
16 our $AGENT = "SMS-Send-KR-Aligo/" . $SMS::Send::KR::Aligo::VERSION;
17 our $TIMEOUT = 3;
18 our $TYPE = "SMS";
19 our $DELAY = 0;
20  
21 sub new {
22 my $class = shift;
23 my %params = (
24 _url => $SMS::Send::KR::Aligo::URL,
25 _agent => $SMS::Send::KR::Aligo::AGENT,
26 _timeout => $SMS::Send::KR::Aligo::TIMEOUT,
27 _from => q{},
28 _type => $SMS::Send::KR::Aligo::TYPE,
29 _delay => $SMS::Send::KR::Aligo::DELAY,
30 _id => q{},
31 _api_key => q{},
32 @_,
33 );
34  
35 die "$class->new: _id is needed\n" unless $params{_id};
36 die "$class->new: _api_key is needed\n" unless $params{_api_key};
37 die "$class->new: _from is needed\n" unless $params{_from};
38 die "$class->new: _type is invalid\n"
39 unless $params{_type} && $params{_type} =~ m/^(SMS|LMS)$/i;
40  
41 my $self = bless \%params, $class;
42 return $self;
43 }
44  
45 sub send_sms {
46 my $self = shift;
47 my %params = (
48 _from => $self->{_from},
49 _type => $self->{_type} || "SMS",
50 _delay => $self->{_delay} || 0,
51 _subject => $self->{_subject},
52 _epoch => q{},
53 @_,
54 );
55  
56 my $text = $params{text};
57 my $to = $params{to};
58 my $from = $params{_from};
59 my $type = $params{_type};
60 my $delay = $params{_delay};
61 my $subject = $params{_subject};
62 my $epoch = $params{_epoch};
63  
64 my %ret = (
65 success => 0,
66 reason => q{},
67 detail => +{},
68 );
69  
70 $ret{reason} = "text is needed", return \%ret unless $text;
71 $ret{reason} = "to is needed", return \%ret unless $to;
72 $ret{reason} = "_type is invalid", return \%ret
73 unless $type && $type =~ m/^(SMS|LMS)$/i;
74  
75 my $http = HTTP::Tiny->new(
76 agent => $self->{_agent},
77 timeout => $self->{_timeout},
78 ) or $ret{reason} = "cannot generate HTTP::Tiny object", return \%ret;
79 my $url = sprintf "%s/send/", $self->{_url};
80  
81 #
82 # delay / send_time: reserve SMS
83 #
84 my $send_time_dt;
85 if ($delay) {
86 $send_time_dt = DateTime->now( time_zone => "Asia/Seoul" )->add( seconds => $delay );
87 }
88 if ($epoch) {
89 $send_time_dt = DateTime->from_epoch(
90 time_zone => "Asia/Seoul",
91 epoch => $epoch,
92 );
93 }
94  
95 #
96 # subject
97 #
98 undef $subject if $type =~ m/SMS/i;
99  
100 my %form = (
101 key => $self->{_api_key},
102 user_id => $self->{_id},
103 receiver => $to,
104 sender => $from,
105 title => $subject,
106 msg => $text,
107 msg_type => uc($type),
108 );
109 if ($send_time_dt) {
110 $form{rdate} = $send_time_dt->ymd(q{});
111 $form{rtime} = $send_time_dt->strftime("%H%M");
112 }
113 $form{$_} or delete $form{$_} for keys %form;
114  
115 my $res = $http->post_form( $url, \%form );
116 $ret{reason} = "cannot get valid response for POST request";
117 if ( $res && $res->{success} ) {
118 $ret{detail} = decode_json( $res->{content} );
119 if ( $ret{detail}{result_code} >= 0 ) {
120 $ret{success} = 1;
121 $ret{reason} = q{};
122 }
123 else {
124 $ret{reason} = $ret{detail}{message};
125 }
126 }
127 else {
128 $ret{detail} = $res;
129 $ret{reason} = "unknown error";
130 }
131  
132 return \%ret;
133 }
134  
135 1;
136  
137 #
138 # This file is part of SMS-Send-KR-Aligo
139 #
140 # This software is copyright (c) 2021 by Keedi Kim.
141 #
142 # This is free software; you can redistribute it and/or modify it under
143 # the same terms as the Perl 5 programming language system itself.
144 #
145  
146 __END__