| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::OverheidIO; | 
| 2 | 4 |  |  | 4 |  | 88090 | use Moose; | 
|  | 4 |  |  |  |  | 375700 |  | 
|  | 4 |  |  |  |  | 26 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | # ABSTRACT: A (semi) abstract class that implements logic to talk to Overheid.IO | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = '1.0'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 4 |  |  | 4 |  | 29801 | use LWP::UserAgent; | 
|  | 4 |  |  |  |  | 98566 |  | 
|  | 4 |  |  |  |  | 137 |  | 
| 9 | 4 |  |  | 4 |  | 35 | use URI; | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 91 |  | 
| 10 | 4 |  |  | 4 |  | 22 | use Carp; | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 238 |  | 
| 11 | 4 |  |  | 4 |  | 1705 | use JSON; | 
|  | 4 |  |  |  |  | 32026 |  | 
|  | 4 |  |  |  |  | 23 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | has ua => ( | 
| 14 |  |  |  |  |  |  | is       => 'ro', | 
| 15 |  |  |  |  |  |  | isa      => 'LWP::UserAgent', | 
| 16 |  |  |  |  |  |  | builder  => '_build_useragent', | 
| 17 |  |  |  |  |  |  | lazy     => 1, | 
| 18 |  |  |  |  |  |  | ); | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | has base_uri => ( | 
| 21 |  |  |  |  |  |  | is      => 'ro', | 
| 22 |  |  |  |  |  |  | isa     => 'URI', | 
| 23 |  |  |  |  |  |  | builder => '_build_base_uri', | 
| 24 |  |  |  |  |  |  | lazy => 1, | 
| 25 |  |  |  |  |  |  | ); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | has max_query_size => ( | 
| 28 |  |  |  |  |  |  | is      => 'ro', | 
| 29 |  |  |  |  |  |  | isa     => 'Int', | 
| 30 |  |  |  |  |  |  | default => 30, | 
| 31 |  |  |  |  |  |  | ); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | has key => ( | 
| 35 |  |  |  |  |  |  | is       => 'ro', | 
| 36 |  |  |  |  |  |  | isa      => 'Str', | 
| 37 |  |  |  |  |  |  | required => 1, | 
| 38 |  |  |  |  |  |  | ); | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | has type => ( | 
| 42 |  |  |  |  |  |  | is      => 'ro', | 
| 43 |  |  |  |  |  |  | isa     => 'Str', | 
| 44 |  |  |  |  |  |  | lazy    => 1, | 
| 45 |  |  |  |  |  |  | builder => '_build_type', | 
| 46 |  |  |  |  |  |  | ); | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | has fieldnames => ( | 
| 50 |  |  |  |  |  |  | is      => 'rw', | 
| 51 |  |  |  |  |  |  | isa     => 'ArrayRef', | 
| 52 |  |  |  |  |  |  | lazy    => 1, | 
| 53 |  |  |  |  |  |  | builder => '_build_fieldnames', | 
| 54 |  |  |  |  |  |  | ); | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | has queryfields => ( | 
| 57 |  |  |  |  |  |  | is      => 'rw', | 
| 58 |  |  |  |  |  |  | isa     => 'ArrayRef', | 
| 59 |  |  |  |  |  |  | lazy    => 1, | 
| 60 |  |  |  |  |  |  | builder => '_build_queryfields', | 
| 61 |  |  |  |  |  |  | ); | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub search { | 
| 64 | 6 |  |  | 6 | 1 | 30986 | my $self        = shift; | 
| 65 | 6 |  |  |  |  | 14 | my $search_term = shift; | 
| 66 | 6 |  |  |  |  | 11 | my $opts        = {@_}; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 6 |  |  |  |  | 11 | my $filter = $opts->{filter}; | 
| 69 | 6 | 100 |  |  |  | 183 | $filter->{actief} = 'true' if $self->type eq 'kvk'; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 2 |  |  |  |  | 15 | my %query = map { sprintf('filters[%s]', $_), => $filter->{$_} } | 
| 72 | 6 |  |  |  |  | 11 | grep { defined $filter->{$_} } keys %{$filter}; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 6 |  |  |  |  | 20 |  | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 6 |  |  |  |  | 161 | $query{size} = $self->max_query_size; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 6 |  |  |  |  | 143 | my $uri = $self->base_uri->clone; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 6 |  |  |  |  | 198 | $uri->query_form( | 
| 79 |  |  |  |  |  |  | %query, | 
| 80 |  |  |  |  |  |  | 'fields[]'      => $self->fieldnames, | 
| 81 |  |  |  |  |  |  | 'queryfields[]' => $self->queryfields, | 
| 82 |  |  |  |  |  |  | query           => $search_term, | 
| 83 |  |  |  |  |  |  | ); | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 6 |  |  |  |  | 947 | return $self->_call_overheid_io($uri); | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | sub _build_base_uri { | 
| 89 | 3 |  |  | 3 |  | 7 | my $self = shift; | 
| 90 | 3 |  |  |  |  | 70 | return URI->new_abs( "/api/". $self->type, 'https://overheid.io'); | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | sub _build_useragent { | 
| 94 | 3 |  |  | 3 |  | 8 | my $self = shift; | 
| 95 | 3 |  |  |  |  | 36 | my $ua = LWP::UserAgent->new( | 
| 96 |  |  |  |  |  |  | ssl_opts => { | 
| 97 |  |  |  |  |  |  | SSL_ca_path     => '/etc/ssl/certs', | 
| 98 |  |  |  |  |  |  | verify_hostname => 1, | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  | ); | 
| 101 | 3 |  |  |  |  | 6591 | $ua->default_header('ovio-api-key', $self->key); | 
| 102 | 3 |  |  |  |  | 261 | return $ua; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | sub _call_overheid_io { | 
| 106 | 4 |  |  | 4 |  | 11 | my ($self, $uri) = @_; | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 4 |  |  |  |  | 114 | my $res = $self->ua->get($uri->as_string); | 
| 109 | 4 |  |  |  |  | 6757 | my $decoded = $res->decoded_content; | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 4 | 100 |  |  |  | 580 | if (!$res->is_success) { | 
| 112 | 1 |  |  |  |  | 9 | die sprintf("%s - %s", $res->status_line, $res->decoded_content), $/; | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 3 |  |  |  |  | 142 | my $json = JSON->new->decode($decoded); | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 3 |  |  |  |  | 31 | return $json; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | __END__ | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | =pod | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | =encoding UTF-8 | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | =head1 NAME | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | WebService::OverheidIO - A (semi) abstract class that implements logic to talk to Overheid.IO | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | =head1 VERSION | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | version 1.0 | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | package WebService::OverheidIO::Foo; | 
| 140 |  |  |  |  |  |  | use Moose; | 
| 141 |  |  |  |  |  |  | extends 'WebService::OverheidIO'; | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | # You must implement the following builders: | 
| 144 |  |  |  |  |  |  | # _build_type | 
| 145 |  |  |  |  |  |  | # _build_fieldnames | 
| 146 |  |  |  |  |  |  | # _build_queryfields | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =head2 ua | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | =head2 base_uri | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | The base URI of the Overheid.IO, lazyly loaded. | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | =head2 max_query_size | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | The max query size, defaults to 30. | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | =head2 key | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | The required Overheid.IO API key. | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | =head2 type | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | The type of Overheid.IO api | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | =head2 fieldnames | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | The names of the fields which the Overheid.IO will respond with | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | =head2 queryfields | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | The names of the fields which will be used to query on | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | =head2 search | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | Search OverheidIO by a search term, you can apply additional filters for zipcodes and such | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | $overheidio->search( | 
| 181 |  |  |  |  |  |  | "Mintlab", | 
| 182 |  |  |  |  |  |  | filter => { | 
| 183 |  |  |  |  |  |  | postcode => '1051JL', | 
| 184 |  |  |  |  |  |  | } | 
| 185 |  |  |  |  |  |  | ); | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | =head1 AUTHOR | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  | Wesley Schwengle <wesley@mintlab.nl> | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | This software is Copyright (c) 2017 by Mintlab BV. | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | This is free software, licensed under: | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | The European Union Public License (EUPL) v1.1 | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | =cut |