File Coverage

blib/lib/SMS/MessageBird/API.pm
Criterion Covered Total %
statement 12 40 30.0
branch 0 12 0.0
condition 0 10 0.0
subroutine 4 10 40.0
pod 3 3 100.0
total 19 75 25.3


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