File Coverage

blib/lib/WWW/NameGen.pm
Criterion Covered Total %
statement 29 44 65.9
branch 7 16 43.7
condition 1 3 33.3
subroutine 6 6 100.0
pod 3 3 100.0
total 46 72 63.8


line stmt bran cond sub pod time code
1             # Revision $Revision$ ( $Date$ ) - $Source$
2              
3             package WWW::NameGen;
4              
5 1     1   1072 use warnings;
  1         2  
  1         42  
6 1     1   8 use strict;
  1         2  
  1         40  
7              
8 1     1   1414 use LWP::UserAgent;
  1         52176  
  1         496  
9              
10             our $VERSION = '0.03';
11              
12             my $max = 2_500;
13             my $chunk_size = 250;
14             my @chunks;
15              
16             sub new {
17 1     1 1 1161 my ($class, %args) = @_;
18 1         5 my $self = bless {%args}, $class;
19 1         6 $self->{'ua'} = LWP::UserAgent->new;
20 1         4024 return $self;
21             }
22              
23             sub get_chunks {
24 1     1 1 3 my ($self, $count, @out) = @_;
25 1 50       12 if ($count < $chunk_size) {
26 1         4 push @out, $count;
27             } else {
28 0         0 my $chunks = $count / $chunk_size;
29 0         0 my $remainder = $count % $chunk_size;
30 0         0 map { push @out, $chunk_size } 1 .. $chunks;
  0         0  
31 0 0       0 if ($remainder) { push @out, $remainder; }
  0         0  
32             }
33 1         4 return @out;
34             }
35              
36             sub generate {
37 1     1 1 1142 my ($self, %args) = @_;
38              
39 1 50       6 my $min = $args{'min'} ? $args{'min'} : 10;
40 1 50       3 my $obscurity = $args{'obscurity'} ? $args{'obscurity'} : 30;
41 1 50       4 my $type = $args{'type'} ? $args{'type'} : 3;
42              
43             # return cached results if we can
44 1 50 33     13 if (! $args{'nocache'} && $self->{'__cache'}) { return @{$self->{'__cache'}}; }
  0         0  
  0         0  
45              
46 1         4 my ($count, @names) = (0, qw//);
47 1         7 my @chunks = $self->get_chunks($min);
48              
49 1         3 for my $tcount (@chunks) {
50 1 50       7 if ($count >= $max) { last; }
  0         0  
51 1         11 my $response = $self->{ua}->post(
52             'http://www.kleimo.com/random/name.cfm',
53             {
54             'type' => $type,
55             'number' => $tcount,
56             'obscurity' => $obscurity,
57             'Go' => 'Generate Random Name(s)',
58             }
59             );
60              
61 1 50       74932 if (! $response->is_success) {
62 1         475 die "Error getting data!\n";
63             }
64              
65 0           my $html = $response->content;
66 0           while ($html =~ m!target="_blank">([^<]*)
!gixm ) {
67 0           push @names, $1;
68             }
69 0           $count += $tcount;
70             }
71              
72 0           $self->{'__cache'} = \@names;
73              
74 0           return @names;
75             }
76              
77             1;
78             __END__