API/File/Whois.pm

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