vCard/Importer.pm

filter: Statementstmt 29.6% 32 / 108 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 47.6% 10 / 21 Podpod 100% 7 / 7 totaltotal 25.1% 49 / 195 CC 24 SCAR 40.9
CC
24
Cov
21%
_process_card
132.0
_extract_structure
110.0
new
6.0
CRAP 59.5
« vCard/Entity.pm ↑ Summary vCard/Line.pm »
1 package RDF::vCard::Importer;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Encode qw[];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use RDF::TrineX::Functions -shortcuts;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use RDF::vCard::Entity;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use RDF::vCard::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Text::vFile::asData;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our $VERSION = '0.012';
17  
18 sub new
19 0 ▶{
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
20 0 my ($class, %options) = @_;
21 0 my $self = bless { %options }, $class;
22 0 ▶ $self->init unless $self->model;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->model 0 0 0
23 0 return $self;
24 }
25  
26 sub init
27 0 ▶{
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
28 0 my ($self, $model, %opts) = @_;
29 0 $self->{model} = rdf_parse($model, %opts);
30 0 return $self;
31 }
32  
33 sub model
34 0 ▶{
Subroutine model: not called CC=1 SCAR=6.9
Pod documented
35 0 my ($self) = @_;
36 0 return $self->{model};
37 }
38  
39 1 *TO_RDF = \&model;
40  
41 sub _ua
42 0 ▶{
Subroutine _ua: not called CC=2 SCAR=17.9
43 0 my ($self) = @_;
44 0 ▶ $self->{ua} ||= LWP::UserAgent->new(agent => sprintf('%s/%s', __PACKAGE__, $VERSION));
Condition0%0/3
$self->{"ua"} ||= "LWP::UserAgent"->new("agent", sprintf('%s/%s', __PACKAGE__, $RDF::vCard::Importer::VERSION))
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"ua"} ||= "LWP::UserAgent"->new("agent", sprintf('%s/%s', __PACKAGE__, $RDF::vCard::Importer::VERSION))
$self->{"ua"} "LWP::UserAgent"->new("agent", sprintf('%s/%s', __PACKAGE__, $RDF::vCard::Importer::VERSION))
Truth table0%0/3
A ||= B
A= $self->{"ua"}B= "LWP::UserAgent"->new("agent", sprintf('%s/%s', __PACKAGE__, $RDF::vCard::Importer::VERSION))
ABresult
1X1
011
000
45 0 return $self->{ua};
46 }
47  
48 sub import_file
49 0 ▶{
Subroutine import_file: not called CC=1 SCAR=6.9
Pod documented
50 0 my ($self, $file, %options) = @_;
51 0 open my $fh, "<:encoding(UTF-8)", $file;
52 0 my $cards = Text::vFile::asData->new->parse($fh);
53 0 close $fh;
54 0 return $self->_process($cards, %options);
55 }
56  
57 sub import_fh
58 0 ▶{
Subroutine import_fh: not called CC=1 SCAR=6.9
Pod documented
59 0 my ($self, $fh, %options) = @_;
60 0 my $cards = Text::vFile::asData->new->parse($fh);
61 0 return $self->_process($cards, %options);
62 }
63  
64 sub import_url
65 0 ▶{
Subroutine import_url: not called CC=2 SCAR=17.9
Pod documented
66 0 my ($self, $url) = @_;
67 0 my $r = $self->_ua->get($url, Accept=>'text/directory;profile=vCard, text/vcard, text/x-vcard, text/directory;q=0.1');
68 0 ▶ return unless $r->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->is_success 0 0 0
69 0 return $self->import_string($r->decoded_content, lang => $r->content_language);
70 }
71  
72 sub import_string
73 0 ▶{
Subroutine import_string: not called CC=1 SCAR=6.9
Pod documented
74 0 my ($self, $data, %options) = @_;
75 0 my @lines = split /\r?\n/, $data;
76 0 my $cards = Text::vFile::asData->new->parse_lines(@lines);
77 0 return $self->_process($cards, %options);
78 }
79  
80 sub _process
81 0 ▶{
Subroutine _process: not called CC=2 SCAR=17.9
82 0 my ($self, $cards, %options) = @_;
83
84 0 my @Cards;
85 0 foreach my $c (@{ $cards->{objects} })
86 {
87 0 push @Cards, $self->_process_card($c, %options);
88 }
89
90 0 return @Cards;
91 }
92  
93 sub _process_card
94 0 ▶{
Subroutine _process_card: not called CC=11 SCAR=48.8
95 0 my ($self, $card, %options) = @_;
96 0 my $C = RDF::vCard->new_entity( profile => $card->{type} );
97
98 0 ▶ while (my ($prop, $vals) = each %{ $card->{properties} })
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$card{"properties"};}) 0 0 0
99 {
100 0 my $group;
101 0 ▶ if ($prop =~ /^(.+)\.([^\.]+)$/) # ignore vCard 4.0 grouping construct
Branch: 1 branch
BranchTrueFalseTotal
if ($prop =~ /^(.+)\.([^\.]+)$/) 0 0 0
102 {
103 0 $prop = $2;
104 0 $group = $1;
105 }
106
107 0 foreach my $val (@$vals)
108 {
109 0 my $strval = $val->{value};
110
111 # I wish Text::vFile::asData did this for me!
112 0 ▶ my $structured_value = ($prop =~ /^(ADR|CATEGORIES|GEO|N|ORG)$/i)
Branch: 1 branch
BranchTrueFalseTotal
$prop =~ /^(ADR|CATEGORIES|GEO|N|ORG)$/i ? : 0 0 0
113 ? $self->_extract_structure($strval)
114 : RDF::vCard::Line->_unescape_value($strval);
115 # Could technically extract structure for all properties,
116 # but it's a waste of time, and some of the RDF::vCard::Line
117 # code might cope badly.
118
119 my $L = RDF::vCard::Line->new(
120 property => uc $prop,
121 value => $structured_value,
122 0 type_parameters => do {
123 # force keys to uppercase
124 0 my (%tp, $k, $v);
125 0 ▶ $tp{uc $k} = $v while ($k, $v) = each %{$val->{param}};
Branch: 1 branch
BranchTrueFalseTotal
() = each %{$$val{"param"};} and do { $tp{uc $k} = $v } 0 0 0
126 0 \%tp;
127 },
128 );
129 $L->type_parameters->{TYPE} = [split /,/, $L->type_parameters->{TYPE}]
130 0 ▶ if ($L->type_parameters and $L->type_parameters->{TYPE});
Branch: 1 branch
BranchTrueFalseTotal
if $L->type_parameters and $L->type_parameters->{"TYPE"} 0 0 0
Condition0%0/3
$L->type_parameters and $L->type_parameters->{"TYPE"}
!ll&&!rl&&r
000
MC/DC0%0/2
$L->type_parameters and $L->type_parameters->{"TYPE"}
$L->type_parameters $L->type_parameters->{"TYPE"}
Truth table0%0/3
A and B
A= $L->type_parametersB= $L->type_parameters->{"TYPE"}
ABresult
0X0
100
111
131 0 ▶ $L->type_parameters->{_GROUP} = $group
Branch: 1 branch
BranchTrueFalseTotal
if $group 0 0 0
132 if $group;
133 0 ▶ $L->type_parameters->{LANG} ||= $options{lang} if defined $options{lang};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"lang"} 0 0 0
Condition0%0/3
$L->type_parameters->{"LANG"} ||= $options{"lang"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$L->type_parameters->{"LANG"} ||= $options{"lang"}
$L->type_parameters->{"LANG"} $options{"lang"}
Truth table0%0/3
A ||= B
A= $L->type_parameters->{"LANG"}B= $options{"lang"}
ABresult
1X1
011
000
134
135 0 $C->add($L);
136 }
137 }
138
139 0 $C->add_to_model( $self->model );
140
141 0 return $C;
142 }
143  
144 sub _extract_structure
145 0 ▶{
Subroutine _extract_structure: not called CC=10 SCAR=47.0
146 0 my ($self, $string) = @_;
147 0 my @naive_parts = split /;/, $string;
148 0 my @parts;
149
150 0 ▶ while (my $part = shift @naive_parts)
Branch: 1 branch
BranchTrueFalseTotal
if (my $part = shift @naive_parts) 0 0 0
151 {
152 0 push @parts, $part;
153 0 ▶ while ($parts[-1] =~ /\\$/ and $parts[-1] !~ /\\\\$/ and @naive_parts)
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[-1] =~ /\\$/ and not $parts[-1] =~ /\\\\$/ and @naive_parts) 0 0 0
Condition0%0/6
$parts[-1] =~ /\\$/ and not $parts[-1] =~ /\\\\$/ and @naive_parts
!ll&&!rl&&r
000
$parts[-1] =~ /\\$/ and not $parts[-1] =~ /\\\\$/
!ll&&!rl&&r
000
MC/DC0%0/3
$parts[-1] =~ /\\$/ and not $parts[-1] =~ /\\\\$/ and @naive_parts
$parts[-1] =~ /\\$/ not $parts[-1] =~ /\\\\$/ @naive_parts
Truth table0%0/4
A and not(B) and C
A= $parts[-1] =~ /\\$/B= not $parts[-1] =~ /\\\\$/C= @naive_parts
ABCresult
0XX0
10X0
1100
1111
154 {
155 0 $parts[-1] =~ s/\\$/;/;
156 0 $parts[-1] .= shift @naive_parts;
157 }
158 }
159
160 0 my @rv;
161 0 foreach my $part (@parts)
162 {
163 0 my @naive_subparts = split /,/, $part;
164 0 my @subparts;
165  
166 0 ▶ while (my $subpart = shift @naive_subparts)
Branch: 1 branch
BranchTrueFalseTotal
if (my $subpart = shift @naive_subparts) 0 0 0
167 {
168 0 push @subparts, $subpart;
169 0 ▶ while ($subparts[-1] =~ /\\$/ and $subparts[-1] !~ /\\\\$/ and @naive_subparts)
Branch: 1 branch
BranchTrueFalseTotal
if ($subparts[-1] =~ /\\$/ and not $subparts[-1] =~ /\\\\$/ and @naive_subparts) 0 0 0
Condition0%0/6
$subparts[-1] =~ /\\$/ and not $subparts[-1] =~ /\\\\$/ and @naive_subparts
!ll&&!rl&&r
000
$subparts[-1] =~ /\\$/ and not $subparts[-1] =~ /\\\\$/
!ll&&!rl&&r
000
MC/DC0%0/3
$subparts[-1] =~ /\\$/ and not $subparts[-1] =~ /\\\\$/ and @naive_subparts
$subparts[-1] =~ /\\$/ not $subparts[-1] =~ /\\\\$/ @naive_subparts
Truth table0%0/4
A and not(B) and C
A= $subparts[-1] =~ /\\$/B= not $subparts[-1] =~ /\\\\$/C= @naive_subparts
ABCresult
0XX0
10X0
1100
1111
170 {
171 0 $subparts[-1] =~ s/\\$/,/;
172 0 $subparts[-1] .= shift @naive_subparts;
173 }
174 }
175
176 0 push @rv, [ map { RDF::vCard::Line->_unescape_value($_) } @subparts ];
177 }
178 0 return [@rv];
179 }
180  
181 1 1;
182  
183 __END__
« vCard/Entity.pm ↑ Summary vCard/Line.pm »