File Coverage

blib/lib/Pinto/Locator/Stratopan.pm
Criterion Covered Total %
statement 43 43 100.0
branch 9 10 90.0
condition 1 2 50.0
subroutine 11 11 100.0
pod 0 2 0.0
total 64 68 94.1


line stmt bran cond sub pod time code
1             # ABSTRACT: Locate targets using Stratopan services
2              
3             package Pinto::Locator::Stratopan;
4              
5 51     51   314 use Moose;
  51         119  
  51         359  
6 51     51   312470 use MooseX::StrictConstructor;
  51         122  
  51         439  
7 51     51   155693 use MooseX::MarkAsMethods ( autoclean => 1 );
  51         124  
  51         436  
8              
9 51     51   175607 use URI;
  51         132  
  51         1578  
10 51     51   280 use JSON qw(decode_json);
  51         109  
  51         402  
11 51     51   21757 use HTTP::Request::Common qw(GET);
  51         70934  
  51         3619  
12              
13 51     51   363 use Pinto::Util qw(whine);
  51         117  
  51         2407  
14 51     51   311 use Pinto::Constants qw(:stratopan);
  51         106  
  51         2936  
15              
16             #-----------------------------------------------------------------------------
17              
18             our $VERSION = '0.13'; # VERSION
19              
20             #-----------------------------------------------------------------------------
21              
22             extends qw(Pinto::Locator);
23              
24             #-----------------------------------------------------------------------------
25              
26             sub locate_package {
27 6     6 0 17 my ($self, %args) = @_;
28              
29 6         24 return $self->_locate_any(%args);
30             }
31              
32             #-----------------------------------------------------------------------------
33              
34             sub locate_distribution {
35 3     3 0 11 my ($self, %args) = @_;
36              
37 3         11 return $self->_locate_any(%args);
38             }
39              
40             #-----------------------------------------------------------------------------
41              
42             sub _locate_any {
43 9     9   23 my ($self, %args) = @_;
44              
45 9         34 my $uri = $PINTO_STRATOPAN_LOCATOR_URI->clone;
46 9         146 $uri->query_form(q => $args{target}->to_string);
47 9         618 my $response = $self->request(GET($uri));
48              
49 9 100       9570 if (!$response->is_success) {
50 1         13 my $status = $response->status_line;
51 1         24 whine "Stratopan is not responding: $status";
52 1         7 return;
53             }
54              
55 8         75 my $structs = eval { decode_json($response->content) };
  8         22  
56 8 100 50     140 whine "Invalid response from Stratopan: $@" and return if $@;
57              
58 7 100       43 return unless my $latest = $structs->[0];
59              
60             # Avoid autovivification here...
61             $latest->{version} = version->parse($latest->{version})
62 3 100       30 if exists $latest->{version};
63              
64             # Avoid autovivification here...
65             $latest->{uri} = URI->new($latest->{uri})
66 3 50       21 if exists $latest->{uri};
67              
68 3         248 return $latest;
69             }
70              
71             #-----------------------------------------------------------------------------
72              
73             __PACKAGE__->meta->make_immutable;
74              
75             #-----------------------------------------------------------------------------
76             1;
77              
78             __END__
79              
80             =pod
81              
82             =encoding UTF-8
83              
84             =for :stopwords Jeffrey Ryan Thalhammer
85              
86             =head1 NAME
87              
88             Pinto::Locator::Stratopan - Locate targets using Stratopan services
89              
90             =head1 VERSION
91              
92             version 0.13
93              
94             =head1 AUTHOR
95              
96             Jeffrey Ryan Thalhammer <jeff@stratopan.com>
97              
98             =head1 COPYRIGHT AND LICENSE
99              
100             This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
101              
102             This is free software; you can redistribute it and/or modify it under
103             the same terms as the Perl 5 programming language system itself.
104              
105             =cut