API/File/Whois.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 81 CC 5 SCAR 18.4
CC
5
Cov
0%
parse
12.0
_build_content
12.0
author
2.0
CRAP 6.3
1 package CPAN::Index::API::File::Whois;
2  
3 our $VERSION = '0.008';
4  
5 use strict;
6 use warnings;
7  
8 use XML::Twig;
9 use namespace::autoclean;
10 use Moose;
11 use MooseX::ClassAttribute;
12  
13 with qw(
14 CPAN::Index::API::Role::Writable
15 CPAN::Index::API::Role::Readable
16 CPAN::Index::API::Role::Clonable
17 CPAN::Index::API::Role::HavingGeneratedBy
18 );
19  
20 class_has _field_map => (
21 is => 'bare',
22 isa => 'HashRef',
23 traits => ['Hash'],
24 handles => {
25 _original_fields => 'keys',
26 _name_for_orig_field => 'get',
27 },
28 default => sub { {
29 asciiname => 'ascii_name',
30 fullname => 'full_name',
31 email => 'email',
32 has_cpandir => 'has_cpandir',
33 homepage => 'homepage',
34 id => 'cpanid',
35 info => 'info',
36 type => 'type',
37 } },
38 );
39  
40 has authors => (
41 is => 'bare',
42 isa => 'ArrayRef[HashRef]',
43 default => sub { [] },
44 traits => ['Array'],
45 handles => {
46 authors => 'elements',
47 add_author => 'push',
48 author_count => 'count',
49 },
50 );
51  
52 sub author
53 {
54 my ($self, $name) = @_;
55 return first { $_->{cpanid} eq $name } $self->authors;
56 }
57  
58 sub parse {
59 my ( $self, $content ) = @_;
60  
61 my $twig = XML::Twig->new;
62 my $xml = $twig->parse($content);
63 my $root = $xml->root;
64  
65 my @authors;
66  
67 foreach my $author ( $xml->root->children('cpanid') ) {
68 my %data;
69  
70 foreach my $field ( $self->_original_fields ) {
71 if ( my $elt = $author->first_child($field) ) {
72 my $elt_text = $elt->text eq '' ? undef : $elt->text;
73 $data{ $self->_name_for_orig_field($field) } = $elt_text;
74 }
75 }
76  
77 push @authors, \%data;
78 }
79  
80 return (
81 last_generated => $root->att('last-generated'),
82 generated_by => $root->att('generated-by'),
83 authors => \@authors,
84 );
85 }
86  
87 sub _build_content {
88 my $self = shift;
89  
90 my $twig = XML::Twig->new( pretty_print => 'indented' );
91 $twig->parse(q[<?xml version="1.0" encoding="UTF-8"?><cpan-whois xmlns='http://www.cpan.org/xmlns/whois'/>]);
92 $twig->root->set_att(
93 'last-generated' => $self->last_generated,
94 'generated-by' => $self->generated_by,
95 );
96  
97 foreach my $author ($self->authors) {
98 my $elt_cpanid = XML::Twig::Elt->new('cpanid');
99  
100 foreach my $name ( $self->_original_fields ) {
101 my $name_for_orig_field = $self->_name_for_orig_field($name);
102 if ( exists $author->{$name_for_orig_field} ) {
103 my $elt_attribute = XML::Twig::Elt->new($name);
104 $elt_attribute->set_text(
105 defined $author->{$name_for_orig_field}
106 ? $author->{$name_for_orig_field} : ''
107 );
108 $elt_attribute->paste( last_child => $elt_cpanid );
109 }
110 }
111  
112 $elt_cpanid->paste( last_child => $twig->root );
113 }
114  
115 return $twig->sprint;
116 }
117  
118 sub default_location { 'authors/00whois.xml' }
119  
120 __PACKAGE__->meta->make_immutable;
121  
122 =pod
123  
124 =encoding UTF-8
125  
126 =head1 NAME
127  
128 CPAN::Index::File::Whois - Interface to C<00whois.xml>.
129  
130 =head1 SYNOPSIS
131  
132 my $mailrc = CPAN::Index::File::Whois->parse_from_repo_uri(
133 'http://cpan.perl.org'
134 );
135  
136 foreach my $author ($mailrc->authors) {
137 ... # do something
138 }
139  
140 =head1 DESCRIPTION
141  
142 This is a class to read and write C<01mailrc.txt>.
143  
144 =head1 METHODS
145  
146 =head2 authors
147  
148 List of hashrefs containing author data. The structure of the hashrefs is
149 as follows:
150  
151 =over
152  
153 =item cpanid
154  
155 CPAN id of the author, required.
156  
157 =item full_name
158  
159 Author's full name. Can be an empty string.
160  
161 =item ascii_name
162  
163 Author's full name, but containing only ASCII characters.
164  
165 =item email
166  
167 Author's email.
168  
169 =item has_cpandir
170  
171 Boolean - true if the author has a cpan directory.
172  
173 =item homepage
174  
175 Author's homepage.
176  
177 =item ino
178  
179 Additional information about the author.
180  
181 =item type
182  
183 Author type, usually C<author>.
184  
185 =back
186  
187 =head2 authors_count
188  
189 Number of authors in this file.
190  
191 =head2 author
192  
193 Method that fetches the entry for a given cpanid.
194  
195 =head2 parse
196  
197 Parses the file and returns its representation as a data structure.
198  
199 =head2 default_location
200  
201 Default file location - C<authors/00whois.xml>.
202  
203 =head1 METHODS FROM ROLES
204  
205 =over
206  
207 =item <CPAN::Index::API::Role::Readable/read_from_string>
208  
209 =item <CPAN::Index::API::Role::Readable/read_from_file>
210  
211 =item <CPAN::Index::API::Role::Readable/read_from_tarball>
212  
213 =item <CPAN::Index::API::Role::Readable/read_from_repo_path>
214  
215 =item <CPAN::Index::API::Role::Readable/read_from_repo_uri>
216  
217 =item L<CPAN::Index::API::Role::Writable/tarball_is_default>
218  
219 =item L<CPAN::Index::API::Role::Writable/repo_path>
220  
221 =item L<CPAN::Index::API::Role::Writable/template>
222  
223 =item L<CPAN::Index::API::Role::Writable/content>
224  
225 =item L<CPAN::Index::API::Role::Writable/write_to_file>
226  
227 =item L<CPAN::Index::API::Role::Writable/write_to_tarball>
228  
229 =item L<CPAN::Index::API::Role::Clonable/clone>
230  
231 =item L<CPAN::Index::API::Role::HavingGeneratedBy/generated_by>
232  
233 =item L<CPAN::Index::API::Role::HavingGeneratedBy/last_generated>
234  
235 =back
236  
237 =cut