lib/Net/API/RPX.pm

filter: Statementstmt 100% 73 / 73 Branchbran 100% 16 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 18 / 18 Podpod 100% 4 / 4 totaltotal 100% 111 / 111 SCAR 22.0
CC
9
Cov
100%
map
3.0
unmap
3.0
_fetch
3.0
CRAP 9.0
« blib/lib/Net/API/RPX.pm ↑ Summary blib/lib/Net/API/RPX/Exception/Network.pm »
1 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Net::API::RPX;
6  
7 # ABSTRACT: Perl interface to Janrain's RPX service
8  
9 3 our $VERSION = '1.000000';
10  
11 3 our $AUTHORITY = 'cpan:KONOBI'; # AUTHORITY
12  
13 3 ▶use Moose qw( has );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Net::API::RPX::Exception::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Net::API::RPX::Exception::Network;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Net::API::RPX::Exception::Service;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22  
23  
24  
25  
26  
27  
28 3 has api_key => (
29 is => 'rw',
30 isa => 'Str',
31 required => 1,
32 );
33  
34  
35  
36  
37  
38  
39  
40 3 has base_url => (
41 is => 'rw',
42 isa => 'Str',
43 required => 1,
44 lazy => 1,
45 default => 'https://rpxnow.com/api/v2/',
46 );
47  
48  
49  
50  
51  
52  
53  
54  
55 3 has ua => (
56 is => 'rw',
57 isa => 'Object',
58 required => 1,
59 lazy => 1,
60 builder => '_build_ua',
61 );
62  
63 sub _build_ua {
64 13 ▶ my ($self) = @_;
Subroutine _build_ua: called CC=1 SCAR=0.0
65 13 return LWP::UserAgent->new( agent => $self->_agent_string );
66 }
67  
68 has _agent_string => (
69 is => 'rw',
70 isa => 'Str',
71 required => 1,
72 lazy => 1,
73 12 ▶ default => sub { 'net-api-rpx-perl/' . $Net::API::RPX::VERSION },
Subroutine __ANON__: called CC=1 SCAR=0.0
74 3 );
75  
76 3 __PACKAGE__->meta->make_immutable;
77 3 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90 sub auth_info {
91 8 ▶ my ( $self, $opts ) = @_;
Subroutine auth_info: called CC=2 SCAR=6.9
Pod documented
92 Net::API::RPX::Exception::Usage->throw(
93 ident => 'auth_info_usage_needs_token',
94 message => 'Token is required',
95 required_parameter => 'token',
96 method_name => '->auth_info',
97 package => __PACKAGE__,
98 signature => '{ token => \'authtoken\' }',
99 8 ▶ ) if !exists $opts->{token};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"token"} 2 6 8
100 6 return $self->_fetch( 'auth_info', $opts );
101 }
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113 sub map { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
114 6 ▶ my ( $self, $opts ) = @_;
Subroutine map: called CC=3 SCAR=11.0
Pod documented
115 Net::API::RPX::Exception::Usage->throw(
116 ident => 'map_usage_needs_identifier',
117 message => 'Identifier is required',
118 required_parameter => 'identifier',
119 method_name => '->map',
120 package => __PACKAGE__,
121 signature => '{ identifier => \'some.open.id\', primary_key => 12 }',
122 6 ▶ ) if !exists $opts->{identifier};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"identifier"} 2 4 6
123  
124 Net::API::RPX::Exception::Usage->throw(
125 ident => 'map_usage_needs_primary_key',
126 message => 'Primary Key is required',
127 required_parameter => 'primary_key',
128 method_name => '->map',
129 package => __PACKAGE__,
130 signature => '{ identifier => \'some.open.id\', primary_key => 12 }',
131 4 ▶ ) if !exists $opts->{primary_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"primary_key"} 2 2 4
132 2 $opts->{primaryKey} = delete $opts->{primary_key};
133  
134 2 return $self->_fetch( 'map', $opts );
135 }
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147 sub unmap {
148 6 ▶ my ( $self, $opts ) = @_;
Subroutine unmap: called CC=3 SCAR=11.0
Pod documented
149 Net::API::RPX::Exception::Usage->throw(
150 ident => 'unmap_usage_needs_identifier',
151 message => 'Identifier is required',
152 required_parameter => 'identifier',
153 method_name => '->unmap',
154 package => __PACKAGE__,
155 signature => '{ identifier => \'some.open.id\', primary_key => 12 }',
156 6 ▶ ) if !exists $opts->{identifier};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"identifier"} 2 4 6
157  
158 Net::API::RPX::Exception::Usage->throw(
159 ident => 'unmap_usage_needs_primay_key',
160 message => 'Primary Key is required',
161 required_parameter => 'primary_key',
162 method_name => '->unmap',
163 package => __PACKAGE__,
164 signature => '{ identifier => \'some.open.id\', primary_key => 12 }',
165 4 ▶ ) if !exists $opts->{primary_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"primary_key"} 2 2 4
166  
167 2 $opts->{primaryKey} = delete $opts->{primary_key};
168  
169 2 return $self->_fetch( 'unmap', $opts );
170 }
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182 sub mappings {
183 4 ▶ my ( $self, $opts ) = @_;
Subroutine mappings: called CC=2 SCAR=6.9
Pod documented
184 Net::API::RPX::Exception::Usage->throw(
185 ident => 'mappings_usage_needs_primary_key',
186 message => 'Primary Key is required',
187 required_parameter => 'primary_key',
188 method_name => '->mappings',
189 package => __PACKAGE__,
190 signature => '{ primary_key => 12 }',
191 4 ▶ ) if !exists $opts->{primary_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"primary_key"} 2 2 4
192  
193 2 $opts->{primaryKey} = delete $opts->{primary_key};
194  
195 2 return $self->_fetch( 'mappings', $opts );
196 }
197  
198 3 my $rpx_errors = {
199 -1 => 'Service Temporarily Unavailable',
200 0 => 'Missing parameter',
201 1 => 'Invalid parameter',
202 2 => 'Data not found',
203 3 => 'Authentication error',
204 4 => 'Facebook Error',
205 5 => 'Mapping exists',
206 };
207  
208 sub _fetch {
209 12 ▶ my ( $self, $uri_part, $opts ) = @_;
Subroutine _fetch: called CC=3 SCAR=11.0
210  
211 12 my $uri = URI->new( $self->base_url . $uri_part );
212 my $res = $self->ua->post(
213 $uri => {
214 12 %{$opts},
215 apiKey => $self->api_key,
216 format => 'json',
217 },
218 );
219  
220 12 ▶ if ( !$res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 2 10 12
221 2 Net::API::RPX::Exception::Network->throw(
222 ident => '_fetch_network_failure',
223 message => 'Could not contact RPX: ' . $res->status_line(),
224 ua_result => $res,
225 status_line => $res->status_line,
226 );
227 }
228  
229 10 my $result = decode_json( $res->content );
230 10 ▶ if ( $result->{'stat'} ne 'ok' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"stat"} ne "ok") 2 8 10
231 2 my $err = $result->{'err'};
232 Net::API::RPX::Exception::Service->throw(
233 ident => '_fetch_service_error',
234 data => $result,
235 status => $result->{'stat'},
236 rpx_error => $result->{'err'},
237 rpx_error_code => $result->{err}->{code},
238 rpx_error_message => $result->{err}->{msg},
239 message => 'RPX returned error of type \'' . $rpx_errors->{ $err->{code} } . '\' with message: ' . $err->{msg},
240 2 );
241 }
242 8 delete $result->{'stat'};
243 8 return $result;
244 }
245  
246 3 1; # End of Net::API::RPX
247  
248 __END__
249  
250 =pod
251  
252 =encoding UTF-8
253  
254 =head1 NAME
255  
256 Net::API::RPX - Perl interface to Janrain's RPX service
257  
258 =head1 VERSION
259  
260 version 1.000000
261  
262 =head1 SYNOPSIS
263  
264 use Net::API::RPX;
265  
266 my $rpx = Net::API::RPX->new({ api_key => '<your_api_key_here>' });
267  
268 $rpx->auth_info({ token => $token });
269  
270 =head1 DESCRIPTION
271  
272 This module is a simple wrapper around Janrain's RPX service. RPX provides a single method for
273 dealing with third-party authentication.
274  
275 See L<http://www.rpxnow.com> for more details.
276  
277 For specific information regarding the RPX API and method arguments, please refer to
278 L<https://rpxnow.com/docs>.
279  
280 =head1 METHODS
281  
282 =head2 C<auth_info>
283  
284 my $user_data = $rpx->auth_info({ token => $params{token} });
285  
286 Upon redirection back from RPX, you will be supplied a token to use for verification. Call
287 auth_info to verify the authenticity of the token and gain user details.
288  
289 'token' argument is required, 'extended' argument is optional.
290  
291 =head2 C<map>
292  
293 $rpx->map({ identifier => 'yet.another.open.id', primary_key => 12 });
294  
295 This method allows you to map more than one 'identifier' to a user.
296  
297 'identifier' argument is required, 'primary_key' argument is required, 'overwrite' is optional.
298  
299 =head2 C<unmap>
300  
301 $rpx->unmap({ identifier => 'yet.another.open.id', primary_key => 12 });
302  
303 This is the inverse of 'map'.
304  
305 'identifier' argument is required, 'primary_key' argument is required.
306  
307 =head2 C<mappings>
308  
309 my $data = $rpx->mappings({ primary_key => 12 });
310  
311 This method returns information about the identifiers associated with a user.
312  
313 'primary_key' argument is required.
314  
315 =head1 ATTRIBUTES
316  
317 =head2 C<api_key>
318  
319 This is the api_key provided by Janrain to interface with RPX. You will need to sign up to RPX
320 to get one of these.
321  
322 =head2 C<base_url>
323  
324 This is the base URL that is used to make API calls against. It defaults to the RPX v2 API.
325  
326 =head2 C<ua>
327  
328 This is a LWP::UserAgent object. You may override it if you require more fine grain control
329 over remote queries.
330  
331 =head1 TEST COVERAGE
332  
333 This distribution is heavily unit and system tested for compatibility with
334 L<< <Test::Builder>|Test::Builder >>. If you come across any bugs, please send me or
335 submit failing tests to Net-API-RPX RT queue. Please see the 'SUPPORT' section below on
336 how to supply these.
337  
338 ---------------------------- ------ ------ ------ ------ ------ ------ ------
339 File stmt bran cond sub pod time total
340 ---------------------------- ------ ------ ------ ------ ------ ------ ------
341 blib/lib/Net/API/RPX.pm 100.0 100.0 n/a 100.0 100.0 100.0 100.0
342 Total 100.0 100.0 n/a 100.0 100.0 100.0 100.0
343 ---------------------------- ------ ------ ------ ------ ------ ------ ------
344  
345 =head1 SEE ALSO
346  
347 L<http://www.janrain.com/>, L<http://www.rpxnow.com/>
348  
349 =head1 AUTHORS
350  
351 =over 4
352  
353 =item *
354  
355 Scott McWhirter <konobi@cpan.org>
356  
357 =item *
358  
359 Kent Fredric <kentnl@cpan.org>
360  
361 =back
362  
363 =head1 COPYRIGHT AND LICENSE
364  
365 This software is Copyright (c) 2015 by Cloudtone Studios.
366  
367 This is free software, licensed under:
368  
369 The (three-clause) BSD License
370  
371 =cut
« blib/lib/Net/API/RPX.pm ↑ Summary blib/lib/Net/API/RPX/Exception/Network.pm »