| 1 | package NOLookup::BrregDifi::DataLookup; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use base qw(Class::Accessor::Chained); ## Provides a new() method | ||
| 6 | use POSIX qw(locale_h); | ||
| 7 | use NOLookup::BrregDifi::Entry; | ||
| 8 | use WWW::Mechanize; | ||
| 9 | use JSON; | ||
| 10 | use URI::Encode qw(uri_encode); | ||
| 11 | use Data::Dumper; | ||
| 12 | $Data::Dumper::Indent=1; | ||
| 13 | |||
| 14 | my $BRREG_TIMEOUT = 60; # secs (default is 180 secs but we want shorter time). | ||
| 15 | |||
| 16 | # The query can be a name, or an orgnumber. | ||
| 17 | # In both cases an array of 0..n data elements are returned, | ||
| 18 | # each containing the same data. | ||
| 19 | my $BRREG = "http://hotell.difi.no/api/json/brreg/enhetsregisteret"; | ||
| 20 | my $BRREG_ORGNAME = "$BRREG?query"; | ||
| 21 | my $BRREG_ORGNO = "$BRREG?orgnr"; | ||
| 22 | |||
| 23 | my @module_methods = qw / | ||
| 24 | |||
| 25 | lookup_orgno | ||
| 26 | lookup_orgname | ||
| 27 | orgno_ok | ||
| 28 | |||
| 29 | status | ||
| 30 | error | ||
| 31 | warning | ||
| 32 | |||
| 33 | data | ||
| 34 | size | ||
| 35 | total_size | ||
| 36 | |||
| 37 | total_page_count | ||
| 38 | cur_page | ||
| 39 | next_page | ||
| 40 | prev_page | ||
| 41 | |||
| 42 | raw_json_decoded | ||
| 43 | /; | ||
| 44 | |||
| 45 | # The new method and also the accessor methods | ||
| 46 | __PACKAGE__->mk_accessors( | ||
| 47 | @module_methods | ||
| 48 | ); | ||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | ######## L o o k u p | ||
| 53 | |||
| 54 | sub lookup_orgno { | ||
| 55 | my ($self, $orgno) = @_; | ||
| 56 | |||
| 57 | # validate the organizaton number | ||
| 58 | unless (orgno_ok($self, $orgno)) { | ||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | ## | ||
| 63 | # Use the orgno as key to brreg. | ||
| 64 | # Match is only found if number does exist. | ||
| 65 | $self->size(0); | ||
| 66 | $self->_lookup_org_entries("$BRREG_ORGNO=$orgno"); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub lookup_orgname { | ||
| 70 | my ($self, $orgname, $max_no_pages, $page_ix) = @_; | ||
| 71 | |||
| 72 | die "mandatory parameter 'orgname' not specified" unless ($orgname); | ||
| 73 | |||
| 74 | my $max_pg_to_fetch = $max_no_pages || 10; | ||
| 75 | my $pg_ix = $page_ix || 1; | ||
| 76 | |||
| 77 | # Use the orgname as filter in search to brreg | ||
| 78 | my $on_e = uri_encode($orgname, {encode_reserved => 1}); | ||
| 79 | |||
| 80 | # set size and total size so we get the first lookup | ||
| 81 | $self->size(0); | ||
| 82 | $self->total_size(1); | ||
| 83 | my $bsize = -1; | ||
| 84 | |||
| 85 | # Fetch the requested pages | ||
| 86 | my $pix = $pg_ix; | ||
| 87 | my $pcnt = 1; | ||
| 88 | |||
| 89 | while ($pcnt <= $max_pg_to_fetch && $bsize < $self->size && $self->size < $self->total_size) { | ||
| 90 | #print STDERR " size: ", $self->size, "\n"; | ||
| 91 | #print STDERR "tsize: ", $self->total_size, "\n"; | ||
| 92 | #print STDERR "Page count is: $pcnt, fetching page $pix...\n"; | ||
| 93 | #print STDERR "$BRREG_ORGNAME=$on_e&page=$pix\n"; | ||
| 94 | |||
| 95 | # Remember size before next lookup. If size has not increased, stop the loop. | ||
| 96 | $bsize = $self->size; | ||
| 97 | |||
| 98 | $self->_lookup_org_entries("$BRREG_ORGNAME=$on_e&page=$pix"); | ||
| 99 | ++$pcnt; | ||
| 100 | ++$pix; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | sub orgno_ok { | ||
| 105 | my ($self, $orgno) = @_; | ||
| 106 | |||
| 107 | # remove any white spaces | ||
| 108 | $orgno =~ s/\s+//g; | ||
| 109 | |||
| 110 | if ($orgno eq "000000000") { | ||
| 111 | $self->status("Invalid orgno: $orgno"); | ||
| 112 | $self->error(1); | ||
| 113 | return 0; | ||
| 114 | } | ||
| 115 | unless ($orgno =~ m/\d{9}/) { | ||
| 116 | $self->status("Organization number $orgno must be 9 digits"); | ||
| 117 | $self->error(1); | ||
| 118 | return 0; | ||
| 119 | } | ||
| 120 | return 1; | ||
| 121 | } | ||
| 122 | |||
| 123 | ##### | ||
| 124 | # The lookup itself, internal method only. | ||
| 125 | |||
| 126 | sub _lookup_org_entries { | ||
| 127 | my ($self, $URL) = @_; | ||
| 128 | |||
| 129 | #print STDERR "URL: $URL\n"; | ||
| 130 | |||
| 131 | my $mech = WWW::Mechanize->new( | ||
| 132 | timeout => $BRREG_TIMEOUT, | ||
| 133 | autocheck => 0 | ||
| 134 | ); | ||
| 135 | |||
| 136 | my $resp = $mech->get($URL); | ||
| 137 | |||
| 138 | unless ($mech->success) { | ||
| 139 | $self->status($resp->status_line); | ||
| 140 | $self->error(1); | ||
| 141 | |||
| 142 | #print STDERR "ERR mech: ", Dumper $self; | ||
| 143 | |||
| 144 | return $self; | ||
| 145 | } | ||
| 146 | |||
| 147 | my $json; | ||
| 148 | { | ||
| 149 | local $@; # protect existing $@ | ||
| 150 | eval { | ||
| 151 | $json = decode_json($mech->content(format=>'text')); | ||
| 152 | }; | ||
| 153 | if ($@) { | ||
| 154 | # Some error | ||
| 155 | $self->status("Invalid response"); | ||
| 156 | $self->error(1); | ||
| 157 | $self->next_page(undef); | ||
| 158 | return $self; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | if ($json) { | ||
| 163 | $self->raw_json_decoded($json); | ||
| 164 | |||
| 165 | # Map the json data structure to | ||
| 166 | # internal entry objects | ||
| 167 | my $eo = NOLookup::BrregDifi::Entry->new; | ||
| 168 | my $entries = $eo->map_json_entries($json); | ||
| 169 | |||
| 170 | # Collect any accumulated problems | ||
| 171 | $self->status($eo->status) if ($eo->status); | ||
| 172 | $self->warning($eo->warning) if ($eo->warning); | ||
| 173 | $self->error($eo->error) if ($eo->error); | ||
| 174 | |||
| 175 | # Save the found data | ||
| 176 | if (@$entries) { | ||
| 177 | push @{$self->{data}}, @$entries; | ||
| 178 | |||
| 179 | $self->size( $self->size + scalar @$entries); | ||
| 180 | $self->total_size($eo->total_result_count); | ||
| 181 | |||
| 182 | $self->total_page_count($eo->total_page_count); | ||
| 183 | |||
| 184 | $self->cur_page($eo->cur_page); | ||
| 185 | $self->next_page($eo->next_page); | ||
| 186 | $self->prev_page($eo->prev_page); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | return $self; | ||
| 190 | } | ||
| 191 | |||
| 192 | =pod | ||
| 193 | |||
| 194 | =encoding ISO-8859-1 | ||
| 195 | |||
| 196 | =head1 NAME | ||
| 197 | |||
| 198 | NOLookup::BrregDifi::DataLookup - Lookup Brreg basic organization data from the JSON | ||
| 199 | formatted service offered by the Difi Brreg data API via | ||
| 200 | "http://hotell.difi.no/api/json/brreg/enhetsregisteret?query"; | ||
| 201 | |||
| 202 | (Brreg is a short name for 'BrE<0xF8>nnE<0xF8>ysundregistrene, the Norwegian Central Organization Registry). | ||
| 203 | |||
| 204 | =head1 DESCRIPTION | ||
| 205 | |||
| 206 | Use WWW::Mechanize and JSON to lookup data from the JSON API at brreg. | ||
| 207 | Use NOLookup::BrregDifi::Entry module to map the resulting json data structure | ||
| 208 | to a NOLookup::BrregDifi::Entry objects. | ||
| 209 | |||
| 210 | The timeout period $BRREG_TIMEOUT is used to | ||
| 211 | trap and return the call if Brreg lookup is hanging for some reason. | ||
| 212 | |||
| 213 | =head1 METHODS | ||
| 214 | |||
| 215 | The module provides the below methods. | ||
| 216 | |||
| 217 | =head3 lookup_orgno() | ||
| 218 | |||
| 219 | Lookup based on a complete organization number. | ||
| 220 | |||
| 221 | Returns an array of one single NOLookup::Brreg::Entry object in the data | ||
| 222 | when a match is found. | ||
| 223 | |||
| 224 | =head3 lookup_orgname() | ||
| 225 | |||
| 226 | Lookup based on a complete or part of an organization name. | ||
| 227 | |||
| 228 | Mandatory parameters are: | ||
| 229 | $orgname: the orgname (or part of) to search for | ||
| 230 | |||
| 231 | Optional parameters are: | ||
| 232 | $max_no_pages: max. number of pages to return, default 10. | ||
| 233 | $page_ix : index to the first page to fetch, default 1. | ||
| 234 | |||
| 235 | Returns 0 entries if none found. | ||
| 236 | |||
| 237 | Returns an array of NOLookup::Brreg::Entry objects in the data | ||
| 238 | when matches are found. | ||
| 239 | |||
| 240 | Note: A maximum of 5 pages are fetched, each of 100 entries. | ||
| 241 | |||
| 242 | =head3 orgno_ok() | ||
| 243 | |||
| 244 | Check if org. number basic syntax is OK, i.e. 9 digits. | ||
| 245 | Note that Brreg may reject the number due to stricter | ||
| 246 | requirements, like checksum etc. | ||
| 247 | |||
| 248 | =head3 raw_json_decoded() | ||
| 249 | |||
| 250 | The raw JSON data structure, as returned by the JSON service, | ||
| 251 | and then decoded to a perl data structure with decode_json(). | ||
| 252 | |||
| 253 | =head3 error() | ||
| 254 | |||
| 255 | Set if an error has occured. | ||
| 256 | On errors, the lookup terminates, with more info | ||
| 257 | in the status(). | ||
| 258 | |||
| 259 | =head3 warning() | ||
| 260 | |||
| 261 | Set if a warning has occured. | ||
| 262 | On warnings, the lookup continues, but warnings | ||
| 263 | are accumulated in the status(). | ||
| 264 | |||
| 265 | =head3 status() | ||
| 266 | |||
| 267 | Further description of error/warning situations. | ||
| 268 | |||
| 269 | =head3 data() | ||
| 270 | |||
| 271 | Found data, an array of NOLookup::BrregDifi::Entry data objects. | ||
| 272 | |||
| 273 | See doc. for NOLookup::BrregDifi::Entry for details. | ||
| 274 | |||
| 275 | =head3 size() | ||
| 276 | |||
| 277 | The number of objects in data(). | ||
| 278 | |||
| 279 | =head3 total_size() | ||
| 280 | |||
| 281 | The total number of objects in data[]. | ||
| 282 | |||
| 283 | Only set if all data has been fetched. | ||
| 284 | |||
| 285 | Only if total_size is set, the user can assume | ||
| 286 | that all data has been fetched. | ||
| 287 | |||
| 288 | =head3 total_page_count() | ||
| 289 | |||
| 290 | The total number of pages that the query produces. | ||
| 291 | Only if all pages are fetched, you have it all. | ||
| 292 | |||
| 293 | =head3 cur_page() | ||
| 294 | |||
| 295 | The current page number. | ||
| 296 | |||
| 297 | =head3 next_page() | ||
| 298 | |||
| 299 | The next page number, if more exists. | ||
| 300 | |||
| 301 | =head3 prev_page() | ||
| 302 | |||
| 303 | The previous page number, unless you are on the first page. | ||
| 304 | |||
| 305 | =head1 SUPPORT | ||
| 306 | |||
| 307 | For now, support questions should be sent to: | ||
| 308 | |||
| 309 | E<lt>(nospam)info(at)norid.noE<gt> | ||
| 310 | |||
| 311 | Please also see the SUPPORT file in the distribution. | ||
| 312 | |||
| 313 | =head1 SEE ALSO | ||
| 314 | |||
| 315 | http://hotell.difi.no/?dataset=brreg/enhetsregisteret | ||
| 316 | |||
| 317 | =head1 AUTHOR | ||
| 318 | |||
| 319 | Trond Haugen, E<lt>(nospam)info(at)norid.noE<gt> | ||
| 320 | |||
| 321 | =head1 COPYRIGHT | ||
| 322 | |||
| 323 | Copyright (c) 2017 Trond Haugen <(nospam)info(at)norid.no>. | ||
| 324 | All rights reserved. | ||
| 325 | |||
| 326 | This program is free software; you can redistribute it and/or modify | ||
| 327 | it under the terms of the GNU General Public License as published by | ||
| 328 | the Free Software Foundation; either version 2 of the License, or | ||
| 329 | (at your option) any later version. | ||
| 330 | |||
| 331 | =head1 LICENSE | ||
| 332 | |||
| 333 | This library is free software. You can redistribute it and/or modify | ||
| 334 | it under the same terms as Perl itself. | ||
| 335 | |||
| 336 | =cut | ||
| 337 | |||
| 338 | 1; |