blib/lib/Net/EANSearch.pm

filter: Statementstmt 34.1% 54 / 158 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 31.8% 7 / 22 Podpod 93.3% 14 / 15 totaltotal 29.3% 75 / 256 CC 24 SCAR 25.1
CC
24
Cov
26%
_apiCall
42.0
isbnLookup
12.0
barcodePrefixSearch
12.0
CRAP 12.2
↑ Summary
1 package Net::EANSearch;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use URL::Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 require Exporter;
12  
13 1 our @ISA = qw(Exporter);
14  
15 # Items to export into callers namespace by default. Note: do not export
16 # names by default without a very good reason. Use EXPORT_OK instead.
17 # Do not simply export all your public functions/methods/constants.
18  
19 # This allows declaration use Net::EANSearch ':all';
20 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
21 # will save memory.
22 1 our %EXPORT_TAGS = ( 'all' => [ qw(
23
24 ) ] );
25  
26 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
27  
28 1 our @EXPORT = qw(
29
30 );
31  
32 1 our $VERSION = '1.23';
33  
34 1 our $ALL_LANGUAGES = 99;
35 1 our $ENGLISH = 1;
36 1 our $DANISH = 2;
37 1 our $GERMAN = 3;
38 1 our $SPANISH = 4;
39 1 our $FINISH = 5;
40 1 our $FRENCH = 6;
41 1 our $HUNGARIAN = 7;
42 1 our $ITALIAN = 8;
43 1 our $JAPANESE = 9;
44 1 our $DUTCH = 10;
45 1 our $NORWEGIAN = 11;
46 1 our $POLISH = 12;
47 1 our $PORTGUESE = 13;
48 1 our $SWEDISH = 15;
49 1 our $CHECH = 16;
50 1 our $CROATIAN = 18;
51 1 our $ROMAINAN = 19;
52 1 our $BULGARIAN = 20;
53 1 our $GREEK = 21;
54  
55 1 my $BASE_URI = 'https://api.ean-search.org/api?format=json&token=';
56 1 my $MAX_API_TRIES = 3; # retry, eg. on 429 error
57  
58 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
59 1 my $class = shift;
60 1 my $token = shift;
61  
62 1 my $ua = LWP::UserAgent->new(agent => "perl-eansearch/$VERSION");
63 1 $ua->timeout(30);
64  
65 1 my $self = bless { base_uri => $BASE_URI . $token, ua => $ua, remaining => -1 }, $class;
66  
67 1 return $self;
68 }
69  
70 0 ▶sub barcodeLookup {
Subroutine barcodeLookup: not called CC=2 SCAR=17.9
Pod documented
71 0 my $self = shift;
72 0 my $ean = shift;
73 0 ▶ my $lang = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
74  
75 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=barcode-lookup&ean=$ean&language=$lang");
76 0 my $json = decode_json($json_str);
77 0 return $json->[0];
78 }
79  
80 0 ▶sub isbnLookup {
Subroutine isbnLookup: not called CC=3 SCAR=24.8
Pod documented
81 0 my $self = shift;
82 0 my $isbn = shift;
83 0 ▶ my $lang = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
84  
85 0 $isbn =~ s/[^0-9X]//g; # remove any non-numeric characters (and X for ISBN-10)
86 0 ▶ if (length($isbn) == 13) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $isbn == 13) 0 0 0
87 0 return $self->barcodeLookup($isbn, $lang); # handle ISBN-13 as EAN
88 }
89 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=barcode-lookup&isbn=$isbn&language=$lang");
90 0 my $json = decode_json($json_str);
91 0 return $json->[0];
92 }
93  
94 0 ▶sub findAsinForEan {
Subroutine findAsinForEan: not called CC=2 SCAR=17.9
Pod documented
95 0 my $self = shift;
96 0 my $ean = shift;
97  
98 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=asin-for-ean-lookup&ean=$ean");
99 0 my $json = decode_json($json_str);
100 0 ▶ return defined($json->[0]) ? $json->[0]->{asin} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $json->[0] ? : 0 0 0
101 }
102  
103 0 ▶sub findEanForAsin {
Subroutine findEanForAsin: not called CC=2 SCAR=17.9
Pod documented
104 0 my $self = shift;
105 0 my $asin = shift;
106  
107 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=ean-for-asin-lookup&asin=$asin");
108 0 my $json = decode_json($json_str);
109 0 ▶ return defined($json->[0]) ? $json->[0]->{ean} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $json->[0] ? : 0 0 0
110 }
111  
112 0 ▶sub findLccnForEan {
Subroutine findLccnForEan: not called CC=2 SCAR=17.9
Pod documented
113 0 my $self = shift;
114 0 my $ean = shift;
115  
116 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=lccn-for-ean-lookup&ean=$ean");
117 0 my $json = decode_json($json_str);
118 0 ▶ return defined($json->[0]) ? $json->[0]->{lccn} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $json->[0] ? : 0 0 0
119 }
120  
121 0 ▶sub findEanForLccn {
Subroutine findEanForLccn: not called CC=2 SCAR=17.9
Pod documented
122 0 my $self = shift;
123 0 my $lccn = shift;
124  
125 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=ean-for-lccn-lookup&lccn=$lccn");
126 0 my $json = decode_json($json_str);
127 0 ▶ return defined($json->[0]) ? $json->[0]->{ean} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $json->[0] ? : 0 0 0
128 }
129  
130 0 ▶sub barcodePrefixSearch {
Subroutine barcodePrefixSearch: not called CC=3 SCAR=24.8
Pod documented
131 0 my $self = shift;
132 0 my $prefix = shift;
133 0 ▶ my $lang = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
134 0 ▶ my $page = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
135  
136 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=barcode-prefix-search&page=$page&language=$lang&prefix=$prefix");
137 0 my $json = decode_json($json_str);
138 0 return @{ $json->{productlist} };
139 }
140  
141 0 ▶sub productSearch {
Subroutine productSearch: not called CC=3 SCAR=24.8
Pod documented
142 0 my $self = shift;
143 0 my $kw = shift;
144 0 ▶ my $lang = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
145 0 ▶ my $page = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
146  
147 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=product-search&page=$page&language=$lang&name="
148 . URL::Encode::url_encode_utf8($kw));
149 0 my $json = decode_json($json_str);
150 0 return @{ $json->{productlist} };
151 }
152  
153 0 ▶sub similarProductSearch {
Subroutine similarProductSearch: not called CC=3 SCAR=24.8
Pod documented
154 0 my $self = shift;
155 0 my $kw = shift;
156 0 ▶ my $lang = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
157 0 ▶ my $page = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
158  
159 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=similar-product-search&page=$page&language=$lang&name="
160 . URL::Encode::url_encode_utf8($kw));
161 0 my $json = decode_json($json_str);
162 0 return @{ $json->{productlist} };
163 }
164  
165 0 ▶sub categorySearch {
Subroutine categorySearch: not called CC=3 SCAR=24.8
Pod documented
166 0 my $self = shift;
167 0 my $category = shift;
168 0 my $kw = shift;
169 0 ▶ my $lang = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
170 0 ▶ my $page = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
171  
172 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=category-search&category=$category"
173 . "&page=$page&language=$lang&name=" . URL::Encode::url_encode_utf8($kw));
174 0 my $json = decode_json($json_str);
175 0 return @{ $json->{productlist} };
176 }
177  
178 0 ▶sub issuingCountry {
Subroutine issuingCountry: not called CC=1 SCAR=6.9
Pod documented
179 0 my $self = shift;
180 0 my $ean = shift;
181  
182 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=issuing-country&ean=$ean");
183 0 my $json = decode_json($json_str);
184 0 return $json->[0]->{issuingCountry};
185 }
186  
187 0 ▶sub barcodeImage {
Subroutine barcodeImage: not called CC=3 SCAR=24.8
Pod documented
188 0 my $self = shift;
189 0 my $ean = shift;
190 0 ▶ my $width = shift || 102;
Condition0%0/2
shift() || 102
l!l
00
MC/DC0%0/1
shift() || 102
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
191 0 ▶ my $height = shift || 50;
Condition0%0/2
shift() || 50
l!l
00
MC/DC0%0/1
shift() || 50
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
192  
193 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=barcode-image&ean=$ean&width=$width&height=$height");
194 0 my $json = decode_json($json_str);
195 0 return decode_base64($json->[0]->{barcode});
196 }
197  
198 0 ▶sub verifyChecksum {
Subroutine verifyChecksum: not called CC=1 SCAR=6.9
Pod documented
199 0 my $self = shift;
200 0 my $ean = shift;
201  
202 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=verify-checksum&ean=$ean");
203 0 my $json = decode_json($json_str);
204 0 return $json->[0]->{valid} + 0;
205 }
206  
207 0 ▶sub creditsRemaining {
Subroutine creditsRemaining: not called CC=2 SCAR=17.9
Pod undocumented
208 0 my $self = shift;
209  
210 0 ▶ if ($self->{remaining} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"remaining"} < 0) 0 0 0
211 0 $self->_apiCall($self->{base_uri} . "&op=account-status");
212 }
213 0 return $self->{remaining};
214 }
215  
216 0 ▶sub _apiCall {
Subroutine _apiCall: not called CC=6 SCAR=37.4
217 0 my $self = shift;
218 0 my $url = shift;
219 0 my $tries = 0;
220  
221 0 ▶ while ($tries < $MAX_API_TRIES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tries < $MAX_API_TRIES) 0 0 0
222 0 my $response = $self->{ua}->request(HTTP::Request->new(GET => $url));
223 0 $tries++;
224 0 ▶ if (!defined($response) || $response->is_error()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $response or $response->is_error) { } 0 0 0
Condition0%0/3
not defined $response or $response->is_error
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $response or $response->is_error
not defined $response $response->is_error
Truth table0%0/3
not(A) or B
A= not defined $responseB= $response->is_error
ABresult
1X1
011
000
225 0 ▶ if ($response->code == 429) { # auto-retry on 429 (too many requests)
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 429) 0 0 0
226 0 sleep 1;
227 0 next;
228 }
229 0 ▶ print STDERR 'Network error: ' . (defined($response) ? $response->code : 'unknown') . "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $response ? : 0 0 0
230 0 return undef;
231 } else {
232 0 $self->{remaining} = $response->header('X-Credits-Remaining');
233 0 return $response->content;
234 }
235 }
236 0 return undef;
237 }
238  
239 1 1;
240  
241 __END__
↑ Summary