API/Resolve.pm

filter: Statementstmt 70.9% 39 / 55 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 10 / 12 Podpod 0.0% 0 / 2 totaltotal 67.1% 49 / 73 CC 3 SCAR 8.9
CC
3
Cov
66%
resolve
6.0
_lazy_api
6.0
BEGIN
1.0
CRAP 2.4
« API.pm ↑ Summary
1 package Wikibase::API::Resolve;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use IO::Barf qw(barf);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use JSON::XS qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Perl6::Slurp qw(slurp);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Wikibase::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 Readonly::Array our @EXPORT_OK => qw(resolve);
15  
16 3 our $VERSION = 0.07;
17  
18 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
19 2 my ($class, @params) = @_;
20  
21 # Create object.
22 2 my $self = bless {}, $class;
23  
24 # Login name.
25 2 $self->{'login_name'} = undef;
26  
27 # Login password.
28 2 $self->{'login_password'} = undef;
29  
30 # MediaWiki::API object.
31 2 $self->{'mediawiki_api'} = MediaWiki::API->new;
32  
33 # MediaWiki site.
34 2 $self->{'mediawiki_site'} = 'www.wikidata.org';
35  
36 # Resolve directory.
37 2 $self->{'resolve_dir'} = '/var/lib/wb_resolve';
38  
39 # Process parameters.
40 2 set_params($self, @params);
41  
42 1 return $self;
43 }
44  
45 0 ▶sub resolve {
Subroutine resolve: not called CC=2 SCAR=17.9
Pod undocumented
46 0 my ($self, $qid) = @_;
47  
48 0 my $qid_label;
49 0 my $qid_file = catfile($self->{'resolve_dir'}, uc($qid));
50  
51 0 my $struct_hr;
52 0 ▶ if (-r $qid_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $qid_file) { } 0 0 0
53 0 $struct_hr = decode_json(slurp($qid_file));
54 } else {
55 0 $self->_lazy_api;
56 0 $struct_hr = $self->{'api'}->get_item_raw($qid);
57 0 barf($qid_file, encode_json($struct_hr));
58 }
59  
60 0 my $item_obj = Wikibase::Datatype::Struct::Item::struct2obj($struct_hr);
61  
62 0 return $item_obj;
63 }
64  
65 0 ▶sub _lazy_api {
Subroutine _lazy_api: not called CC=2 SCAR=17.9
66 0 my $self = shift;
67  
68 0 ▶ if (defined $self->{'api'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"api"}) 0 0 0
69 0 return;
70 }
71  
72 $self->{'api'} = Wikibase::API->new(
73 'mediawiki_api' => $self->{'mediawiki_api'},
74 'mediawiki_site' => $self->{'mediawiki_site'},
75 'login_name' => $self->{'login_name'},
76 0 'login_password' => $self->{'login_password'},
77 );
78  
79 0 return;
80 }
81  
82 3 1;
83  
84 __END__
« API.pm ↑ Summary