| 1 | package SMS::MessageBird::API; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 2 | 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<https://rest.messagebird.com>. | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my ($package, %params) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if (!%params || !exists $params{api_key} || !$params{api_key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
%params and exists $params{"api_key"} and $params{"api_key"}
%params and exists $params{"api_key"}
MC/DC0%0/3
%params and exists $params{"api_key"} and $params{"api_key"}
%params
exists $params{"api_key"}
$params{"api_key"}
Truth table0%0/4
A and B and C
A= %paramsB= exists $params{"api_key"}C= $params{"api_key"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | } => ($package || 'SMS::MessageBird'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$package || 'SMS::MessageBird'
MC/DC0%0/1
$package || 'SMS::MessageBird'
$package
Truth table0%0/2
A
A= $package
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | $self->{originator} = $params{originator} if $params{originator}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | $self->{api_url} | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | = $params{api_url} || 'https://rest.messagebird.com'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$params{'api_url'} || 'https://rest.messagebird.com'
MC/DC0%0/1
$params{'api_url'} || 'https://rest.messagebird.com'
$params{'api_url'}
Truth table0%0/2
A
A= $params{'api_url'}
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | my ($self, $originator) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
originator: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | $self->{originator} = $originator if $originator; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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<https://rest.messagebird.com>. | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | sub api_url { | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | my ($self, $api_url) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
api_url: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | if ($api_url) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | my ($self, $method, $endpoint, $data) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_api_request: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | my %request_params; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if ($data) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | ok => ($api_response->is_success) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | code => $api_response->code, | ||||||||||||||||||||||||||||||||||||||||||
| 159 | content => JSON->new->pretty(1)->decode($api_response->content), | ||||||||||||||||||||||||||||||||||||||||||
| 160 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | sub _full_endpoint { | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | my ($self, $endpoint) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_full_endpoint: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | return $self->{api_url} . $endpoint; | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | sub _no_param_supplied { | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | my ($self, $param) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_no_param_supplied: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 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<< <james at ronanweb.co.uk> >> | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | Please report any bugs or feature requests to C<bug-sms-messagebird at rt.cpan.org>, | ||||||||||||||||||||||||||||||||||||||||||
| 191 | or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SMS-MessageBird>. | ||||||||||||||||||||||||||||||||||||||||||
| 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<GitHub|https://github.com/jamesronan/SMS-MessageBird>. | ||||||||||||||||||||||||||||||||||||||||||
| 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 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||