File Coverage

blib/lib/SMS/MessageBird/API.pm
Criterion Covered Total %
statement 18 50 36.0
branch 0 14 0.0
condition 0 10 0.0
subroutine 6 12 50.0
pod 3 3 100.0
total 27 89 30.3


line stmt bran cond sub pod time code
1             package SMS::MessageBird::API;
2              
3 2     2   880 use strict;
  2         11  
  2         62  
4 2     2   11 use warnings;
  2         3  
  2         57  
5              
6 2     2   1043 use Encode qw( encode_utf8 );
  2         21251  
  2         145  
7 2     2   15 use LWP::UserAgent;
  2         4  
  2         66  
8 2     2   17 use JSON;
  2         4  
  2         11  
9 2     2   192 use URI;
  2         5  
  2         1198  
10              
11             =head1 NAME
12              
13             SMS::MessageBird::API - Provides API integration base for SMS::MessageBird.
14              
15              
16             =head1 VERSION
17              
18             Version 0.03
19              
20             =cut
21              
22             our $VERSION = '0.03';
23              
24              
25             =head1 METHODS
26              
27              
28             =head2 new (contructor)
29              
30             In: %params - Various parameters for the API interface.
31              
32             Creates a new instance of SMS::MessageBird.
33              
34             =head3 Parameters
35              
36             Parmeters are passed to the contructor as a hash. Required / acceptable keys
37             are as follows:
38              
39             =over
40              
41             =item api_key
42              
43             Required. The MessageBird account API key used for authentication with
44             MessageBird's API.
45              
46             =item originator
47              
48             As per the MessageBird documentation, all sending functionality requires an
49             originator. This can be set once on the SMS::MessageBird object and passed to
50             all the module methods. This can be set later using the originator() mutator.
51              
52             =item api_url
53              
54             If for some reason you need to use some form of local HTTP proxy / forwarder
55             this parameter can be used to specifiy the alternate address. If it is omittied
56             the default is MessageBird's URL I.
57              
58             =back
59              
60             =cut
61              
62             sub new {
63 0     0 1   my ($package, %params) = @_;
64              
65 0 0 0       if (!%params || !exists $params{api_key} || !$params{api_key}) {
      0        
66 0           warn 'No API key suppied to SMS::MessageBird contructor';
67 0           return undef;
68             }
69              
70             my $self = bless {
71             api_key => $params{api_key},
72 0   0       } => ($package || 'SMS::MessageBird');
73              
74 0 0         $self->{originator} = $params{originator} if $params{originator};
75              
76             $self->{api_url}
77 0   0       = $params{api_url} || 'https://rest.messagebird.com';
78              
79             $self->{ua} = LWP::UserAgent->new(
80             agent => "Perl/SMS::MessageBird/$VERSION",
81             default_headers => HTTP::Headers->new(
82             'content-type' => 'application/json',
83             Accept => 'application/json',
84             Authorization => 'AccessKey ' . $self->{api_key},
85 0           ),
86             );
87              
88 0           return $self;
89             }
90              
91              
92             =head2 originator
93              
94             In: $originator (optional) - New originator to set.
95             Out: The currently set originator.
96              
97             Mutator for the originator parameter. This parameter is the displayed
98             "From" in the SMS. It can be a phone number (including country code) or an
99             alphanumeric string of up to 11 characters.
100              
101             This can be set for the lifetime of the object and used for all messages sent
102             using the instance or passed individually to each call.
103              
104             You can pass the originator param to the constructor rather than use this
105             mutator, but it's here in case you want to send 2 batches of SMS from differing
106             originiators using the same object.
107              
108             =cut
109              
110             sub originator {
111 0     0 1   my ($self, $originator) = @_;
112              
113 0 0         $self->{originator} = $originator if $originator;
114              
115 0           return $self->{originator};
116             }
117              
118              
119             =head2 api_url
120              
121             In: $api_url (optional) - New api_url to set.
122             Out: The currently set api_url.
123              
124             Mutator for the api_ul parameter. Should some form of network relay be required
125             this can be used to override the default I.
126              
127             =cut
128              
129             sub api_url {
130 0     0 1   my ($self, $api_url) = @_;
131              
132 0 0         if ($api_url) {
133 0           $api_url =~ s{/$}{};
134 0           $self->{api_url} = $api_url;
135             }
136              
137 0           return $self->{api_url};
138             }
139              
140              
141              
142             sub _api_request {
143 0     0     my ($self, $method, $endpoint, $data) = @_;
144              
145 0           my %request_params;
146 0 0         if ($data) {
147              
148             # For a GET request, we need to send the $data as request params.
149 0 0         if (lc $method eq 'get') {
150 0           my $get_url = URI->new($endpoint);
151 0           $get_url->query_form(%$data);
152 0           $endpoint = $get_url;
153              
154             # For the others POST, PUT, PATCH - send the data as payload.
155             } else {
156              
157 0           my $content_payload = JSON->new->encode($data);
158              
159             $request_params{'Content-Type'} = 'application/json;charset=utf-8',
160 0           $request_params{Content} = Encode::encode_utf8($content_payload);
161             }
162             }
163              
164 0           my $api_response = $self->{ua}->$method(
165             $self->_full_endpoint($endpoint),
166             %request_params,
167             );
168              
169             return {
170 0 0         ok => ($api_response->is_success) ? 1 : 0,
171             code => $api_response->code,
172             content => JSON->new->pretty(1)->decode($api_response->content),
173             };
174             }
175              
176             sub _full_endpoint {
177 0     0     my ($self, $endpoint) = @_;
178              
179 0           return $self->{api_url} . $endpoint;
180             }
181              
182             sub _no_param_supplied {
183 0     0     my ($self, $param) = @_;
184              
185             return {
186 0           http_code => undef,
187             ok => 0,
188             content => {
189             errors => [
190             { description => "No $param supplied" }
191             ],
192             },
193             };
194             }
195              
196              
197             =head1 AUTHOR
198              
199             James Ronan, C<< >>
200              
201             =head1 BUGS
202              
203             Please report any bugs or feature requests to C,
204             or through the web interface at L.
205             I will be notified, and then you'll automatically be notified of progress on your
206             bug as I make changes.
207              
208             Alternatively you can raise an issue on the source code which is available on
209             L.
210              
211             =head1 LICENSE AND COPYRIGHT
212              
213             Copyright 2016 James Ronan.
214              
215             This library is free software; you can redistribute it and/or modify it under
216             the same terms as Perl itself.
217              
218             =cut
219              
220             1;
221