blib/lib/NSMS/API.pm

filter: Statementstmt 100% 74 / 74 Branchbran 50.0% 8 / 16 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 20 / 20 Podpod 100% 2 / 2 totaltotal 91.3% 105 / 115 CC 10 SCAR 9.6
CC
10
Cov
90%
send
6.8
_json_to_struct
3.1
auth
3.1
CRAP 2.6
↑ Summary
1  
2 package NSMS::API;
3  
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # ABSTRACT: API para enviar SMS através da NSMS (http://www.nsms.com.br/)
17 2 our $VERSION = '0.006'; # VERSION
18  
19  
20 has ua => (
21 is => 'rw',
22 isa => 'Object',
23 lazy => 1,
24 1 ▶ default => sub { LWP::UserAgent->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
25 2 );
26  
27  
28 2 has username => (
29 is => 'rw',
30 isa => 'Str',
31 required => 1
32 );
33  
34  
35 2 has password => (
36 is => 'rw',
37 isa => 'Str',
38 required => 1
39 );
40  
41  
42 2 has baseurl => (
43 is => 'rw',
44 isa => 'Str',
45 default => 'http://api.nsms.com.br/api',
46 );
47  
48  
49 2 has extra => (
50 is => 'rw',
51 isa => 'Str',
52 default => ''
53 );
54  
55  
56 2 has debug => (
57 is => 'rw',
58 isa => 'Bool',
59 default => 0
60 );
61  
62  
63 4 ▶subtype 'NSMS_Number' => as 'Str' => where { $_ =~ /^[0-9]{10}$/ } =>
Subroutine __ANON__: called CC=1 SCAR=0.0
64 2 ▶ message {"The number you provider, $_, was not a mobile number"};
Subroutine __ANON__: called CC=1 SCAR=0.0
65  
66 2 has to => (
67 is => 'rw',
68 isa => 'NSMS_Number'
69 );
70  
71  
72 3 ▶subtype 'NSMS_Message' => as 'Str' => where { length($_) < 140 } =>
Subroutine __ANON__: called CC=1 SCAR=0.0
73 1 ▶ message {"The lenght of message has more then 140 chars."};
Subroutine __ANON__: called CC=1 SCAR=0.0
74  
75 2 has text => (
76 is => 'rw',
77 isa => 'NSMS_Message'
78 );
79  
80 has url_auth => (
81 is => 'ro',
82 isa => 'Str',
83 lazy => 1,
84 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
85 1 my $self = shift;
86 1 join( '/', $self->baseurl, 'auth', $self->username, $self->password );
87 }
88 2 );
89  
90 has url_sendsms => (
91 is => 'ro',
92 isa => 'Str',
93 lazy => 1,
94 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
95 1 my $self = shift;
96 1 join( '/', $self->baseurl, 'get', 'json' )
97 . '?to=55'
98 . $self->to
99 . '&content='
100 . uri_escape( $self->text )
101 . '&extra='
102 . uri_escape ( $self->extra );
103  
104 }
105 2 );
106  
107  
108 2 has has_auth => (
109 is => 'rw',
110 isa => 'Bool',
111 default => 0
112 );
113  
114 2 ▶sub _json_to_struct {
Subroutine _json_to_struct: called CC=3 SCAR=11.3
115 2 my ( $self, $ret ) = @_;
116 2 ▶ $ret = $ret->content if ref($ret) eq 'HTTP::Response';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ret eq "HTTP::Response" 2 0 2
117 2 my $st = decode_json($ret);
118 2 ▶ print Dumper($st) if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 2 2
119 2 return $st;
120 }
121  
122  
123 1 ▶sub auth {
Subroutine auth: called CC=3 SCAR=11.2
Pod documented
124 1 my $self = shift;
125 1 ▶ warn $self->url_auth if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
126 1 my $content = $self->ua->get( $self->url_auth );
127 1 my $ret = $self->_json_to_struct($content);
128 1 ▶ return '' unless $ret->{sms}{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $ret->{"sms"}{"ok"} 0 1 1
129 1 $self->has_auth(1);
130 1 return $ret->{sms}{ok};
131 }
132  
133  
134 1 ▶sub send {
Subroutine send: called CC=6 SCAR=19.1
Pod documented
135 1 my ( $self, $to, $text ) = @_;
136 1 ▶ $self->to($to) if $to;
Branch: 1 branch
BranchTrueFalseTotal
if $to 0 1 1
137 1 ▶ $self->text($text) if $text;
Branch: 1 branch
BranchTrueFalseTotal
if $text 0 1 1
138 1 ▶ $self->auth unless $self->has_auth;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_auth 0 1 1
139 1 ▶ warn $self->url_sendsms if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
140 1 my $content = $self->ua->get( $self->url_sendsms );
141 1 my $ret = $self->_json_to_struct($content);
142 1 ▶ return $ret->{sms}{ok} || '';
Condition50%1/2
$ret->{'sms'}{'ok'} || ''
l!l
10
MC/DC0%0/1
$ret->{'sms'}{'ok'} || ''
$ret->{'sms'}{'ok'}
Truth table50%1/2
A
A= $ret->{'sms'}{'ok'}
Aresult
11
00
143 }
144  
145 2 1;
146  
147  
148 __END__
↑ Summary