blib/lib/WWW/Search/ValentinskaCZ.pm

filter: Statementstmt 42.6% 29 / 68 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 8 / 14 Podpod 100% 2 / 2 totaltotal 43.3% 39 / 90 CC 5 SCAR 14.1
CC
5
Cov
39%
native_retrieve_some
12.0
_fix_url
6.0
_remove_tr
6.0
CRAP 4.1
↑ Summary
1 package WWW::Search::ValentinskaCZ;
2  
3 # Pragmas.
4 3 ▶use base qw(WWW::Search);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Modules.
9 3 ▶use Encode qw(decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Text::Iconv;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Web::Scraper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Constants.
16 3 Readonly::Scalar our $MAINTAINER => 'Michal Spacek <skim@cpan.org>';
17 3 Readonly::Scalar my $VALENTINSKA_CZ => 'http://valentinska.cz/';
18 3 Readonly::Scalar my $VALENTINSKA_CZ_ACTION1 => 'index.php?hledani=Vyhledej&hltex=';
19  
20 # Version.
21 3 our $VERSION = 0.02;
22  
23 # Setup.
24 sub native_setup_search {
25 0 ▶ my ($self, $query) = @_;
Subroutine native_setup_search: not called CC=1 SCAR=6.9
Pod documented
26 $self->{'_def'} = scraper {
27  
28 # Get list of books.
29 process '//table[@class="ProductTable"]/tr/td',
30 'books[]' => scraper {
31  
32 0 ▶ process '//h2/a', 'title' => 'TEXT';
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 process '//h2/a', 'url' => '@href';
34 0 process '//img', 'cover_url' => '@src';
35 0 process '//p[@class="AuthorName"]',
36 'author' => 'TEXT';
37 0 process '//p[@class="BookPrice"]/span',
38 'price' => 'TEXT';
39 0 return;
40 0 ▶ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 0 return;
42 0 };
43 0 $self->{'_query'} = $query;
44 0 return 1;
45 }
46  
47 # Get data.
48 sub native_retrieve_some {
49 0 ▶ my $self = shift;
Subroutine native_retrieve_some: not called CC=3 SCAR=24.8
Pod documented
50  
51 # Query.
52 0 my $i1 = Text::Iconv->new('utf-8', 'windows-1250');
53 0 my $query = $i1->convert(decode_utf8($self->{'_query'}));
54  
55 # Get content.
56 0 my $ua = LWP::UserAgent->new(
57 'agent' => "WWW::Search::KacurCZ/$VERSION",
58 );
59 0 my $response = $ua->get($VALENTINSKA_CZ.
60 $VALENTINSKA_CZ_ACTION1.$query,
61 );
62  
63 # Process.
64 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 0 0 0
65 0 my $i2 = Text::Iconv->new('windows-1250', 'utf-8');
66 0 my $content = $i2->convert($response->content);
67  
68 # Get books structure.
69 0 my $books_hr = $self->{'_def'}->scrape($content);
70  
71 # Process each book.
72 0 foreach my $book_hr (@{$books_hr->{'books'}}) {
73 0 _fix_url($book_hr, 'url');
74 0 _fix_url($book_hr, 'cover_url');
75 0 $self->_remove_tr($book_hr, 'title');
76 0 push @{$self->{'cache'}}, $book_hr;
77 }
78 }
79  
80 0 return;
81 }
82  
83 # Fix URL to absolute path.
84 sub _fix_url {
85 0 ▶ my ($book_hr, $url) = @_;
Subroutine _fix_url: not called CC=2 SCAR=17.9
86 0 ▶ if (exists $book_hr->{$url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $book_hr->{$url}) 0 0 0
87 0 $book_hr->{$url} = $VALENTINSKA_CZ.$book_hr->{$url};
88 }
89 0 return;
90 }
91  
92 # Remove trailing whitespace.
93 sub _remove_tr {
94 0 ▶ my ($self, $book_hr, $key) = @_;
Subroutine _remove_tr: not called CC=2 SCAR=17.9
95 0 ▶ if (! exists $book_hr->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $book_hr->{$key}) 0 0 0
96 0 return;
97 }
98 0 $book_hr->{$key} =~ s/^\s+//gms;
99 0 $book_hr->{$key} =~ s/\s+$//gms;
100 0 return;
101 }
102  
103 3 1;
104  
105 __END__
↑ Summary