File Coverage

blib/lib/SMS/MessageBird.pm
Criterion Covered Total %
statement 71 76 93.4
branch 6 8 75.0
condition 3 8 37.5
subroutine 19 20 95.0
pod 3 3 100.0
total 102 115 88.7


line stmt bran cond sub pod time code
1             package SMS::MessageBird;
2              
3 2     2   67982 use 5.006;
  2         7  
4 2     2   11 use strict;
  2         4  
  2         39  
5 2     2   9 use warnings;
  2         4  
  2         75  
6              
7 2     2   1407 use LWP::UserAgent;
  2         98038  
  2         71  
8 2     2   1407 use JSON;
  2         18535  
  2         13  
9              
10 2     2   1283 use SMS::MessageBird::API::SMS;
  2         6  
  2         61  
11 2     2   897 use SMS::MessageBird::API::Voice;
  2         5  
  2         63  
12 2     2   922 use SMS::MessageBird::API::Verify;
  2         6  
  2         61  
13 2     2   870 use SMS::MessageBird::API::HLR;
  2         6  
  2         56  
14 2     2   850 use SMS::MessageBird::API::Balance;
  2         6  
  2         54  
15 2     2   857 use SMS::MessageBird::API::Lookup;
  2         4  
  2         755  
16              
17             =head1 NAME
18              
19             SMS::MessageBird - SMS sending module that uses the MessageBird gateway.
20              
21             =head1 VERSION
22              
23             Version 0.04
24              
25             =cut
26              
27             our $VERSION = '0.04';
28              
29              
30             =head1 SYNOPSIS
31              
32             This module is a Perl interface for interacting with the MessageBird SMS
33             Gateway API.
34              
35             use SMS::MessageBird;
36              
37             my $messagebird = SMS::MessageBird->new(
38             api_key => 'test_abcdefghijklmnopqrstuvwxyz',
39             originator => 'Me!', # Specify here, or use the originator() method.
40             );
41              
42             # Optional. This can be updated at any time, but only needs to be set once.
43             $messagebird->originator('Me!');
44              
45             my $result = $messagebird->sms->send(
46             recipients => qw( 07123456789 ),
47             message => 'This is my SMS text', # synonym for 'body'
48             );
49              
50             =head1 DESCRIPTION
51              
52             This module provides a simple Perl interface to the MessageBird JSON API. It
53             deals with the JSON stuff allowing you to get back useful Perl data.
54              
55             To use this module you'll need an account with
56             L. Once you have that,
57             you can create an API key on your account and feed it to this module which will
58             authenticate with their API using that key.
59              
60             The methods implmented acceept the paramteres as named in the MessageBird API
61             documentation which can be found at the L.
62             If you're using this distribution you should be familiar with the API
63             documentation.
64              
65             =head2 API Modules
66              
67             This distribution provides several modules which are used by the
68             SMS::MessageBird object. Each module implements a section of the api.
69              
70             Use of the functionality of a given module, is done via the accessor for that
71             module, thus:
72              
73             my $result = $messagebird->sms->send(...);
74             my $balance = $messagebird->balance->get();
75             ...
76              
77             =head3 Available Modules
78              
79             =over
80              
81             =item sms
82              
83             This is the accessor for the L module. Used for
84             sending/receiving SMS messages.
85              
86             =item voice
87              
88             This is the accessor for the L module. Used for
89             sending/receiving Text-to-Voice messages.
90              
91             =item verify
92              
93             This is the accessor for the L module. Used to
94             implement the MessageBird number verification API.
95              
96             =item hlr
97              
98             This is the accessor for the L module. Used to send
99             Network Queries to mobile numbers.
100              
101             =item balance
102              
103             This is the accessor for the L module. Used to
104             retrieve your MessageBird account balance.
105              
106             =item lookup
107              
108             This is the accessor for the L module. Used to
109             validate phone numbers, and provide optional formats for that number.
110              
111             =back
112              
113              
114             =head1 METHODS
115              
116             =head2 new (contructor)
117              
118             In: %params - Various parameters for the API interface.
119              
120             Creates a new instance of SMS::MessageBird.
121              
122             =head3 Parameters
123              
124             Parmeters are passed to the contructor as a hash. Required / acceptable keys
125             are as follows:
126              
127             =over
128              
129             =item api_key
130              
131             Required. The MessageBird account API key used for authentication with
132             MessageBird's API.
133              
134             =item originator
135              
136             As per the MessageBird documentation, all sending functionality requires an
137             originator. This can be set once on the SMS::MessageBird object and passed to
138             all the module methods. This can be set later using the originator() mutator.
139              
140             =item api_url
141              
142             If for some reason you need to use some form of local HTTP proxy / forwarder
143             this parameter can be used to specifiy the alternate address. If it is omittied
144             the default is MessageBird's URL I.
145              
146             =back
147              
148             =cut
149              
150             sub new {
151 1     1 1 526 my ($package, %params) = @_;
152              
153 1 50 33     11 if (!%params || !exists $params{api_key} || !$params{api_key}) {
      33        
154 0         0 warn 'No API key suppied to SMS::MessageBird contructor';
155 0         0 return undef;
156             }
157              
158             my $self = bless {
159             module_data => {
160             api_key => $params{api_key},
161 1   50     5 api_url => 'https://rest.messagebird.com',
162             },
163             } => ($package || 'SMS::MessageBird');
164              
165              
166 1         2 for my $param (qw( originator api_url )) {
167             $self->{module_data}{$param}
168 2 100       11 = $params{$param} if defined $params{$param};
169             }
170              
171             # Make sure the api_url doesn't have a trailing slash.
172 1         3 $self->{module_data}{api_url} =~ s{/$}{};
173              
174 1         4 $self->_load_modules();
175              
176 1         3 return $self;
177             }
178              
179              
180             =head2 originator
181              
182             In: $originator (optional) - New originator to set.
183             Out: The currently set originator.
184              
185             Mutator for the originator parameter. This parameter is the displayed
186             "From" in the SMS. It can be a phone number (including country code) or an
187             alphanumeric string of up to 11 characters.
188              
189             This can be set for the lifetime of the object and used for all messages sent
190             using the instance or passed individually to each call.
191              
192             You can pass the originator param to the constructor rather than use this
193             mutator, but it's here in case you want to send 2 batches of SMS from differing
194             originiators using the same object.
195              
196             =cut
197              
198             sub originator {
199 1     1 1 967 my ($self, $originator) = @_;
200              
201             # Set the new originator if one was supplied and reload all the modules
202             # with the new data.
203 1 50       7 if ($originator) {
204 1         4 $self->{module_data}{originator} = $originator;
205 1         7 $self->_load_modules();
206             }
207              
208 1         6 return $self->{module_data}{originator};
209             }
210              
211              
212             =head2 api_url
213              
214             In: $api_url (optional) - New api_url to set.
215             Out: The currently set api_url.
216              
217             Mutator for the api_ul parameter. Should some form of network relay be required
218             this can be used to override the default I.
219              
220             =cut
221              
222             sub api_url {
223 2     2 1 5 my ($self, $api_url) = @_;
224              
225             # Set the new api_url if one was supplied and reload all the modules
226             # with the new data.
227 2 100       6 if ($api_url) {
228 1         3 $api_url =~ s{/$}{};
229 1         2 $self->{module_data}{api_url} = $api_url;
230 1         5 $self->_load_modules();
231             }
232              
233 2         14 return $self->{module_data}{api_url};
234             }
235              
236              
237              
238             # Internal method to load/reload the associated API modules.
239              
240 0           sub _load_modules {
241 4     4   7 my ($self) = @_;
242              
243 4         9 $self->{loaded_modules} = undef;
244              
245 4         16 my %modules = (
246             sms => 'SMS::MessageBird::API::SMS',
247             voice => 'SMS::MessageBird::API::Voice',
248             verify => 'SMS::MessageBird::API::Verify',
249             hlr => 'SMS::MessageBird::API::HLR',
250             balance => 'SMS::MessageBird::API::Balance',
251             lookup => 'SMS::MessageBird::API::Lookup',
252             );
253              
254 2     2   16 no strict 'refs';
  2         4  
  2         83  
255 2     2   12 no warnings 'redefine';
  2         4  
  2         279  
256              
257 4         16 while (my ($module_name, $module) = each %modules) {
258 24         90 *{"SMS::MessageBird::$module_name"} = sub {
259 0     0   0 $module->new( %{ $self->{module_data} } );
  0         0  
260 24         65 };
261 24         35 push @{ $self->{loaded_modules} }, $module;
  24         124  
262             }
263              
264 2     2   30 use warnings 'redefine';
  2         3  
  2         83  
265 2     2   10 use strict 'refs';
  2         12  
  2         133  
266             }
267              
268              
269             =head1 AUTHOR
270              
271             James Ronan, C<< >>
272              
273             =head1 BUGS
274              
275             Please report any bugs or feature requests to C,
276             or through the web interface at L.
277             I will be notified, and then you'll automatically be notified of progress on your
278             bug as I make changes.
279              
280             Alternatively you can raise an issue on the source code which is available on
281             L.
282              
283             =head1 LICENSE AND COPYRIGHT
284              
285             Copyright 2016 James Ronan.
286              
287             This library is free software; you can redistribute it and/or modify it under
288             the same terms as Perl itself.
289              
290             =cut
291              
292             1; # End of SMS::MessageBird