File Coverage

blib/lib/WebService/Lobid/Organisation.pm
Criterion Covered Total %
statement 62 71 87.3
branch 12 22 54.5
condition 1 3 33.3
subroutine 9 10 90.0
pod 0 1 0.0
total 84 107 78.5


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