blib/lib/XRI/Resolution/Lite.pm

filter: Statementstmt 85.4% 53 / 62 Branchbran 25.0% 4 / 16 Conditioncond 41.1% 7 / 17 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 65.8% 77 / 117 CC 16 SCAR 26.9
CC
16
Cov
67%
resolve
27.1
new
7.0
BEGIN
1.0
CRAP 14.7
↑ Summary
1 package XRI::Resolution::Lite;
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 2 ▶use parent qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 __PACKAGE__->mk_accessors(qw/resolver ua parser/);
8  
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 XRI::Resolution::Lite - The Lightweight client module for XRI Resolution
18  
19 =head1 VERSION
20  
21 version 0.04
22  
23 =cut
24  
25 2 our $VERSION = '0.04';
26  
27 2 my %param_map = (
28 format => '_xrd_r',
29 type => '_xrd_t',
30 media => '_xrd_m',
31 );
32  
33 =head1 SYNOPSIS
34  
35 use XML::LibXML::XPathContext;
36 use XRI::Resolution::Lite;
37  
38 my $r = XRI::Resolution::Lite->new;
39 my $xrds = $r->resolve('=zigorou'); ### XML::LibXML::Document
40 my $ctx = XML::LibXML::XPathContext->new($xrds);
41 my @services = $ctx->findnodes('//Service');
42  
43 =head1 METHODS
44  
45 =head2 new
46  
47 =over 2
48  
49 =item $args
50  
51 This param must be HASH reference. Available 2 fields.
52  
53 =over 2
54  
55 =item ua
56  
57 (Optional) L<LWP::UserAgent> object or its inheritance.
58  
59 =item resolver
60  
61 (Optional) URI string of XRI Proxy Resolver.
62 If this param is omitted, using XRI Global Proxy Resolver, "http://xri.net/", as resolver.
63  
64 =back
65  
66 =back
67  
68 =cut
69  
70 1 ▶sub new {
Subroutine new: called CC=5 SCAR=19.4
Pod documented
71 1 my ( $class, $args ) = @_;
72  
73 1 ▶ $args ||= +{};
Condition50%1/2
$args ||= {}
l!l
01
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
74 $args = +{
75 ua => $args->{ua} || LWP::UserAgent->new,
76 resolver => ( $args->{resolver} )
77 ? ( UNIVERSAL::isa( $args->{resolver}, 'URI' )
78 ? $args->{resolver}
79 1 ▶ : URI->new( $args->{resolver} ) )
Branch: 2 branches
BranchTrueFalseTotal
$args->{'resolver'} ? : 0 1 1
UNIVERSAL::isa($args->{'resolver'}, 'URI') ? : 0 0 0
Condition33%1/3
$args->{'ua'} || 'LWP::UserAgent'->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{'ua'} || 'LWP::UserAgent'->new
$args->{'ua'} 'LWP::UserAgent'->new
Truth table33%1/3
A || B
A= $args->{'ua'}B= 'LWP::UserAgent'->new
ABresult
1X1
011
000
80 : URI->new('http://xri.net/'),
81 parser => XML::LibXML->new,
82 };
83  
84 1 my $self = $class->SUPER::new($args);
85 1 return $self;
86 }
87  
88 =head2 resolve($qxri, \%params, \%media_flags)
89  
90 When type parameter is substituted "application/xrds+xml" or "application/xrd+xml", the result would be returned as L<XML::LibXML::Document> object.
91 Substituted "text/uri-list" to type parameter, the result would be returned as url list ARRAY or ARRAYREF.
92  
93 =over 2
94  
95 =item $qxri
96  
97 Query XRI string. For example :
98  
99 =zigorou
100 @linksafe
101 @id*zigorou
102  
103 =item $params
104  
105 This param must be HASH reference. Available 3 fields.
106 See Section 3.3 of XRI Resolution 2.0.
107 L<http://docs.oasis-open.org/xri/xri-resolution/2.0/specs/cd03/xri-resolution-V2.0-cd-03.html#_Ref129424065>
108  
109 =over 2
110  
111 =item format
112  
113 Resolution Output Format. This param would be '_xrd_r' query parameter.
114  
115 =item type
116  
117 Service Type. This param would be '_xrd_t' query parameter.
118  
119 =item media
120  
121 Service Media Type. This param would be '_xrd_m' query parameter.
122  
123 =back
124  
125 =item $media_flags
126  
127 If you want to specify flag on or off, then substitute to 1 as true, 0 as false.
128  
129 =over 2
130  
131 =item https
132  
133 Specifies use of HTTPS trusted resolution. default value is 0.
134  
135 =item saml
136  
137 Specifies use of SAML trusted resolution. default value is 0.
138  
139 =item refs
140  
141 Specifies whether Refs should be followed during resolution (by default they are followed), default value is 1.
142  
143 =item sep
144  
145 Specifies whether service endpoint selection should be performed. default value is 0.
146  
147 =item nodefault_t
148  
149 Specifies whether a default match on a Type service endpoint selection element is allowed. default value is 1.
150  
151 =item nodefault_p
152  
153 Specifies whether a default match on a Path service endpoint selection element is allowed. default value is 1.
154  
155 =item nodefault_m
156  
157 Specifies whether a default match on a MediaType service endpoint selection element is allowed. default value is 1.
158  
159 =item uric
160  
161 Specifies whether a resolver should automatically construct service endpoint URIs. default value is 0.
162  
163 =item cid
164  
165 Specifies whether automatic canonical ID verification should performed. default value is 1
166  
167 =back
168  
169 =back
170  
171 =cut
172  
173 1 ▶sub resolve {
Subroutine resolve: called CC=12 SCAR=33.0
Pod documented
174 1 my ( $self, $qxri, $params, $media_flags ) = @_;
175  
176 1 ▶ $params ||= {};
Condition50%1/2
$params ||= {}
l!l
01
MC/DC0%0/1
$params ||= {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
177 1 ▶ $media_flags ||= {};
Condition50%1/2
$media_flags ||= {}
l!l
01
MC/DC0%0/1
$media_flags ||= {}
$media_flags
Truth table50%1/2
A
A= $media_flags
Aresult
11
00
178  
179 1 $qxri =~ s|^xri://||; ### normalize
180  
181 1 my %query = ();
182 %query = (
183 _xrd_r => 'application/xrds+xml',
184 0 map { ( $param_map{$_}, $params->{$_} ) } keys %$params
185 );
186  
187 1 my %flags = (
188 https => 0,
189 saml => 0,
190 refs => 1,
191 sep => 0,
192 nodefault_t => 1,
193 nodefault_p => 1,
194 nodefault_m => 1,
195 uric => 0,
196 cid => 1,
197 );
198  
199 $query{'_xrd_r'} .=
200 9 ▶ ';' . join ';' => map { $_->[0] . '=' . $_->[1] ? 'true' : 'false' }
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] . '=' . $_->[1] ? : 9 0 9
201 1 ▶ map { [ $_, $media_flags->{$_} || $flags{$_} ] }
Condition66%2/3
$media_flags->{$_} || $flags{$_}
l!l&&r!l&&!r
054
MC/DC50%1/2
$media_flags->{$_} || $flags{$_}
$media_flags->{$_} $flags{$_}
Truth table66%2/3
A || B
A= $media_flags->{$_}B= $flags{$_}
ABresult
1X1
011
000
202 keys %flags;
203  
204 1 my $hxri = $self->resolver->clone;
205 1 $hxri->path($qxri);
206 1 $hxri->query_form(%query);
207  
208 1 my $req = HTTP::Request->new( GET => $hxri );
209 1 ▶ $req->header( Accept => $params->{type} || 'application/xrds+xml' );
Condition50%1/2
$params->{'type'} || 'application/xrds+xml'
l!l
01
MC/DC0%0/1
$params->{'type'} || 'application/xrds+xml'
$params->{'type'}
Truth table50%1/2
A
A= $params->{'type'}
Aresult
11
00
210  
211 1 my ( $res, $e );
212  
213 1 eval { $res = $self->ua->request($req); };
214 1 ▶ if ( $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e = $@) 0 1 1
215 0 $@ = undef;
216 0 croak($e);
217 }
218  
219 1 ▶ croak( $res->status_line ) unless ( $res->is_success ); ### HTTP error
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 1 0 1
220 0 ▶ croak( $res->content )
Branch: 1 branch
BranchTrueFalseTotal
if $res->header("Content-Type") =~ m[^text/plain] 0 0 0
221 if ( $res->header('Content-Type') =~ m#^text/plain# )
222 ; ### Invalid Content-Type
223  
224 0 ▶ unless ( defined $params->{format} && $params->{format} eq 'text/uri-list' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{'format'} and $params->{'format'} eq 'text/uri-list') { } 0 0 0
Condition0%0/3
defined $params->{"format"} and $params->{"format"} eq "text/uri-list"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $params->{"format"} and $params->{"format"} eq "text/uri-list"
defined $params->{"format"} $params->{"format"} eq "text/uri-list"
Truth table0%0/3
A and B
A= defined $params->{"format"}B= $params->{"format"} eq "text/uri-list"
ABresult
0X0
100
111
225 { ## XRDS or XRD format
226 0 my $doc = $self->parser->parse_string( $res->content );
227 0 return $doc;
228 }
229 else { ## URL List format
230 0 my @url_list = split "\n" => $res->content;
231 0 ▶ wantarray ? @url_list : \@url_list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
232 }
233 }
234  
235 =head1 SEE ALSO
236  
237 =over 2
238  
239 =item http://docs.oasis-open.org/xri/xri-resolution/2.0/specs/cd03/xri-resolution-V2.0-cd-03.html
240  
241 There are XRI Resolution spec in OASIS.
242  
243 =back
244  
245 =head1 AUTHOR
246  
247 Toru Yamaguchi, C<< <zigorou@cpan.org> >>
248  
249 =head1 BUGS
250  
251 Please report any bugs or feature requests to
252 C<bug-xri-resolution-lite@rt.cpan.org>, or through the web interface at
253 L<http://rt.cpan.org>. I will be notified, and then you'll automatically be
254 notified of progress on your bug as I make changes.
255  
256 =head1 COPYRIGHT & LICENSE
257  
258 Copyright 2008 Toru Yamaguchi, All Rights Reserved.
259  
260 This program is free software; you can redistribute it and/or modify it
261 under the same terms as Perl itself.
262  
263 =cut
264  
265 2 1; # End of XRI::Resolution::Lite
↑ Summary