File Coverage

blib/lib/WebService/Mocean/Client.pm
Criterion Covered Total %
statement 28 51 54.9
branch 2 6 33.3
condition 0 8 0.0
subroutine 7 9 77.7
pod 0 2 0.0
total 37 76 48.6


line stmt bran cond sub pod time code
1             package WebService::Mocean::Client;
2              
3 11     11   93 use Carp;
  11         26  
  11         770  
4 11     11   79 use Moo;
  11         24  
  11         89  
5 11     11   4240 use Types::Standard qw(Str Ref);
  11         29  
  11         73  
6 11     11   11255 use Array::Utils qw(array_minus);
  11         4531  
  11         9435  
7              
8             with 'Role::REST::Client';
9              
10             our $VERSION = '0.05';
11              
12             has api_url => (
13             isa => Str,
14             is => 'rw',
15             default => sub { 'https://rest.moceanapi.com/rest/1' },
16             );
17              
18             has api_key => (
19             isa => Str,
20             is => 'rw',
21             required => 1,
22             );
23              
24             has api_secret => (
25             isa => Str,
26             is => 'rw',
27             required => 1,
28             );
29              
30             has '_response_status' => (
31             isa => Ref['HASH'],
32             is => 'ro',
33             init_arg => undef,
34             default => sub {
35             {
36             0 => 'OK. No error encountered.',
37             1 => 'Authorization failed. Invalid mocean-api-key or mocean-api-secret.',
38             2 => 'Insufficient balance. Not enough credit in the account to send to at least one of the receivers.',
39             4 => 'At least one of the destination numbers is not white listed.',
40             5 => 'At least one of the destination numbers is black listed.',
41             6 => 'No destination number specified.',
42             8 => 'Sender ID not found.',
43             9 => 'Invalid UDH field.',
44             10 => 'Invalid mclass field.',
45             17 => 'Invalid validity field.',
46             19 => 'Invalid character set or message body.',
47             20 => 'Insufficient headers for sending SMS.',
48             23 => 'Empty mocean-text.',
49             24 => 'Unknown error.',
50             26 =>
51             'Invalid schedule format. (Hint: must have leading zero for time.)',
52             27 => 'Max number of receivers in a single request reached. Too many receivers in mocean-to field.',
53             28 => 'Invalid destination number. Receiver is invalid after stripping all non-numerics.',
54             29 => 'Message body is too long.',
55             32 => 'Message throttled.',
56             34 => 'Unknown request.',
57             37 => 'Invalid sender length.',
58             40 => 'System down for maintenance.',
59             43 => 'SMS flooding detected.',
60             44 => 'Invalid Sender ID.',
61             45 => 'System error, please retry later.',
62             48 => 'At least one of the senders is black listed.',
63             49 => 'At least one of the senders is not white listed.',
64             50 => 'Inappropriate content detected.',
65             }
66             },
67             );
68              
69             has '_required_fields' => (
70             isa => Ref['HASH'],
71             is => 'ro',
72             init_arg => undef,
73             default => sub {
74             {
75             sms => [qw(mocean-from mocean-to mocean-text)],
76             'verify/req' => [qw(mocean-to mocean-brand)],
77             'verify/check' => [qw(mocean-reqid mocean-code)],
78             'report/message' => [qw(mocean-msgid)],
79             'account/balance' => [],
80             'account/pricing' => [],
81             }
82             },
83             );
84              
85             sub BUILD {
86 2     2 0 520 my ($self, $args) = @_;
87              
88 2         39 $self->server($args->{api_url});
89 2         102 $self->api_key($args->{api_key});
90 2         93 $self->api_secret($args->{api_secret});
91              
92 2         99 $self->set_persistent_header(
93             'User-Agent' => __PACKAGE__ . $WebService::Mocean::Client::VERSION
94             );
95              
96 2         285 return $self;
97             }
98              
99             sub request {
100 0     0 0 0 my ($self, $command, $queries, $method) = @_;
101              
102 0   0     0 $command ||= q||;
103 0   0     0 $queries ||= {};
104 0   0     0 $method ||= 'get';
105              
106 0         0 $self->_check_required_params($command, $queries);
107              
108 0         0 my $params = $self->_auth_params();
109 0         0 $queries = {%{$queries}, %{$params}};
  0         0  
  0         0  
110              
111             # In case the api_url was updated.
112 0         0 $self->server($self->api_url);
113              
114 0   0     0 my $response_format = $queries->{'mocean-resp-format'} || 'xml';
115              
116 0         0 $self->type(qq|application/$response_format|);
117              
118             # Do not append '/' at the end of URL. Otherwise you will get HTTP 406
119             # error.
120 0         0 my $path = "/$command";
121              
122 0         0 my $response;
123 0 0       0 if ($self->can($method)) {
124 0         0 $response = $self->$method($path, $queries);
125             }
126             else {
127 0         0 croak "No such HTTP method: $method";
128             }
129              
130 0         0 return $self->_response($response->data);
131             }
132              
133             sub _response {
134 0     0   0 my ($self, $response) = @_;
135              
136 0         0 my $response_code = $response->{'mocean-code'};
137 0         0 my $response_status = $self->_response_status->{$response_code};
138              
139 0         0 $response->{'mocean-code-status'} = $response_status;
140              
141 0         0 return $response;
142             }
143              
144             sub _auth_params {
145 1     1   37 my ($self) = @_;
146              
147             return {
148 1         56 'mocean-api-key' => $self->api_key,
149             'mocean-api-secret' => $self->api_secret,
150             };
151             }
152              
153             sub _check_required_params {
154 1     1   36 my ($self, $command, $params) = @_;
155              
156 1         8 my $required_fields = $self->_required_fields->{$command};
157              
158 1 50       4 croak "Missing or invalid command : $command"
159             if (!defined $required_fields);
160              
161 1         2 my @param_keys = keys %{$params};
  1         5  
162 1         3 my @missing = array_minus(@{$required_fields}, @param_keys);
  1         7  
163              
164 1 50       23 croak 'Missing required params: ' . join ', ', @missing
165             if (scalar @missing);
166             }
167              
168             1;