blib/lib/JSON/GRDDL.pm

filter: Statementstmt 84.7% 100 / 118 Branchbran 57.4% 31 / 54 Conditioncond 35.7% 15 / 42 MC/DCmcdc 4.0% 1 / 25 Subroutinesub 100% 20 / 20 Podpod 100% 6 / 6 totaltotal 65.2% 173 / 265 CC 48 SCAR 28.9
CC
48
Cov
68%
data
49.4
discover
13.1
ua
11.2
CRAP 18.0
↑ Summary
1 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package JSON::GRDDL;
6  
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON::T;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Object::AUTHORITY;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use RDF::Trine;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Scalar::Util qw[blessed];
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 $JSON::GRDDL::AUTHORITY = 'cpan:TOBYINK';
17 2 $JSON::GRDDL::VERSION = '0.002';
18 }
19  
20 sub new
21 3 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
22 3 my ($class) = @_;
23 3 return bless { cache=>{}, ua=>undef, }, $class;
24 }
25  
26 sub ua
27 5 ▶{
Subroutine ua: called CC=6 SCAR=24.1
Pod documented
28 5 my $self = shift;
29 5 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 5 5
30 {
31 0 my $rv = $self->{'ua'};
32 0 $self->{'ua'} = shift;
33 croak "Set UA to something that is not an LWP::UserAgent!"
34 0 ▶ unless blessed $self->{'ua'} && $self->{'ua'}->isa('LWP::UserAgent');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self->{"ua"}) and $self->{"ua"}->isa("LWP::UserAgent") 0 0 0
Condition0%0/3
builtin::blessed($self->{"ua"}) and $self->{"ua"}->isa("LWP::UserAgent")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($self->{"ua"}) and $self->{"ua"}->isa("LWP::UserAgent")
builtin::blessed($self->{"ua"}) $self->{"ua"}->isa("LWP::UserAgent")
Truth table0%0/3
A and B
A= builtin::blessed($self->{"ua"})B= $self->{"ua"}->isa("LWP::UserAgent")
ABresult
0X0
100
111
35 0 return $rv;
36 }
37 5 ▶ unless (blessed $self->{'ua'} && $self->{'ua'}->isa('LWP::UserAgent'))
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($self->{"ua"}) and $self->{"ua"}->isa("LWP::UserAgent")) 3 2 5
Condition66%2/3
builtin::blessed($self->{"ua"}) and $self->{"ua"}->isa("LWP::UserAgent")
!ll&&!rl&&r
302
MC/DC50%1/2
builtin::blessed($self->{"ua"}) and $self->{"ua"}->isa("LWP::UserAgent")
builtin::blessed($self->{"ua"}) $self->{"ua"}->isa("LWP::UserAgent")
Truth table66%2/3
A and B
A= builtin::blessed($self->{"ua"})B= $self->{"ua"}->isa("LWP::UserAgent")
ABresult
0X0
100
111
38 {
39 3 $self->{'ua'} = LWP::UserAgent->new(agent=>sprintf('%s/%s (%s) ',
40 __PACKAGE__,
41 __PACKAGE__->VERSION,
42 __PACKAGE__->AUTHORITY,
43 ));
44 }
45 5 return $self->{'ua'};
46 }
47  
48 sub data
49 5 ▶{
Subroutine data: called CC=17 SCAR=39.0
Pod documented
50 5 my ($self, $document, $uri, %options) = @_;
51
52 5 ▶ unless (ref $document)
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $document) 3 2 5
53 {
54 3 $document = from_json("$document");
55 }
56
57 5 ▶ $options{'model'} ||= RDF::Trine::Model->temporary_model;
Condition66%2/3
$options{"model"} ||= "RDF::Trine::Model"->temporary_model
l!l&&r!l&&!r
230
MC/DC0%0/2
$options{"model"} ||= "RDF::Trine::Model"->temporary_model
$options{"model"} "RDF::Trine::Model"->temporary_model
Truth table66%2/3
A ||= B
A= $options{"model"}B= "RDF::Trine::Model"->temporary_model
ABresult
1X1
011
000
58
59 5 my $T = $self->discover($document, $uri, %options);
60 5 ▶ if ($T)
Branch: 3 branches
BranchTrueFalseTotal
if ($T) { } 4 1 5
elsif (ref $document eq 'HASH' and not $options{'nested'} and not grep({not $_ =~ /:/;} keys %$document)) { } 0 1 1
elsif (ref $document eq 'HASH' and $document->{'$schema'}{'$ref'} eq 'http://soapjr.org/schemas/RDF_JSON') { } 0 2 2
61 {
62 4 return $self->transform_by_uri($document, $uri, $T, %options);
63 }
64 ▶ elsif (ref $document eq 'HASH' and !$options{'nested'}
Condition33%2/6
ref $document eq "HASH" and not $options{"nested"} and not grep({not $_ =~ /:/;} keys %$document)
!ll&&!rl&&r
100
ref $document eq "HASH" and not $options{"nested"}
!ll&&!rl&&r
100
MC/DC0%0/3
ref $document eq "HASH" and not $options{"nested"} and not grep({not $_ =~ /:/;} keys %$document)
ref $document eq "HASH" not $options{"nested"} not grep({not $_ =~ /:/;} keys %$document)
Truth table25%1/4
A and not(B) and not(C)
A= ref $document eq "HASH"B= not $options{"nested"}C= not grep({not $_ =~ /:/;} keys %$document)
ABCresult
0XX0
10X0
1100
1111
65 0 and (not grep { $_ !~ /:/ } keys %$document))
66 {
67 # looks like it's bona-fide RDF/JSON.
68 0 $options{'model'}->add_hashref($document);
69 0 return $options{'model'};
70 }
71 ▶ elsif (ref $document eq 'HASH'
Condition33%1/3
ref $document eq "HASH" and $document->{"\$schema"}{"\$ref"} eq "http://soapjr.org/schemas/RDF_JSON"
!ll&&!rl&&r
100
MC/DC0%0/2
ref $document eq "HASH" and $document->{"\$schema"}{"\$ref"} eq "http://soapjr.org/schemas/RDF_JSON"
ref $document eq "HASH" $document->{"\$schema"}{"\$ref"} eq "http://soapjr.org/schemas/RDF_JSON"
Truth table33%1/3
A and B
A= ref $document eq "HASH"B= $document->{"\$schema"}{"\$ref"} eq "http://soapjr.org/schemas/RDF_JSON"
ABresult
0X0
100
111
72 and $document->{'$schema'}->{'$ref'} eq 'http://soapjr.org/schemas/RDF_JSON')
73 {
74 # claims it's bona-fide RDF/JSON.
75 0 $options{'model'}->add_hashref($document);
76 0 return $options{'model'};
77 }
78
79 # Not returned anything yet, so try recursing.
80 {
81 1 local $options{'nested'} = 1;
82
83 1 ▶ if (ref $document eq 'HASH')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $document eq 'HASH') { } 0 1 1
elsif (ref $document eq 'ARRAY') { } 1 0 1
84 {
85 0 foreach my $item (values %$document)
86 {
87 0 ▶ if ('HASH' eq ref $item or 'ARRAY' eq ref $item)
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" eq ref $item or "ARRAY" eq ref $item) 0 0 0
Condition0%0/3
"HASH" eq ref $item or "ARRAY" eq ref $item
l!l&&r!l&&!r
000
MC/DC0%0/2
"HASH" eq ref $item or "ARRAY" eq ref $item
"HASH" eq ref $item "ARRAY" eq ref $item
Truth table0%0/3
A or B
A= "HASH" eq ref $itemB= "ARRAY" eq ref $item
ABresult
1X1
011
000
88 {
89 0 $self->data($item, $uri, %options);
90 }
91 }
92 }
93 elsif (ref $document eq 'ARRAY')
94 {
95 1 foreach my $item (@$document)
96 {
97 2 ▶ if ('HASH' eq ref $item or 'ARRAY' eq ref $item)
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" eq ref $item or "ARRAY" eq ref $item) 2 0 2
Condition33%1/3
"HASH" eq ref $item or "ARRAY" eq ref $item
l!l&&r!l&&!r
200
MC/DC0%0/2
"HASH" eq ref $item or "ARRAY" eq ref $item
"HASH" eq ref $item "ARRAY" eq ref $item
Truth table33%1/3
A or B
A= "HASH" eq ref $itemB= "ARRAY" eq ref $item
ABresult
1X1
011
000
98 {
99 2 $self->data($item, $uri, %options);
100 }
101 }
102 }
103 }
104
105 1 return $options{'model'};
106 }
107  
108 sub discover
109 5 ▶{
Subroutine discover: called CC=10 SCAR=25.7
Pod documented
110 5 my ($self, $document, $uri, %options) = @_;
111 5 my $T;
112
113 5 ▶ unless (ref $document)
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $document) 0 5 5
114 {
115 0 $document = from_json("$document");
116 }
117  
118 5 ▶ return unless ref $document eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $document eq "HASH" 1 4 5
119
120 4 ▶ if (defined $document->{'$transformation'})
Branch: 3 branches
BranchTrueFalseTotal
if (defined $document->{'$transformation'}) { } 1 3 4
elsif (defined $document->{'$schema'}{'$schemaTransformation'}) { } 0 3 3
elsif (defined $document->{'$schema'}{'$ref'}) { } 3 0 3
121 {
122 1 $T = $self->_resolve_relative_ref($document->{'$transformation'}, $uri);
123 }
124 elsif (defined $document->{'$schema'}->{'$schemaTransformation'})
125 {
126 0 $T = $self->_resolve_relative_ref($document->{'$schema'}->{'$schemaTransformation'}, $uri);
127 }
128 elsif (defined $document->{'$schema'}->{'$ref'})
129 {
130 3 my $s = $self->_resolve_relative_ref($document->{'$schema'}->{'$ref'}, $uri);
131 3 my $r = $self->_fetch($s,
132 Accept => 'application/schema+json, application/x-schema+json, application/json');
133
134 3 ▶ if (defined $r
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r and $r->code == 200 and $r->header("content-type") =~ m[^\s*(((application|text)/(x-)?json)|(application/(x-)?schema\+json))\b]) 3 0 3
Condition33%2/6
defined $r and $r->code == 200 and $r->header("content-type") =~ m[^\s*(((application|text)/(x-)?json)|(application/(x-)?schema\+json))\b]
!ll&&!rl&&r
003
defined $r and $r->code == 200
!ll&&!rl&&r
003
MC/DC0%0/3
defined $r and $r->code == 200 and $r->header("content-type") =~ m[^\s*(((application|text)/(x-)?json)|(application/(x-)?schema\+json))\b]
defined $r $r->code == 200 $r->header("content-type") =~ m[^\s*(((application|text)/(x-)?json)|(application/(x-)?schema\+json))\b]
Truth table25%1/4
A and B and C
A= defined $rB= $r->code == 200C= $r->header("content-type") =~ m[^\s*(((application|text)/(x-)?json)|(application/(x-)?schema\+json))\b]
ABCresult
0XX0
10X0
1100
1111
135 && $r->code == 200
136 && $r->header('content-type') =~ m#^\s*(((application|text)/(x-)?json)|(application/(x-)?schema\+json))\b#)
137 {
138 3 my $schema = from_json($r->decoded_content);
139 3 ▶ if (defined $schema->{'$schemaTransformation'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema->{"\$schemaTransformation"}) 3 0 3
140 {
141 3 $T = $self->_resolve_relative_ref($schema->{'$schemaTransformation'}, $s);
142 }
143 }
144 }
145 4 return ($T);
146 }
147  
148 sub transform_by_uri
149 4 ▶{
Subroutine transform_by_uri: called CC=4 SCAR=16.6
Pod documented
150 4 my ($self, $document, $uri, $transformation_uri, %options) = @_;
151
152 4 my ($name) = ($transformation_uri =~ /\#(.+)$/);
153
154 4 my $r = $self->_fetch($transformation_uri,
155 Accept => 'application/ecmascript, application/javascript, text/ecmascript, text/javascript, application/x-ecmascript');
156 4 ▶ if (defined $r
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r and $r->code == 200 and $r->header("content-type") =~ m[^\s*((application|text)/(x-)?(java|ecma)script)\b]) 4 0 4
Condition33%2/6
defined $r and $r->code == 200 and $r->header("content-type") =~ m[^\s*((application|text)/(x-)?(java|ecma)script)\b]
!ll&&!rl&&r
004
defined $r and $r->code == 200
!ll&&!rl&&r
004
MC/DC0%0/3
defined $r and $r->code == 200 and $r->header("content-type") =~ m[^\s*((application|text)/(x-)?(java|ecma)script)\b]
defined $r $r->code == 200 $r->header("content-type") =~ m[^\s*((application|text)/(x-)?(java|ecma)script)\b]
Truth table25%1/4
A and B and C
A= defined $rB= $r->code == 200C= $r->header("content-type") =~ m[^\s*((application|text)/(x-)?(java|ecma)script)\b]
ABCresult
0XX0
10X0
1100
1111
157 && $r->code == 200
158 && $r->header('content-type') =~ m#^\s*((application|text)/(x-)?(java|ecma)script)\b#)
159 {
160 4 return $self->transform_by_jsont($document, $uri, $r->decoded_content, $name, %options);
161 }
162
163 0 return;
164 }
165  
166 sub transform_by_jsont
167 4 ▶{
Subroutine transform_by_jsont: called CC=2 SCAR=7.1
Pod documented
168 4 my ($self, $document, $uri, $transformation, $name, %options) = @_;
169
170 4 my $jsont = JSON::T->new($transformation, $name);
171 4 my $out = $jsont->transform_structure($document);
172
173 4 _relabel($out);
174
175 4 ▶ $options{'model'} ||= RDF::Trine::Model->temporary_model;
Condition33%1/3
$options{"model"} ||= "RDF::Trine::Model"->temporary_model
l!l&&r!l&&!r
400
MC/DC0%0/2
$options{"model"} ||= "RDF::Trine::Model"->temporary_model
$options{"model"} "RDF::Trine::Model"->temporary_model
Truth table33%1/3
A ||= B
A= $options{"model"}B= "RDF::Trine::Model"->temporary_model
ABresult
1X1
011
000
176 4 $options{'model'}->add_hashref($out);
177 4 return $options{'model'};
178 }
179  
180 sub _relabel
181 4 ▶{
Subroutine _relabel: called CC=10 SCAR=23.9
182 4 my ($data) = @_;
183 4 my $pfx = '_:p'.int( 10_000_000 + rand(80_000_000) );
184
185 4 foreach my $key (keys %$data)
186 {
187 4 ▶ if ($key =~ /^_:(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^_:(.*)/) 4 0 4
188 {
189 4 my $new_key = $pfx . $1;
190 4 $data->{$new_key} = delete $data->{$key}
191 }
192 }
193
194 4 foreach my $po (values %$data)
195 {
196 4 foreach my $ol (values %$po)
197 {
198 12 foreach my $o (@$ol)
199 {
200 12 ▶ next if $o->{type} =~ /literal/i;
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"type"} =~ /literal/i 4 8 12
201 8 ▶ next if exists $o->{lang};
Branch: 1 branch
BranchTrueFalseTotal
if exists $o->{"lang"} 0 8 8
202 8 ▶ next if exists $o->{datatype};
Branch: 1 branch
BranchTrueFalseTotal
if exists $o->{"datatype"} 0 8 8
203
204 8 ▶ if ($o->{value} =~ /^_:(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"value"} =~ /^_:(.*)/) 0 8 8
205 {
206 0 $o->{value} = $pfx . $1;
207 }
208 }
209 }
210 }
211 }
212  
213 sub _fetch
214 7 ▶{
Subroutine _fetch: called CC=2 SCAR=7.0
215 7 my ($self, $document, %headers) = @_;
216 7 ▶ $self->{'cache'}->{$document} ||= $self->ua->get($document, %headers);
Condition66%2/3
$self->{"cache"}{$document} ||= $self->ua->get($document, %headers)
l!l&&r!l&&!r
250
MC/DC0%0/2
$self->{"cache"}{$document} ||= $self->ua->get($document, %headers)
$self->{"cache"}{$document} $self->ua->get($document, %headers)
Truth table66%2/3
A ||= B
A= $self->{"cache"}{$document}B= $self->ua->get($document, %headers)
ABresult
1X1
011
000
217 7 return $self->{'cache'}->{$document};
218 }
219  
220 sub _resolve_relative_ref
221 7 ▶{
Subroutine _resolve_relative_ref: called CC=4 SCAR=15.5
222 7 my ($self, $ref, $base) = @_;
223  
224 7 ▶ return $ref unless $base; # keep relative unless we have a base URI
Branch: 1 branch
BranchTrueFalseTotal
unless $base 0 7 7
225  
226 7 ▶ if ($ref =~ /^([a-z][a-z0-9\+\.\-]*)\:/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($ref =~ /^([a-z][a-z0-9\+\.\-]*)\:/i) 0 7 7
227 {
228 0 return $ref; # already an absolute reference
229 }
230  
231 # create absolute URI
232 7 my $abs = URI->new_abs($ref, $base)->canonical->as_string;
233  
234 7 ▶ while ($abs =~ m!^(http://.*)(\.\./|\.)+(\.\.|\.)?$!i)
Branch: 1 branch
BranchTrueFalseTotal
if ($abs =~ m[^(http://.*)(\.\./|\.)+(\.\.|\.)?$]i) 0 7 7
235 0 { $abs = $1; } # fix edge case of 'http://example.com/../../../'
236  
237 7 return $abs;
238 }
239  
240 2 1;
241  
242 __END__
↑ Summary