Lookup.pm

filter: Statementstmt 83.9% 99 / 118 Branchbran 62.5% 30 / 48 Conditioncond 52.3% 11 / 21 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 100% 17 / 17 Podpod 100% 2 / 2 totaltotal 73.5% 161 / 219 CC 33 SCAR 25.8
CC
33
Cov
75%
search
21.1
new
20.2
_create_url
10.0
CRAP 13.2
↑ Summary Lookup/obituaries.pm »
1 package Genealogy::Obituary::Lookup;
2  
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Data::Reuse;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Genealogy::Obituary::Lookup::obituaries;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Module::Info;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Object::Configure 0.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Params::Get 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Params::Validate::Strict 0.09;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Return::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 use constant URLS => {
18 # 'M' => "https://mlarchives.rootsweb.com/listindexes/emails?listname=gen-obit&page=",
19 'M' => 'https://wayback.archive-it.org/20669/20231102044925/https://mlarchives.rootsweb.com/listindexes/emails?listname=gen-obit&page=',
20 'F' => 'https://www.freelists.org/post/obitdailytimes/Obituary-Daily-Times-',
21 6 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 NAME
24  
25 Genealogy::Obituary::Lookup - Lookup an obituary
26  
27 =head1 VERSION
28  
29 Version 0.20
30  
31 =cut
32  
33 6 our $VERSION = '0.20';
34  
35 # Class-level constants
36 use constant {
37 6 DEFAULT_CACHE_DURATION => '1 day', # The database is updated daily
38 MIN_LAST_NAME_LENGTH => 1,
39 MAX_LAST_NAME_LENGTH => 100,
40 6 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 =head1 SYNOPSIS
43  
44 Looks up obituaries
45  
46 use Genealogy::Obituary::Lookup;
47 my $info = Genealogy::Obituary::Lookup->new();
48 # ...
49  
50 =head1 SUBROUTINES/METHODS
51  
52 =head2 new
53  
54 Creates a Genealogy::Obituary::Lookup object.
55  
56 my $obits = Genealogy::Obituary::Lookup->new();
57  
58 Accepts the following optional arguments:
59  
60 =over 4
61  
62 =item * C<cache> - Passed to L<Database::Abstraction>
63  
64 =item * C<config_file>
65  
66 Points to a configuration file which contains the parameters to C<new()>.
67 The file can be in any common format including C<YAML>, C<XML>, and C<INI>.
68 This allows the parameters to be set at run time.
69  
70 =item * C<directory>
71  
72 The directory containing the file obituaries.sql.
73 If only one argument is given to C<new()>, it is taken to be C<directory>.
74  
75 =item * C<logger> - Passed to L<Database::Abstraction>
76  
77 =back
78  
79 =cut
80  
81 sub new
82 {
83 18 ▶ my $class = shift;
Subroutine new: called CC=16 SCAR=30.1
Pod documented
84 18 my %args;
85  
86 # Handle hash or hashref arguments
87 18 ▶ if((scalar(@_) == 1) && !ref($_[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1 and not ref $_[0]) { } 0 18 18
elsif (my $params = Params::Get::get_params(undef, @_)) { } 12 6 18
Condition66%2/3
scalar @_ == 1 and not ref $_[0]
!ll&&!rl&&r
1710
MC/DC0%0/2
scalar @_ == 1 and not ref $_[0]
scalar @_ == 1 not ref $_[0]
Truth table66%2/3
A and not(B)
A= scalar @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
88 0 $args{'directory'} = $_[0];
89 } elsif(my $params = Params::Get::get_params(undef, @_)) {
90 12 %args = %{$params};
91 }
92  
93 18 ▶ if(!defined($class)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $class) { } 1 17 18
elsif (builtin::blessed($class)) { } 2 15 17
94 1 ▶ if((scalar keys %args) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %args > 0) 0 1 1
95 # Use Genealogy::Obituary::Lookup->new, not Genealogy::Obituary::Lookup::new
96 0 carp(__PACKAGE__, ' use ->new() not ::new() to instantiate');
97 0 return;
98 }
99  
100 # FIXME: this only works when no arguments are given
101 1 $class = __PACKAGE__;
102 } elsif(Scalar::Util::blessed($class)) {
103 # If $class is an object, clone it with new arguments
104 2 return bless { %{$class}, %args }, ref($class);
105 }
106  
107 # Load the configuration from a config file, if provided
108 16 %args = %{Object::Configure::configure($class, \%args)};
109  
110 # Validate logger object has required methods
111 15 ▶ if(defined $args{'logger'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"logger"}) 15 0 15
112 15 ▶ unless(Scalar::Util::blessed($args{'logger'}) && $args{'logger'}->can('info') && $args{'logger'}->can('error')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($args{"logger"}) and $args{"logger"}->can("info") and $args{"logger"}->can("error")) 0 15 15
Condition33%2/6
builtin::blessed($args{"logger"}) and $args{"logger"}->can("info") and $args{"logger"}->can("error")
!ll&&!rl&&r
0015
builtin::blessed($args{"logger"}) and $args{"logger"}->can("info")
!ll&&!rl&&r
0015
MC/DC0%0/3
builtin::blessed($args{"logger"}) and $args{"logger"}->can("info") and $args{"logger"}->can("error")
builtin::blessed($args{"logger"}) $args{"logger"}->can("info") $args{"logger"}->can("error")
Truth table25%1/4
A and B and C
A= builtin::blessed($args{"logger"})B= $args{"logger"}->can("info")C= $args{"logger"}->can("error")
ABCresult
0XX0
10X0
1100
1111
113 0 Carp::croak("Logger must be an object with info() and error() methods");
114 }
115 }
116  
117 15 ▶ if(!defined(my $directory = ($args{'directory'} || $Genealogy::Obituary::Lookup::obituaries->{'directory'}))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined(my $directory = $args{"directory"} || $$Genealogy::Obituary::Lookup::obituaries{"directory"})) 7 8 15
Condition66%2/3
$args{'directory'} || $$Genealogy::Obituary::Lookup::obituaries{'directory'}
l!l&&r!l&&!r
807
MC/DC50%1/2
$args{'directory'} || $$Genealogy::Obituary::Lookup::obituaries{'directory'}
$args{'directory'} $$Genealogy::Obituary::Lookup::obituaries{'directory'}
Truth table66%2/3
A || B
A= $args{'directory'}B= $$Genealogy::Obituary::Lookup::obituaries{'directory'}
ABresult
1X1
011
000
118 # If the directory argument isn't given, see if we can find the data
119 7 $directory = Module::Info->new_from_loaded($class)->file();
120 7 $directory =~ s/\.pm$//;
121 7 $args{'directory'} = File::Spec->catfile($directory, 'data');
122 }
123  
124 15 ▶ unless((-d $args{'directory'}) && (-r $args{'directory'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $args{"directory"} and -r $args{"directory"}) 3 12 15
Condition66%2/3
-d $args{"directory"} and -r $args{"directory"}
!ll&&!rl&&r
3012
MC/DC50%1/2
-d $args{"directory"} and -r $args{"directory"}
-d $args{"directory"} -r $args{"directory"}
Truth table66%2/3
A and B
A= -d $args{"directory"}B= -r $args{"directory"}
ABresult
0X0
100
111
125 3 ▶ if(my $logger = $args{'logger'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $logger = $args{"logger"}) 3 0 3
126 3 $logger->warn("$class: $args{directory} is not a directory");
127 }
128 3 Carp::carp("$class: $args{directory} is not a directory");
129 3 return;
130 }
131  
132 # cache_duration can be overridden by the args
133 12 return bless {
134 cache_duration => DEFAULT_CACHE_DURATION,
135 %args,
136 }, $class;
137 }
138  
139 =head2 search
140  
141 Searches the database.
142  
143 # Returns an array of hashrefs
144 my @smiths = $obits->search(last => 'Smith'); # You must at least define the last name to search for
145  
146 print $smiths[0]->{'first'}, "\n";
147  
148 Supports two return modes:
149  
150 =over 4
151  
152 =item * C<List context>
153  
154 Returns an array of hash references.
155  
156 =item * C<Scalar context>
157  
158 Returns a single hash reference,
159 or C<undef> if there is no match.
160  
161 =back
162  
163 =head3 FORMAL SPECIFICATION
164  
165 =head4 INPUT
166  
167 {
168 'last' => {
169 type => 'string',
170 min => 1,
171 max => 100,
172 matches => qr/^[\w\-]+$/ # Allow hyphens in surnames
173 }, 'first' => {
174 type => 'string',
175 optional => 1,
176 min => 1,
177 max => 100
178 }, 'middle' => {
179 type => 'string',
180 optional => 1,
181 min => 1,
182 max => 100
183 }, 'age' => {
184 type => 'integer',
185 optional => 1,
186 min => 0,
187 max => 120
188 }
189 }
190  
191 =head4 OUTPUT
192  
193 Argument error: croak
194 No matches found: undef
195  
196 Scalar context:
197  
198 {
199 'type' => 'hashref',
200 'min' => 1
201 }
202  
203 List context:
204  
205 {
206 'type' => 'array',
207 'min' => 1
208 }
209  
210 =cut
211  
212 sub search
213 {
214 4 ▶ my $self = shift;
Subroutine search: called CC=11 SCAR=30.5
Pod documented
215  
216 # Ensure $self is valid
217 4 ▶ Carp::croak('search() must be called on an object') unless(Scalar::Util::blessed($self));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 4 4
218  
219 4 my $params = Params::Validate::Strict::validate_strict({
220 args => Params::Get::get_params('last', @_),
221 schema => {
222 'last' => {
223 type => 'string',
224 min => 1,
225 max => 100,
226 matches => qr/^[\w\-]+$/ # Allow hyphens in surnames
227 }, 'first' => {
228 type => 'string',
229 optional => 1,
230 min => 1,
231 max => 100
232 }, 'middle' => {
233 type => 'string',
234 optional => 1,
235 min => 1,
236 max => 100
237 }, 'age' => {
238 type => 'integer',
239 optional => 1,
240 min => 0,
241 max => 120
242 }
243 }
244 });
245  
246 # Validate required parameters thoroughly
247 3 ▶ unless((defined($params->{'last'})) && (length($params->{'last'}) > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$params{"last"} and length $$params{"last"} > 0) 0 3 3
Condition33%1/3
defined $$params{"last"} and length $$params{"last"} > 0
!ll&&!rl&&r
003
MC/DC0%0/2
defined $$params{"last"} and length $$params{"last"} > 0
defined $$params{"last"} length $$params{"last"} > 0
Truth table33%1/3
A and B
A= defined $$params{"last"}B= length $$params{"last"} > 0
ABresult
0X0
100
111
248 0 Carp::carp("Value for 'last' is mandatory");
249 0 return;
250 }
251  
252 # Sanitize input to prevent SQL injection
253 3 $params->{'last'} =~ s/[^\w\s\-']//g; # Allow only word chars, spaces, hyphens, apostrophes
254  
255 3 ▶ $self->{'obituaries'} ||= Genealogy::Obituary::Lookup::obituaries->new(no_entry => 1, no_fixate => 1, %{$self});
Condition66%2/3
$$self{"obituaries"} ||= "Genealogy::Obituary::Lookup::obituaries"->new("no_entry", 1, "no_fixate", 1, %{$self;})
l!l&&r!l&&!r
120
MC/DC0%0/2
$$self{"obituaries"} ||= "Genealogy::Obituary::Lookup::obituaries"->new("no_entry", 1, "no_fixate", 1, %{$self;})
$$self{"obituaries"} "Genealogy::Obituary::Lookup::obituaries"->new("no_entry", 1, "no_fixate", 1, %{$self;})
Truth table66%2/3
A ||= B
A= $$self{"obituaries"}B= "Genealogy::Obituary::Lookup::obituaries"->new("no_entry", 1, "no_fixate", 1, %{$self;})
ABresult
1X1
011
000
256  
257 3 ▶ if(!defined($self->{'obituaries'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$self{"obituaries"}) 0 3 3
258 0 Carp::croak("Can't open the obituaries database");
259 }
260  
261 3 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 3 0 3
262 3 ▶ if(my $obituaries = $self->{'obituaries'}->selectall_hashref($params)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obituaries = $$self{"obituaries"}->selectall_hashref($params)) 3 0 3
263 3 foreach my $obit(@{$obituaries}) {
264 6 ▶ $obit->{'url'} = _create_url($obit) if(defined($obit));
Branch: 1 branch
BranchTrueFalseTotal
if defined $obit 6 0 6
265 }
266 3 my @rc = grep { defined $_ } @{$obituaries};
267 3 Data::Reuse::fixate(@rc);
268 3 return @rc;
269 }
270 0 return;
271 }
272 0 ▶ if(defined(my $obit = $self->{'obituaries'}->fetchrow_hashref($params))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $obit = $$self{"obituaries"}->fetchrow_hashref($params))) 0 0 0
273 0 $obit->{'url'} = _create_url($obit);
274 0 Data::Reuse::fixate(%{$obit});
275  
276 0 return Return::Set::set_return($obit, { 'type' => 'hashref', 'min' => 1 });
277 }
278 0 return; # undef
279 }
280  
281 sub _create_url {
282 10 ▶ my $obit = shift;
Subroutine _create_url: called CC=8 SCAR=23.0
283 10 my $source = $obit->{'source'};
284 10 my $page = $obit->{'page'};
285  
286 10 ▶ if(!defined($page)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page) 1 9 10
287 # use Data::Dumper;
288 # ::diag(Data::Dumper->new([$obit])->Dump());
289 1 Carp::croak(__PACKAGE__, ': undefined $page');
290 }
291 9 ▶ if(!defined($source)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $source) 1 8 9
292 1 Carp::croak(__PACKAGE__, ": $page: undefined source");
293 }
294  
295 8 ▶ if($source eq 'M') {
Branch: 1 branch
BranchTrueFalseTotal
if ($source eq "M") 4 4 8
296 4 return URLS->{'M'} . $page;
297 }
298 4 ▶ if($source eq 'F') {
Branch: 1 branch
BranchTrueFalseTotal
if ($source eq "F") 3 1 4
299 3 return URLS->{'F'} . $page;
300 }
301 1 ▶ if($source eq 'L') {
Branch: 1 branch
BranchTrueFalseTotal
if ($source eq "L") 0 1 1
302 0 ▶ if($obit->{'newspaper'} =~ /^https?:\/\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$obit{"newspaper"} =~ m[^https?://]) 0 0 0
303 0 return $obit->{'newspaper'};
304 }
305 0 ▶ if($obit->{'page'} =~ /^https?:\/\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$obit{"page"} =~ m[^https?://]) 0 0 0
306 0 return $obit->{'page'};
307 }
308 0 Carp::croak(__PACKAGE__, ": undefined newspaper. Newspaper much be given when source type is 'L'");
309 }
310 1 Carp::croak(__PACKAGE__, ": Invalid source, '$source'. Valid sources are 'M', 'F' and 'L'");
311 }
312  
313 =head1 AUTHOR
314  
315 Nigel Horne, C<< <njh at bandsman.co.uk> >>
316  
317 =head1 BUGS
318  
319 Ancestry has removed the archives.
320 The first 18 pages are on Wayback machine, but the rest is lost.
321  
322 =head1 SEE ALSO
323  
324 L<Database::Abstraction>
325  
326 =over 4
327  
328 =item * The Obituary Daily Times
329  
330 L<https://sites.rootsweb.com/~obituary/>
331  
332 =item * Archived Rootsweb data
333  
334 L<https://wayback.archive-it.org/20669/20231102044925/https://mlarchives.rootsweb.com/listindexes/emails?listname=gen-obit>
335  
336 =item * Funeral Notices
337  
338 L<https://www.funeral-notices.co.uk>
339  
340 =item * Recent data
341  
342 L<https://www.freelists.org/list/obitdailytimes>
343  
344 =item * Older data
345  
346 L<https://obituaries.rootsweb.com/obits/searchObits>
347  
348 =back
349  
350 =head1 SUPPORT
351  
352 This module is provided as-is without any warranty.
353  
354 You can find documentation for this module with the perldoc command.
355  
356 perldoc Genealogy::Obituary::Lookup
357  
358 You can also look for information at:
359  
360 =over 4
361  
362 =item * MetaCPAN
363  
364 L<https://metacpan.org/release/Genealogy-Obituary-Lookup>
365  
366 =item * RT: CPAN's request tracker
367  
368 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Genealogy-Obituary-Lookup>
369  
370 =item * CPAN Testers' Matrix
371  
372 L<http://matrix.cpantesters.org/?dist=Genealogy-Obituary-Lookup>
373  
374 =item * CPAN Testers Dependencies
375  
376 L<http://deps.cpantesters.org/?module=Genealogy::Obituary::Lookup>
377  
378 =back
379  
380 =head1 LICENSE AND COPYRIGHT
381  
382 Copyright 2020-2025 Nigel Horne.
383  
384 This program is released under the following licence: GPL2
385  
386 =cut
387  
388 6 1;
↑ Summary Lookup/obituaries.pm »