blib/lib/Net/EANSearch.pm

filter: Statementstmt 34.8% 54 / 155 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 31.8% 7 / 22 Podpod 66.6% 10 / 15 totaltotal 28.2% 71 / 251 CC 23 SCAR 24.8
CC
23
Cov
27%
_apiCall
42.0
barcodePrefixSearch
12.0
productSearch
12.0
CRAP 12.0
↑ 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.21';
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 sub new {
59 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
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 sub barcodeLookup {
71 0 ▶ my $self = shift;
Subroutine barcodeLookup: not called CC=2 SCAR=17.9
Pod documented
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=asin-for-ean-lookup&ean=$ean&language=$lang");
76 0 my $json = decode_json($json_str);
77 0 return $json->[0];
78 }
79  
80 sub isbnLookup {
81 0 ▶ my $self = shift;
Subroutine isbnLookup: not called CC=2 SCAR=17.9
Pod documented
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 my $json_str = $self->_apiCall($self->{base_uri} . "&op=barcode-lookup&isbn=$isbn&language=$lang");
86 0 my $json = decode_json($json_str);
87 0 return $json->[0];
88 }
89  
90 sub findAsinForEan {
91 0 ▶ my $self = shift;
Subroutine findAsinForEan: not called CC=2 SCAR=17.9
Pod undocumented
92 0 my $ean = shift;
93  
94 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=asin-for-ean-lookup&ean=$ean");
95 0 my $json = decode_json($json_str);
96 0 ▶ return defined($json->[0]) ? $json->[0]->{asin} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $json->[0] ? : 0 0 0
97 }
98  
99 sub findEanForAsin {
100 0 ▶ my $self = shift;
Subroutine findEanForAsin: not called CC=2 SCAR=17.9
Pod undocumented
101 0 my $asin = shift;
102  
103 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=ean-for-asin-lookup&asin=$asin");
104 0 my $json = decode_json($json_str);
105 0 ▶ return defined($json->[0]) ? $json->[0]->{ean} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $json->[0] ? : 0 0 0
106 }
107  
108 sub findLccnForEan {
109 0 ▶ my $self = shift;
Subroutine findLccnForEan: not called CC=2 SCAR=17.9
Pod undocumented
110 0 my $ean = shift;
111  
112 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=lccn-for-ean-lookup&ean=$ean");
113 0 my $json = decode_json($json_str);
114 0 ▶ return defined($json->[0]) ? $json->[0]->{lccn} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $json->[0] ? : 0 0 0
115 }
116  
117 sub findEanForLccn {
118 0 ▶ my $self = shift;
Subroutine findEanForLccn: not called CC=2 SCAR=17.9
Pod undocumented
119 0 my $lccn = shift;
120  
121 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=ean-for-lccn-lookup&lccn=$lccn");
122 0 my $json = decode_json($json_str);
123 0 ▶ return defined($json->[0]) ? $json->[0]->{ean} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $json->[0] ? : 0 0 0
124 }
125  
126 sub barcodePrefixSearch {
127 0 ▶ my $self = shift;
Subroutine barcodePrefixSearch: not called CC=3 SCAR=24.8
Pod documented
128 0 my $prefix = shift;
129 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
130 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
131  
132 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=barcode-prefix-search&page=$page&language=$lang&prefix=$prefix");
133 0 my $json = decode_json($json_str);
134 0 return @{ $json->{productlist} };
135 }
136  
137 sub productSearch {
138 0 ▶ my $self = shift;
Subroutine productSearch: not called CC=3 SCAR=24.8
Pod documented
139 0 my $kw = shift;
140 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
141 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
142  
143 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=product-search&page=$page&language=$lang&name="
144 . URL::Encode::url_encode_utf8($kw));
145 0 my $json = decode_json($json_str);
146 0 return @{ $json->{productlist} };
147 }
148  
149 sub similarProductSearch {
150 0 ▶ my $self = shift;
Subroutine similarProductSearch: not called CC=3 SCAR=24.8
Pod documented
151 0 my $kw = shift;
152 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
153 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
154  
155 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=similar-product-search&page=$page&language=$lang&name="
156 . URL::Encode::url_encode_utf8($kw));
157 0 my $json = decode_json($json_str);
158 0 return @{ $json->{productlist} };
159 }
160  
161 sub categorySearch {
162 0 ▶ my $self = shift;
Subroutine categorySearch: not called CC=3 SCAR=24.8
Pod documented
163 0 my $category = shift;
164 0 my $kw = shift;
165 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
166 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
167  
168 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=category-search&category=$category"
169 . "&page=$page&language=$lang&name=" . URL::Encode::url_encode_utf8($kw));
170 0 my $json = decode_json($json_str);
171 0 return @{ $json->{productlist} };
172 }
173  
174 sub issuingCountry {
175 0 ▶ my $self = shift;
Subroutine issuingCountry: not called CC=1 SCAR=6.9
Pod documented
176 0 my $ean = shift;
177  
178 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=issuing-country&ean=$ean");
179 0 my $json = decode_json($json_str);
180 0 return $json->[0]->{issuingCountry};
181 }
182  
183 sub barcodeImage {
184 0 ▶ my $self = shift;
Subroutine barcodeImage: not called CC=3 SCAR=24.8
Pod documented
185 0 my $ean = shift;
186 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
187 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
188  
189 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=barcode-image&ean=$ean&width=$width&height=$height");
190 0 my $json = decode_json($json_str);
191 0 return decode_base64($json->[0]->{barcode});
192 }
193  
194 sub verifyChecksum {
195 0 ▶ my $self = shift;
Subroutine verifyChecksum: not called CC=1 SCAR=6.9
Pod documented
196 0 my $ean = shift;
197  
198 0 my $json_str = $self->_apiCall($self->{base_uri} . "&op=verify-checksum&ean=$ean");
199 0 my $json = decode_json($json_str);
200 0 return $json->[0]->{valid} + 0;
201 }
202  
203 sub creditsRemaining {
204 0 ▶ my $self = shift;
Subroutine creditsRemaining: not called CC=2 SCAR=17.9
Pod undocumented
205  
206 0 ▶ if ($self->{remaining} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"remaining"} < 0) 0 0 0
207 0 $self->_apiCall($self->{base_uri} . "&op=account-status");
208 }
209 0 return $self->{remaining};
210 }
211  
212 sub _apiCall {
213 0 ▶ my $self = shift;
Subroutine _apiCall: not called CC=6 SCAR=37.4
214 0 my $url = shift;
215 0 my $tries = 0;
216  
217 0 ▶ while ($tries < $MAX_API_TRIES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tries < $MAX_API_TRIES) 0 0 0
218 0 my $response = $self->{ua}->request(HTTP::Request->new(GET => $url));
219 0 $tries++;
220 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
221 0 ▶ if ($response->code == 429) { # auto-retry on 429 (too many requests)
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 429) 0 0 0
222 0 sleep 1;
223 0 next;
224 }
225 0 ▶ print STDERR 'Network error: ' . (defined($response) ? $response->code : 'unknown') . "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $response ? : 0 0 0
226 0 return undef;
227 } else {
228 0 $self->{remaining} = $response->header('X-Credits-Remaining');
229 0 return $response->content;
230 }
231 }
232 0 return undef;
233 }
234  
235 1 1;
236  
237 __END__
↑ Summary