File Coverage

blib/lib/WWW/Hetzner/Cloud/API/Locations.pm
Criterion Covered Total %
statement 29 30 96.6
branch 4 6 66.6
condition 1 2 50.0
subroutine 9 9 100.0
pod 3 3 100.0
total 46 50 92.0


line stmt bran cond sub pod time code
1             package WWW::Hetzner::Cloud::API::Locations;
2             # ABSTRACT: Hetzner Cloud Locations API
3              
4             our $VERSION = '0.100';
5              
6 25     25   195 use Moo;
  25         55  
  25         168  
7 25     25   10436 use Carp qw(croak);
  25         67  
  25         1764  
8 25     25   13185 use WWW::Hetzner::Cloud::Location;
  25         90  
  25         1080  
9 25     25   244 use namespace::clean;
  25         61  
  25         117  
10              
11              
12             has client => (
13             is => 'ro',
14             required => 1,
15             weak_ref => 1,
16             );
17              
18             sub _wrap {
19 7     7   16 my ($self, $data) = @_;
20 7         198 return WWW::Hetzner::Cloud::Location->new(
21             client => $self->client,
22             %$data,
23             );
24             }
25              
26             sub _wrap_list {
27 2     2   6 my ($self, $list) = @_;
28 2         6 return [ map { $self->_wrap($_) } @$list ];
  6         2679  
29             }
30              
31              
32             sub list {
33 2     2 1 1494 my ($self, %params) = @_;
34              
35 2         24 my $result = $self->client->get('/locations', params => \%params);
36 2   50     16 return $self->_wrap_list($result->{locations} // []);
37             }
38              
39              
40             sub get {
41 1     1 1 39 my ($self, $id) = @_;
42 1 50       6 croak "Location ID required" unless $id;
43              
44 1         14 my $result = $self->client->get("/locations/$id");
45 1         6 return $self->_wrap($result->{location});
46             }
47              
48              
49             sub get_by_name {
50 1     1 1 38 my ($self, $name) = @_;
51 1 50       6 croak "Name required" unless $name;
52              
53 1         6 my $locations = $self->list;
54 1         40 for my $loc (@$locations) {
55 2 100       17 return $loc if $loc->name eq $name;
56             }
57 0           return;
58             }
59              
60              
61             1;
62              
63             __END__