| 1 | package NOLookup::BrregDifi::Entry; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use base qw(Class::Accessor::Chained); ## provides a new() method | ||
| 6 | use Encode; | ||
| 7 | use POSIX qw(locale_h); | ||
| 8 | |||
| 9 | use Data::Dumper; | ||
| 10 | $Data::Dumper::Indent=1; | ||
| 11 | |||
| 12 | # module methods | ||
| 13 | my @module_methods = ( | ||
| 14 | |||
| 15 | qw / | ||
| 16 | status | ||
| 17 | error | ||
| 18 | warning | ||
| 19 | |||
| 20 | map_json_entries | ||
| 21 | |||
| 22 | next_page | ||
| 23 | cur_page | ||
| 24 | prev_page | ||
| 25 | total_page_count | ||
| 26 | |||
| 27 | result_count | ||
| 28 | total_result_count | ||
| 29 | |||
| 30 | /); | ||
| 31 | |||
| 32 | my @json_data_methods = ( | ||
| 33 | |||
| 34 | qw / | ||
| 35 | nkode2 | ||
| 36 | nkode1 | ||
| 37 | ansatte_dato | ||
| 38 | tlf_mobil | ||
| 39 | organisasjonsform | ||
| 40 | ppoststed | ||
| 41 | tvangsavvikling | ||
| 42 | forretningsadr | ||
| 43 | regifriv | ||
| 44 | orgnr | ||
| 45 | forradrland | ||
| 46 | stiftelsesdato | ||
| 47 | forradrkommnr | ||
| 48 | konkurs | ||
| 49 | regdato | ||
| 50 | avvikling | ||
| 51 | hovedenhet | ||
| 52 | regifr | ||
| 53 | forradrpoststed | ||
| 54 | ppostland | ||
| 55 | forradrkommnavn | ||
| 56 | forradrpostnr | ||
| 57 | navn | ||
| 58 | regnskap | ||
| 59 | url | ||
| 60 | ansatte_antall | ||
| 61 | sektorkode | ||
| 62 | regimva | ||
| 63 | ppostnr | ||
| 64 | postadresse | ||
| 65 | nkode3 | ||
| 66 | regiaa | ||
| 67 | tlf | ||
| 68 | |||
| 69 | / ); | ||
| 70 | |||
| 71 | # The new method and also the accessor methods | ||
| 72 | __PACKAGE__->mk_accessors( | ||
| 73 | @module_methods, | ||
| 74 | @json_data_methods | ||
| 75 | ); | ||
| 76 | |||
| 77 | sub map_json_entries { | ||
| 78 | my ($self, $json) = @_; | ||
| 79 | |||
| 80 | return unless ($json); | ||
| 81 | |||
| 82 | my @aj; | ||
| 83 | my @ao; | ||
| 84 | |||
| 85 | #print Dumper $json; | ||
| 86 | |||
| 87 | $self->status(""); | ||
| 88 | |||
| 89 | if ($json->{posts}) { | ||
| 90 | |||
| 91 | push @aj, @{$json->{entries}}; | ||
| 92 | |||
| 93 | # count the current posts | ||
| 94 | $self->result_count(scalar @{$json->{entries}}); | ||
| 95 | $self->total_result_count($json->{posts}); | ||
| 96 | |||
| 97 | $self->cur_page($json->{page}); | ||
| 98 | |||
| 99 | if ($json->{page} == 1) { | ||
| 100 | $self->prev_page(1); | ||
| 101 | } else { | ||
| 102 | $self->prev_page($json->{page} - 1); | ||
| 103 | } | ||
| 104 | if ($json->{pages} > $json->{page}) { | ||
| 105 | $self->next_page($json->{page} + 1); | ||
| 106 | } else { | ||
| 107 | $self->next_page(undef); | ||
| 108 | } | ||
| 109 | |||
| 110 | $self->total_page_count($json->{pages}); | ||
| 111 | |||
| 112 | # Check entry array for supported keys | ||
| 113 | # set a warning if some are not expected | ||
| 114 | foreach my $ej (@aj) { | ||
| 115 | foreach my $k (keys %$ej) { | ||
| 116 | unless ($self->can($k)) { | ||
| 117 | $self->status($self->status . "JSON data key entry not expected: $k\n"); | ||
| 118 | $self->warning(1); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | my $eo = NOLookup::BrregDifi::Entry->new($ej); | ||
| 122 | push @ao, $eo; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | # return a ref to the data array | ||
| 126 | return \@ao; | ||
| 127 | } | ||
| 128 | |||
| 129 | =pod | ||
| 130 | |||
| 131 | =encoding ISO-8859-1 | ||
| 132 | |||
| 133 | =head1 NAME | ||
| 134 | |||
| 135 | NOLookup::BrregDifi::Entry - Map a Brreg json data structure element | ||
| 136 | to NOLookup::BrregDifi::Entry data objects. | ||
| 137 | |||
| 138 | =head1 DESCRIPTION | ||
| 139 | |||
| 140 | Map the json data to data objects. | ||
| 141 | |||
| 142 | Return a reference to an array of NOLookup::BrregDifi::Entry objects. | ||
| 143 | |||
| 144 | =head1 METHODS | ||
| 145 | |||
| 146 | =head2 module methods | ||
| 147 | |||
| 148 | The methods provided by this module. | ||
| 149 | |||
| 150 | =head3 error() | ||
| 151 | |||
| 152 | Set if an error has occured. | ||
| 153 | On errors, the lookup terminates. | ||
| 154 | |||
| 155 | =head3 warning() | ||
| 156 | |||
| 157 | Set if a warning has occured. | ||
| 158 | On warnings, the lookup continues. | ||
| 159 | |||
| 160 | =head3 status() | ||
| 161 | |||
| 162 | Further description of an error/warning situation. | ||
| 163 | |||
| 164 | If the returned JSON structure contains an unsupported data element, | ||
| 165 | the lookup will be performed, but a warning may be returned and | ||
| 166 | and with a status saying: | ||
| 167 | |||
| 168 | 'Warning: JSON data key entry not expected: xxxx' | ||
| 169 | |||
| 170 | where 'xxxx' is the unexpected key. If this happens, the module | ||
| 171 | should be updated with a new method to support the element 'xxx'. | ||
| 172 | |||
| 173 | =head3 map_json_entries() | ||
| 174 | |||
| 175 | Map the JSON data structure from Brreg to NOLookup::BrregDifi::Entry | ||
| 176 | data objects. | ||
| 177 | |||
| 178 | Returns a ref. to an array of NOLookup::BrregDifi::Entry data objects. | ||
| 179 | |||
| 180 | =head3 cur_page() | ||
| 181 | |||
| 182 | The number of the current page. | ||
| 183 | |||
| 184 | =head3 next_page() | ||
| 185 | |||
| 186 | The number of the next page, if any. | ||
| 187 | |||
| 188 | =head3 prev_page() | ||
| 189 | |||
| 190 | The number of the previous page, if not on first page. | ||
| 191 | |||
| 192 | =head3 total_page_count() | ||
| 193 | |||
| 194 | The total number of pages matching the search. | ||
| 195 | |||
| 196 | =head3 result_count() | ||
| 197 | |||
| 198 | The number of data entries in this page. | ||
| 199 | |||
| 200 | =head3 total_result_count() | ||
| 201 | |||
| 202 | The number of total data entries in all pages matching the search. | ||
| 203 | |||
| 204 | =head2 Accessor methods | ||
| 205 | |||
| 206 | Data elements are available through acessors in the | ||
| 207 | NOLookup::BrregDifi::Entry object. This is the possible JSON data | ||
| 208 | methods, which are the accessor methods that can be used to find the | ||
| 209 | returned data elements. | ||
| 210 | |||
| 211 | The accessor methods are listed in the @json_methods array | ||
| 212 | |||
| 213 | =head1 SUPPORT | ||
| 214 | |||
| 215 | For now, support questions should be sent to: | ||
| 216 | |||
| 217 | E<lt>(nospam)info(at)norid.noE<gt> | ||
| 218 | |||
| 219 | Please also see the SUPPORT file in the distribution. | ||
| 220 | |||
| 221 | =head1 SEE ALSO | ||
| 222 | |||
| 223 | C<NOLookup::BrregDifi::DataLookup> | ||
| 224 | |||
| 225 | =head1 AUTHOR | ||
| 226 | |||
| 227 | Trond Haugen, E<lt>(nospam)info(at)norid.noE<gt> | ||
| 228 | |||
| 229 | =head1 COPYRIGHT | ||
| 230 | |||
| 231 | Copyright (c) 2017 Trond Haugen <(nospam)info(at)norid.no>. | ||
| 232 | All rights reserved. | ||
| 233 | |||
| 234 | This program is free software; you can redistribute it and/or modify | ||
| 235 | it under the terms of the GNU General Public License as published by | ||
| 236 | the Free Software Foundation; either version 2 of the License, or | ||
| 237 | (at your option) any later version. | ||
| 238 | |||
| 239 | =head1 LICENSE | ||
| 240 | |||
| 241 | This library is free software. You can redistribute it and/or modify | ||
| 242 | it under the same terms as Perl itself. | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | 1; |