File Coverage

blib/lib/WebService/Kramerius/API4/Base.pm
Criterion Covered Total %
statement 18 68 26.4
branch 0 22 0.0
condition n/a
subroutine 6 11 54.5
pod 0 1 0.0
total 24 102 23.5


line stmt bran cond sub pod time code
1             package WebService::Kramerius::API4::Base;
2              
3 20     20   219574 use strict;
  20         80  
  20         797  
4 20     20   157 use warnings;
  20         41  
  20         1108  
5              
6 20     20   10036 use Class::Utils qw(set_params);
  20         322167  
  20         2566  
7 20     20   1665 use Error::Pure qw(err);
  20         42  
  20         985  
8 20     20   122 use List::Util 1.33 qw(none);
  20         436  
  20         1207  
9 20     20   16041 use LWP::UserAgent;
  20         1255973  
  20         15434  
10              
11             our $VERSION = 0.02;
12              
13             # Constructor.
14             sub new {
15 0     0 0   my ($class, @params) = @_;
16              
17             # Create object.
18 0           my $self = bless {}, $class;
19              
20             # Library URL.
21 0           $self->{'library_url'} = undef;
22              
23             # LWP::UserAgent instance.
24 0           $self->{'lwp_ua'} = undef;
25              
26             # Output dispatch.
27 0           $self->{'output_dispatch'} = {};
28              
29             # Verbose output.
30 0           $self->{'verbose'} = 0;
31              
32             # Process params.
33 0           set_params($self, @params);
34              
35             # Check library URL.
36 0 0         if (! defined $self->{'library_url'}) {
37 0           err "Parameter 'library_url' is required.";
38             }
39              
40             # LWP::UserAgent.
41 0 0         if (! $self->{'lwp_ua'}) {
42 0           $self->{'lwp_ua'} = LWP::UserAgent->new;
43 0           $self->{'lwp_ua'}->agent('WebService::Kramerius::API4/'.$VERSION);
44             }
45              
46             # Object.
47 0           return $self;
48             }
49              
50             sub _construct_opts {
51 0     0     my ($self, $opts_hr) = @_;
52              
53             # TODO Use URI?
54              
55 0           my $opts = '';
56 0           foreach my $key (keys %{$opts_hr}) {
  0            
57 0 0         if ($opts) {
58 0           $opts .= '&';
59             }
60 0           my $ref = ref $opts_hr->{$key};
61 0 0         if ($ref eq 'ARRAY') {
    0          
62 0           $opts .= $key.'='.(join ',', @{$opts_hr->{$key}});
  0            
63             } elsif ($ref eq '') {
64 0           $opts .= $key.'='.$opts_hr->{$key};
65             } else {
66 0           err "Reference to '$ref' doesn't supported.";
67             }
68             }
69 0 0         if ($opts) {
70 0           $opts = '?'.$opts;
71             }
72              
73 0           return $opts;
74             }
75              
76             sub _get_data {
77 0     0     my ($self, $url) = @_;
78              
79 0 0         if ($self->{'verbose'}) {
80 0           print "URL: $url\n";
81             }
82 0           my $req = HTTP::Request->new('GET' => $url);
83 0           my $res = $self->{'lwp_ua'}->request($req);
84 0 0         if (! $res->is_success) {
85 0           err "Cannot get '$url' URL.",
86             'HTTP code', $res->code,
87             'message', $res->message,
88             ;
89             }
90 0           my $content_type = $res->headers->content_type;
91              
92             # XXX Hack for forced content type.
93 0 0         if (exists $self->{'_force_content_type'}) {
94 0           $content_type = delete $self->{'_force_content_type'};
95             }
96              
97 0           my $ret = $res->content;
98 0 0         if (exists $self->{'output_dispatch'}->{$content_type}) {
99 0           $ret = $self->{'output_dispatch'}->{$content_type}->($ret);
100             }
101              
102 0           return $ret;
103             }
104              
105             sub _validate_opts {
106 0     0     my ($self, $opts_hr, $valid_opts_ar) = @_;
107              
108 0           foreach my $opt_key (keys %{$opts_hr}) {
  0            
109 0 0   0     if (none { $opt_key eq $_ } @{$valid_opts_ar}) {
  0            
  0            
110 0           err "Option '$opt_key' doesn't supported.";
111             }
112             }
113              
114 0           return;
115             }
116              
117             1;
118              
119             __END__