File Coverage

blib/lib/XML/FOAFKnows/FromvCard.pm
Criterion Covered Total %
statement 22 24 91.6
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 30 32 93.7


line stmt bran cond sub pod time code
1             package XML::FOAFKnows::FromvCard;
2              
3 3     3   9302 use 5.007;
  3         10  
  3         115  
4 3     3   17 use strict;
  3         7  
  3         107  
5 3     3   14 use warnings;
  3         9  
  3         88  
6 3     3   14 use Carp;
  3         5  
  3         219  
7              
8 3     3   2714 use Text::vCard::Addressbook;
  3         231471  
  3         39  
9 3     3   106 use Text::vCard;
  3         6  
  3         14  
10 3     3   2813 use Digest::SHA1 qw(sha1_hex);
  3         2437  
  3         199  
11 3     3   2423 use IDNA::Punycode;
  0            
  0            
12              
13             use base qw( Text::vCard );
14              
15             our $VERSION = '0.6';
16              
17             sub format {
18             my $that = shift;
19             my $class = ref($that) || $that;
20             my ($text,%config) = @_;
21             my $address_book = Text::vCard::Addressbook->new({
22             'source_text' => $text,
23             });
24              
25             my $privstatattrib = $config{attribute} || 'CLASS';
26             # Parse and build the fragment
27             my $records = '';
28             my @urls = ();
29             my $counts = 0;
30             foreach my $vcard ($address_book->vcards()) {
31             my $privacystat = 'PRIVATE'; # The default privacy option if nothing else is set
32             if (defined($config{privacy})) {
33             $privacystat = uc($config{privacy});
34             } elsif (($vcard->get($privstatattrib))[0]) {
35             $privacystat = ($vcard->get($privstatattrib))[0]->value; # Check the status and generate full records only for public records
36             }
37             next if ($privacystat eq 'CONFIDENTIAL');
38             my @email = ($vcard->get('EMAIL'));
39             my $url = ($vcard->get('URL'))[0];
40             next unless ($url || $email[0]); # We need at least an URL or an email to continue
41             $counts++;
42             $records .= "\n\t
43             # TODO: nodeIDs, but how to generate...?
44             if (($vcard->get('NICKNAME'))[0]) { # a nodeID on the Person record can be useful
45             my $punynick = encode_punycode(($vcard->get('NICKNAME'))[0]->value); # puny-encode any nicks,
46             $punynick =~ s/\s/_/gs; # and replace any whitespace with underscores
47             $records .= ' rdf:nodeID="' . $punynick . '">'.
48             "\n\t\t" . ($vcard->get('NICKNAME'))[0]->value . '';
49             } else {
50             $records .= ' rdf:nodeID="person'.$counts.'">';
51             }
52              
53             foreach (@email) {
54             next unless (defined($_));
55             $records .= "\n\t\t" . sha1_hex('mailto:' . $_->value) . '';
56             }
57             if ($url) {
58             my $tmp = $url->value;
59             $tmp =~ s/\\:/:/g; # Some files seem to have colons in URLs escaped
60             $records .= "\n\t\t".'';
61             }
62             my $fullname = '';
63             if ($privacystat eq 'PUBLIC') {
64             my $name = ($vcard->get('N'))[0];
65             if ($name) {
66             $records .= "\n\t\t".$name->family.'';
67             $fullname = $name->family;
68             if ($name->given()) {
69             $records .= "\n\t\t".$name->given.''.
70             "\n\t\t".$name->given.' '.$name->family.'';
71             $fullname = $name->given.' '.$name->family;
72             }
73             }
74             elsif (($vcard->get('FN'))[0]->fullname()) {
75             $records .= "\n\t\t".($vcard->get('FN'))[0]->fullname.'';
76             $fullname = ($vcard->get('FN'))[0]->fullname;
77             }
78             }
79             # Now we build the URL to be returned by the links method
80             if ($vcard->get('URL')) {
81             foreach my $url2 ($vcard->get('URL')) {
82             my $tmp = $url2->value;
83             $tmp =~ s/\\:/:/g;
84             push(@urls, {url => $tmp, title => $fullname});
85             }
86             }
87             $records .= "\n\t\n\n";
88             }
89              
90             my $self = {
91             _out => $records,
92             _urls => \@urls,
93             _config => \%config,
94             };
95             bless($self, $class);
96             return $self;
97             }
98              
99              
100             sub title { return undef }
101              
102             sub links { return shift->{_urls}; }
103              
104             sub fragment { return shift->{_out}; }
105              
106             sub document {
107             my ($self,$encoding) = @_;
108              
109             my $out = '
110             if ($encoding) {
111             $out .= ' encoding="'.$encoding.'"';
112             }
113             $out .= '?>';
114             $out .= "\n
115             'xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" '.
116             'xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" '.
117             'xmlns:foaf="http://xmlns.com/foaf/0.1/">'.
118             "\n
119              
120             if ($self->{_config}->{uri}) {
121             $out .= ' rdf:about="'.$self->{_config}->{uri}.'">';
122             } else {
123             $out .= ">\n\n";
124             }
125             if ($self->{_config}->{email}) {
126             $out .= "\n\t" . sha1_hex('mailto:' . $self->{_config}->{email}) . '';
127             }
128             if ($self->{_config}->{seeAlso}) {
129             $out .= "\n\t".''."\n\n";
130             }
131             return $out . $self->{_out} . "\n\n\n";
132             }
133              
134              
135              
136              
137             1;
138             __END__