File Coverage

blib/lib/WQS/SPARQL/Result.pm
Criterion Covered Total %
statement 41 44 93.1
branch 9 12 75.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 59 65 90.7


line stmt bran cond sub pod time code
1             package WQS::SPARQL::Result;
2              
3 4     4   315999 use strict;
  4         54  
  4         112  
4 4     4   22 use warnings;
  4         9  
  4         111  
5              
6 4     4   1431 use Class::Utils qw(set_params);
  4         39126  
  4         155  
7 4     4   200 use Error::Pure qw(err);
  4         14  
  4         139  
8 4     4   2420 use URI;
  4         25703  
  4         1528  
9              
10             our $VERSION = 0.03;
11              
12             sub new {
13 5     5 1 2733 my ($class, @params) = @_;
14              
15             # Create object.
16 5         14 my $self = bless {}, $class;
17              
18             # Verbose mode.
19 5         20 $self->{'verbose'} = 0;
20              
21             # Process parameters.
22 5         21 set_params($self, @params);
23              
24 5         46 return $self;
25             }
26              
27             sub result {
28 4     4 1 1033 my ($self, $result_hr, $vars_ar) = @_;
29              
30 4 100       15 if (! defined $vars_ar) {
31 3         7 $vars_ar = $result_hr->{'head'}->{'vars'};
32             }
33              
34 4 50       12 if ($self->{'verbose'}) {
35 0         0 require Data::Printer;
36 0         0 Data::Printer::p($result_hr);
37             }
38              
39 4         7 my @res;
40 4 50       11 if (exists $result_hr->{'results'}->{'bindings'}) {
41 4         6 my @items = @{$result_hr->{'results'}->{'bindings'}};
  4         12  
42 4         10 foreach my $item_hr (@items) {
43 4         5 my $result_hr;
44 4         5 foreach my $var (@{$vars_ar}) {
  4         8  
45 5 50       12 if (! exists $item_hr->{$var}) {
46 0         0 next;
47             }
48              
49             # TODO Implement other values
50              
51             # QID.
52             # TODO Check real QID, could be another uri.
53 5 100       18 if ($item_hr->{$var}->{'type'} eq 'uri') {
    100          
54 3         15 my $qid_uri = URI->new($item_hr->{$var}->{'value'});
55 3         8184 my @segs = $qid_uri->path_segments;
56 3         200 $result_hr->{$var} = $segs[-1];
57             } elsif ($item_hr->{$var}->{'type'} eq 'literal') {
58             # TODO Lang?
59 1         4 $result_hr->{$var} = $item_hr->{$var}->{'value'};
60             } else {
61 1         17 err "Type '".$item_hr->{$var}->{'type'}."' doesn't supported.";
62             }
63             }
64 3         11 push @res, $result_hr;
65             }
66             }
67              
68 3         13 return @res;
69             }
70              
71             1;
72              
73             __END__