MessageBird.pm

filter: Statementstmt 94.8% 73 / 77 Branchbran 80.0% 8 / 10 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 95.0% 19 / 20 Podpod 100% 3 / 3 totaltotal 86.8% 106 / 122 CC 10 SCAR 11.8
CC
10
Cov
88%
new
9.4
originator
2.0
_load_modules
2.0
CRAP 3.3
« MessageBird/API/Voice.pm ↑ Summary MessageBird/API/Balance.pm »
1 package SMS::MessageBird;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use SMS::MessageBird::API::SMS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use SMS::MessageBird::API::Voice;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use SMS::MessageBird::API::Verify;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use SMS::MessageBird::API::HLR;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use SMS::MessageBird::API::Balance;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use SMS::MessageBird::API::Lookup;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 SMS::MessageBird - SMS sending module that uses the MessageBird gateway.
20  
21 =head1 VERSION
22  
23 Version 0.02
24  
25 =cut
26  
27 2 our $VERSION = '0.02';
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<MessageBird|https://www.messagebird.com/>. 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<MessageBird Developer Documentation|https://www.messagebird.com/en-gb/developers>.
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<SMS::MessageBird::API::SMS> module. Used for
84 sending/receiving SMS messages.
85  
86 =item voice
87  
88 This is the accessor for the L<SMS::MessageBird::API::Voice> module. Used for
89 sending/receiving Text-to-Voice messages.
90  
91 =item verify
92  
93 This is the accessor for the L<SMS::MessageBird::API::Verify> module. Used to
94 implement the MessageBird number verification API.
95  
96 =item hlr
97  
98 This is the accessor for the L<SMS::MessageBird::API::HLR> module. Used to send
99 Network Queries to mobile numbers.
100  
101 =item balance
102  
103 This is the accessor for the L<SMS::MessageBird::API::Balance> module. Used to
104 retrieve your MessageBird account balance.
105  
106 =item lookup
107  
108 This is the accessor for the L<SMS::MessageBird::API::Lookup> 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<https://rest.messagebird.com>.
145  
146 =back
147  
148 =cut
149  
150 sub new {
151 1 ▶ my ($package, %params) = @_;
Subroutine new: called CC=7 SCAR=22.4
Pod documented
152  
153 1 ▶ if (!%params || !exists $params{api_key} || !$params{api_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%params and exists $params{"api_key"} and $params{"api_key"}) 0 1 1
Condition33%2/6
%params and exists $params{"api_key"} and $params{"api_key"}
!ll&&!rl&&r
001
%params and exists $params{"api_key"}
!ll&&!rl&&r
001
MC/DC0%0/3
%params and exists $params{"api_key"} and $params{"api_key"}
%params exists $params{"api_key"} $params{"api_key"}
Truth table25%1/4
A and B and C
A= %paramsB= exists $params{"api_key"}C= $params{"api_key"}
ABCresult
0XX0
10X0
1100
1111
154 0 warn 'No API key suppied to SMS::MessageBird contructor';
155 0 return undef;
156 }
157  
158 my $self = bless {
159 module_data => {
160 api_key => $params{api_key},
161 1 ▶ api_url => 'https://rest.messagebird.com',
Condition50%1/2
$package || 'SMS::MessageBird'
l!l
10
MC/DC0%0/1
$package || 'SMS::MessageBird'
$package
Truth table50%1/2
A
A= $package
Aresult
11
00
162 },
163 } => ($package || 'SMS::MessageBird');
164  
165  
166 1 for my $param (qw( originator api_url )) {
167 $self->{module_data}{$param}
168 2 ▶ = $params{$param} if defined $params{$param};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{$param} 1 1 2
169 }
170  
171 # Make sure the api_url doesn't have a trailing slash.
172 1 $self->{module_data}{api_url} =~ s{/$}{};
173  
174 1 $self->_load_modules();
175  
176 1 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 ▶ my ($self, $originator) = @_;
Subroutine originator: called CC=2 SCAR=7.0
Pod documented
200  
201 # Set the new originator if one was supplied and reload all the modules
202 # with the new data.
203 1 ▶ if ($originator) {
Branch: 1 branch
BranchTrueFalseTotal
if ($originator) 1 0 1
204 1 $self->{module_data}{originator} = $originator;
205 1 $self->_load_modules();
206 }
207  
208 1 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<https://rest.messagebird.com>.
219  
220 =cut
221  
222 sub api_url {
223 2 ▶ my ($self, $api_url) = @_;
Subroutine api_url: called CC=2 SCAR=6.9
Pod documented
224  
225 # Set the new api_url if one was supplied and reload all the modules
226 # with the new data.
227 2 ▶ if ($api_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($api_url) 1 1 2
228 1 $api_url =~ s{/$}{};
229 1 $self->{module_data}{api_url} = $api_url;
230 1 $self->_load_modules();
231 }
232  
233 2 return $self->{module_data}{api_url};
234 }
235  
236  
237  
238 # Internal method to load/reload the associated API modules.
239  
240 sub _load_modules {
241 4 ▶ my ($self) = @_;
Subroutine _load_modules: called CC=2 SCAR=7.0
242  
243 4 $self->{loaded_modules} = undef;
244  
245 4 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 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
255 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
256  
257 4 ▶ while (my ($module_name, $module) = each %modules) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %modules) 24 4 28
258 24 *{"SMS::MessageBird::$module_name"} = sub {
259 0 ▶ $module->new( %{ $self->{module_data} } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
260 24 };
261 24 push @{ $self->{loaded_modules} }, $module;
262 }
263  
264 2 ▶ use warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
265 2 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
266 }
267  
268  
269 =head1 AUTHOR
270  
271 James Ronan, C<< <james at ronanweb.co.uk> >>
272  
273 =head1 BUGS
274  
275 Please report any bugs or feature requests to C<bug-sms-messagebird at rt.cpan.org>,
276 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SMS-MessageBird>.
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<GitHub|https://github.com/jamesronan/SMS-MessageBird>.
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 2 1; # End of SMS::MessageBird
« MessageBird/API/Voice.pm ↑ Summary MessageBird/API/Balance.pm »