File Coverage

blib/lib/WebService/Lobid/Organisation.pm
Criterion Covered Total %
statement 64 76 84.2
branch 12 22 54.5
condition 1 3 33.3
subroutine 9 10 90.0
pod 0 1 0.0
total 86 112 76.7


line stmt bran cond sub pod time code
1             package WebService::Lobid::Organisation;
2              
3             our $VERSION = 0.005;
4              
5             # ABSTRACT: interface to the lobid-Organisations API
6              
7             =for html
8              
9             =head1 NAME
10              
11             WebService::Lobid::Organisation - interface to the lobid-Organisations API
12              
13             =head1 SYNOPSIS
14              
15             my $Library = WebService::Lobid::Organisation->new(isil=> 'DE-380');
16              
17             if ($Library->status eq 'OK'){
18             printf("This Library is called '%s', its homepage is at '%s'
19             and it can be found at %f/%f",
20             $Library->name, $Library->url, $Library->lat, $Library->long
21             );
22              
23             if ($Library->has_wikipedia){
24             printf("%s has its own wikipedia entry: %s",
25             $Library->name, $Library->wikipedia);
26             }
27              
28             if ($Library->has_multiple_emails){
29             print $Library->email->[0];
30             }else{
31             print $Library->email;
32             }
33             }else{
34             print $Library->error;
35             }
36              
37             =head1 METHODS
38              
39             =over 4
40              
41             =item new(isil=>$isil)
42              
43             tries to fetch data for the organisation identified by the ISIL C<$isil>.
44             If an entry is found then the attribute C is set to I
45              
46             If an error occurs, the attribute C is set to I with the error
47             message in C<$self->error>. Otherwise C is I.
48              
49             =back
50              
51             =head1 ATTRIBUTES
52              
53             currently the following attributes are supported
54              
55             =over 4
56              
57             =item * B
58              
59             inherited from L, default is I
60              
61             =item * B
62              
63             inherited from L, I if C reachable,
64             otherwise C
65              
66             =item * B
67              
68             inherited from L, default ist I<3> seconds
69              
70              
71             =item * B
72              
73             inherited from L, I if L can use SSL,
74             otherwise C
75              
76             =item * B (true|false)
77              
78             indicates if an entry is found
79              
80             =item * B
81              
82             the L of the organisation. Has the predicate function I.
83              
84             =item * B
85              
86             Has the predicate function I.
87              
88             =item * B
89              
90             Has the predicate function I
91              
92             =item * B
93              
94             Service URL, normally the OPAC, Has the predicate function I
95              
96             =item * B
97              
98             Has the predicate function I
99              
100             =item * B
101              
102             The city or town where institution resides. Has the predicate function I
103              
104             =item * B
105              
106             Has the predicate function I
107              
108             =item * B
109              
110             Has the predicate function I
111              
112             =item * B
113              
114             Has the predicate function I. The email address for the instition including a I prefix. A scalar if there ist just one email address, an array reference if there are more than one adresses (in this case C is set to I<1>
115              
116             =item * B
117              
118             set to I<1> if there is more than one address in C
119              
120             =item * B
121              
122             The longitude of the place. Has the predicate function I.
123              
124             =item * B
125              
126             The latitude of the place. Has the predicate function I
127              
128             =item * B
129              
130             I or I
131              
132             =item * B
133              
134             error message, if C<$self->status> is I
135              
136             =back
137              
138             =head1 DEPENDENCIES
139              
140             L, L, L, L, L
141              
142             =head1 LOGGING
143              
144             This module uses the L Framework
145              
146             =head1 AUTHOR
147              
148             Peter Mayr
149              
150             =head1 REPOSITORY
151              
152             The source code is also on GitHub . Pull requests and bug reports welcome!
153              
154             =head1 VERSION
155              
156             0.005
157              
158             =head1 LICENCE AND COPYRIGHT
159              
160             GNU GPL V3
161              
162             Peter Mayr 2016
163              
164             =cut
165              
166 2     2   141956 use strict;
  2         13  
  2         58  
167 2     2   10 use warnings;
  2         5  
  2         47  
168              
169 2     2   1523 use HTTP::Tiny;
  2         103255  
  2         84  
170 2     2   1387 use JSON;
  2         25180  
  2         11  
171 2     2   1219 use Log::Any;
  2         16446  
  2         10  
172 2     2   1230 use Moo;
  2         23265  
  2         13  
173 2     2   4065 use Try::Tiny;
  2         2651  
  2         1765  
174              
175             extends 'WebService::Lobid';
176              
177             has isil => ( is => 'rw', predicate => 1, required => 1 );
178             has name => ( is => 'rw', predicate => 1 );
179             has url => ( is => 'rw', predicate => 1 );
180             has provides => ( is => 'rw', predicate => 1 );
181             has addressCountry => ( is => 'rw', predicate => 1 );
182             has addressLocality => ( is => 'rw', predicate => 1 );
183             has postalCode => ( is => 'rw', predicate => 1 );
184             has streetAddress => ( is => 'rw', predicate => 1 );
185             has email => ( is => 'rw', predicate => 1 );
186             has has_multiple_emails => ( is => 'rw', default => 0 );
187             has lon => ( is => 'rw', predicate => 1 );
188             has lat => ( is => 'rw', predicate => 1 );
189             has found => ( is => 'rw', default => 'false' );
190              
191             has log => (
192             is => 'ro',
193             default => sub { Log::Any->get_logger },
194             );
195              
196             sub BUILD {
197 2     2 0 13 my $self = shift;
198              
199 2         8 my $query_string = undef;
200 2         7 my $response = undef;
201 2         6 my $json_result = undef;
202 2         10 my $result_ref = undef;
203 2         8 my $no_of_results = undef;
204 2         10 my %data = ();
205 2         8 my $email = undef;
206 2         39 my $uri = sprintf( "%s%s/%s", $self->api_url, "organisation", $self->isil );
207              
208 2         19 $query_string = sprintf( "%s%s/%s.json",
209             $self->api_url, "organisations", $self->isil );
210              
211 2         31 $self->log->infof( "URL: %s", $query_string );
212 2         40 $response = HTTP::Tiny->new(timeout => $self->api_timeout)->get($query_string);
213              
214 2 100       479317 if ( $response->{success} ) {
215 1         604 $json_result = $response->{content};
216             }
217             else {
218              
219 1 50       632 if ( $response->{status} eq '404' ) {
220 1         25 $self->log->warnf( "ISIL %s not found!", $self->isil );
221 1         14 $self->status('OK');
222 1         9 $self->found("false");
223 1         69 return;
224             }
225             else {
226             $self->log->errorf( "Problem accessing the API: %s!",
227 0         0 $response->{status} );
228 0         0 $self->status("Error");
229             $self->error(sprintf("Problem accessing the API: %s!",
230 0         0 $response->{status}));
231 0         0 return;
232             }
233             }
234              
235 1         27 $self->log->debugf( "Got JSON Result: %s", $json_result );
236              
237             try {
238 1     1   225 $result_ref = decode_json($json_result);
239             }
240             catch {
241 0     0   0 $self->log->errorf( "Decoding of response '%s' failed: %s",
242             $json_result, $_ );
243 0         0 $self->status("Error");
244 0         0 $self->error(sprintf("Decoding of response '%s' failed: %s",
245             $json_result, $_)
246             );
247 0         0 return;
248 1         31 };
249              
250 1 50       63 if ( $result_ref->{isil} eq $self->isil ) {
251 1         16 $self->log->infof( "Got result for ISIL %s", $self->isil );
252 1         12 $self->found("true");
253 1         15 $self->status("OK");
254             }
255              
256 1 50       8 if ( $result_ref->{email} ) {
257 1         6 $email = $result_ref->{email};
258 1 50       8 if ( ref($email) eq 'ARRAY' ) { # multiple E-Mail Adresses
259 0         0 $self->has_multiple_emails(1);
260 0         0 for ( my $i = 0; $i < scalar( @{$email} ); $i++ ) {
  0         0  
261 0         0 $email->[$i] =~ s/^mailto://;
262             }
263             }
264             else {
265              
266 1         12 $email =~ s/^mailto://;
267              
268             }
269 1         8 $self->email($email);
270             }
271              
272 1 50       12 $self->name( $result_ref->{name} ) if ( $result_ref->{name} );
273 1 50       11 $self->url( $result_ref->{url} ) if ( $result_ref->{url} );
274 1 50       11 $self->provides( $result_ref->{provides} ) if ( $result_ref->{provides} );
275              
276 1 50 33     19 if ( ( defined( $result_ref->{location} ) )
277             && ( ref( $result_ref->{location} ) eq 'ARRAY' ) )
278             {
279 1 50       10 if ( $result_ref->{location}->[0]->{geo} ) {
280 1         14 $self->lat( $result_ref->{location}->[0]->{geo}->{lat} );
281 1         6 $self->lon( $result_ref->{location}->[0]->{geo}->{lon} );
282             }
283 1 50       7 if ( $result_ref->{location}->[0]->{address} ) {
284             $self->addressCountry(
285 1         10 $result_ref->{location}->[0]->{address}->{addressCountry} );
286             $self->addressLocality(
287 1         8 $result_ref->{location}->[0]->{address}->{addressLocality} );
288             $self->postalCode(
289 1         9 $result_ref->{location}->[0]->{address}->{postalCode} );
290             $self->streetAddress(
291 1         40 $result_ref->{location}->[0]->{address}->{streetAddress} );
292              
293             }
294             }
295              
296             } ## end sub BUILD
297              
298             1;