File Coverage

blib/lib/WebService/OpenStates.pm
Criterion Covered Total %
statement 38 70 54.2
branch 0 16 0.0
condition 0 27 0.0
subroutine 14 15 93.3
pod 1 1 100.0
total 53 129 41.0


line stmt bran cond sub pod time code
1             package WebService::OpenStates;
2              
3             our $VERSION = '1.03';
4              
5 3     3   634347 use strict;
  3         7  
  3         110  
6 3     3   14 use warnings;
  3         5  
  3         160  
7 3     3   56 use v5.14;
  3         10  
8              
9 3     3   18 use Carp 'croak';
  3         7  
  3         244  
10 3     3   2389 use HTTP::Tiny;
  3         192267  
  3         208  
11 3     3   2215 use Function::Parameters;
  3         17508  
  3         16  
12 3     3   3571 use Types::Common::String 'NonEmptyStr';
  3         622124  
  3         33  
13 3     3   6641 use Types::Common::Numeric 'NumRange';
  3         116833  
  3         58  
14 3     3   4589 use JSON::MaybeXS;
  3         41351  
  3         307  
15 3     3   2265 use URI;
  3         29666  
  3         197  
16 3     3   2832 use Moo;
  3         45798  
  3         21  
17 3     3   11694 use namespace::clean;
  3         66312  
  3         29  
18              
19             ##
20             has api_key => (
21             is => 'ro',
22             isa => NonEmptyStr,
23             required => 1,
24             default => sub { $ENV{OPENSTATES_API_KEY} },
25             );
26              
27             ##
28             has _api_url => (
29             is => 'lazy',
30             init_arg => undef,
31 1     1   4045 builder => sub { 'https://v3.openstates.org/people.geo' },
32             );
33              
34             ##
35             has _client => (
36             is => 'lazy',
37             init_arg => undef,
38             builder => sub {
39 1     1   25 state $ua = HTTP::Tiny->new;
40 1         156 return $ua;
41             },
42             );
43              
44             ##
45 0 0 0 0 1   method legislators_for_location ((NumRange[-90,90]) :$lat, (NumRange[-180,180]) :$lon) {
  0 0 0        
  0 0 0        
  0 0 0        
  0 0 0        
  0 0 0        
  0 0 0        
  0   0        
  0   0        
  0            
46 0           my $uri = URI->new( $self->_api_url );
47 0           $uri->query_form(
48             apikey => $self->api_key,
49             lat => $lat,
50             lng => $lon,
51             include => ['links', 'offices'],
52             );
53              
54 0           my $call = $self->_client->get( $uri );
55              
56             my $response = try {
57 0 0         if ( ! $call->{success} ) {
58 0           my $resp = { status => 'error', content => decode_json( $call->{content} ) };
59 0           return $resp;
60             }
61             else {
62 0           my $data = decode_json( $call->{content} );
63              
64 0           my @result;
65              
66 0           my @legislators = @{ $data->{results} };
  0            
67              
68 0           for my $legislator (sort { $a->{family_name} cmp $b->{family_name} } @legislators) {
  0            
69             my $result = {
70             name => $legislator->{name},
71             title => join(' ', $legislator->{jurisdiction}{name}, $legislator->{current_role}{title}),
72             party => $legislator->{party},
73             email => $legislator->{email},
74 0           links => [map {$_->{url}} @{$legislator->{links}}],
  0            
75 0           offices => [ map {+{name => $_->{name}, phone => $_->{voice}, address => $_->{address} }} @{$legislator->{offices}}],
  0            
  0            
76             };
77              
78 0           push(@result, $result);
79             }
80              
81 0           return { status => 'success', legislators => \@result };
82             }
83             }
84 0           catch {
85 0           return { status => 'error', content => "Caught fatal error trying to call Open States API: $_" };
86             };
87              
88 0           return $response;
89             }
90              
91             1; # return true
92              
93             __END__