File Coverage

blib/lib/WWW/AUR.pm
Criterion Covered Total %
statement 61 67 91.0
branch 7 12 58.3
condition 4 6 66.6
subroutine 16 17 94.1
pod 1 2 50.0
total 89 104 85.5


line stmt bran cond sub pod time code
1             package WWW::AUR;
2              
3 12     12   67232 use warnings 'FATAL' => 'all';
  12         25  
  12         576  
4 12     12   56 use strict;
  12         25  
  12         5307  
5              
6 12     12   64 use Exporter;
  12         25  
  12         508  
7 12     12   60 use Carp qw();
  12         20  
  12         202  
8 12     12   62 use File::Spec qw();
  12         19  
  12         1240  
9              
10             BEGIN {
11             # We must define these as soon as possible. They are used in other
12             # WWW::AUR modules. Like the ones we use after this block...
13              
14 12     12   35 our $VERSION = '0.18';
15 12         36 our $BASEPATH = '/tmp/WWW-AUR';
16 12         50 our $HOST = 'aur.archlinux.org';
17 12         21 our $UA = 'WWW::AUR::UserAgent';
18              
19 12         197 our @ISA = qw(Exporter);
20 12         256 our @EXPORT_OK = qw(_is_path_param _path_params
21             _category_name _category_index
22             _useragent);
23             }
24              
25 12     12   3897 use WWW::AUR::RPC;
  12         29  
  12         2189  
26              
27             #---CONSTRUCTOR---
28             sub new
29             {
30 2     2 0 24 my $class = shift;
31 2         9 return bless { _path_params( @_ ) }, $class
32             }
33              
34             #---PUBLIC METHOD---
35             sub search
36             {
37 2     2 1 520 my ($self, $query) = @_;
38 2         14 my $found_ref = WWW::AUR::RPC::search( $query );
39              
40 2         1060 require WWW::AUR::Package;
41 4550         21785 return map {
42 2         30 WWW::AUR::Package->new( $_->{name}, info => $_, %$self );
43             } @$found_ref;
44             }
45              
46             #---HELPER FUNCTION---
47             sub _def_wrapper_method
48             {
49 48     48   87 my ($name, $class) = @_;
50              
51 12     12   74 no strict 'refs';
  12         26  
  12         7549  
52 48         1814 *{ "WWW::AUR::$name" } = sub {
53 2     2   9 my $self = shift;
54 2         641 eval "require $class";
55 2 50       15 if ( $@ ) {
56 0         0 Carp::confess "Failed to load $class module:\n$@";
57             }
58 2         5 return eval { $class->new( @_, %$self ) };
  2         29  
59 48         193 };
60             }
61              
62             _def_wrapper_method( 'find' => 'WWW::AUR::Package' );
63             _def_wrapper_method( 'maintainer' => 'WWW::AUR::Maintainer' );
64             _def_wrapper_method( 'iter' => 'WWW::AUR::Iterator' );
65             _def_wrapper_method( 'login' => 'WWW::AUR::Login' );
66              
67             #-----------------------------------------------------------------------------
68             # UTILITY FUNCTIONS
69             #-----------------------------------------------------------------------------
70             # These functions are used internally by other WWW::AUR modules...
71              
72             my %_IS_PATH_PARAM = map { ( $_ => 1 ) }
73             qw/ basepath dlpath extpath destpath /;
74              
75             #---INTERNAL FUNCTION---
76             sub _is_path_param
77             {
78 23746     23746   24571 my ($name) = @_;
79 23746         77941 return $_IS_PATH_PARAM{ $name };
80             }
81              
82             #---INTERNAL FUNCTION---
83             sub _path_params
84             {
85 4677     4677   14209 my @filterme = @_;
86 4677         5182 my %result;
87              
88             FILTER_LOOP:
89 4677         10904 while ( my $key = shift @filterme ) {
90 23746 100       37481 next unless _is_path_param( $key );
91 13908 50       30436 my $val = shift @filterme or last FILTER_LOOP;
92 13908         42451 $result{ $key } = $val;
93             }
94              
95             # Fill path parameters with default values if they are unspecified...
96 4677         4911 our $BASEPATH;
97 4677   66     11824 my $base = $result{ 'basepath' } || $BASEPATH;
98 4677         104288 return ( 'dlpath' => File::Spec->catdir( $base, 'src' ),
99             'extpath' => File::Spec->catdir( $base, 'build' ),
100             'destpath' => File::Spec->catdir( $base, 'cache' ),
101             %result );
102             }
103              
104             my @_CATEGORIES = qw{ daemons devel editors emulators games gnome
105             i18n kde lib modules multimedia network office
106             science system x11 xfce kernels fonts };
107              
108             #---INTERNAL FUNCTION---
109             sub _category_name
110             {
111 6868     6868   22959 my ($i) = @_;
112 6868         8232 $i -= 2;
113 6868 100 66     30277 if ( $i >= 0 && $i <= $#_CATEGORIES ) {
114 6612         21948 return $_CATEGORIES[$i];
115             } else {
116 256         911 return 'undefined';
117             }
118             }
119              
120             #---INTERNAL FUNCTION---
121             sub _category_index
122             {
123 0     0   0 my ($name) = @_;
124 0         0 $name = lc $name;
125              
126 0         0 for my $i ( 0 .. $#_CATEGORIES ) {
127 0 0       0 return 2 + $i if $name eq $_CATEGORIES[ $i ];
128             }
129              
130 0         0 Carp::croak "$name is not a valid category name";
131             }
132              
133             #---INTERNAL FUNCTION---
134             # Create a user-agent object. The class name is specified in $UA.
135             sub _useragent
136             {
137 126     126   1463 our $UA;
138 126 50       71888 eval "require $UA" or die;
139 126         1659 return $UA->new(@_);
140             }
141              
142             1;