File Coverage

blib/lib/WebService/Mocean/Client.pm
Criterion Covered Total %
statement 23 44 52.2
branch 2 6 33.3
condition 0 8 0.0
subroutine 6 8 75.0
pod 0 2 0.0
total 31 68 45.5


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