blib/lib/Net/DynDNS/GoDaddy.pm

filter: Statementstmt 100% 93 / 93 Branchbran 100% 18 / 18 Conditioncond 93.3% 14 / 15 MC/DCmcdc 88.8% 8 / 9 Subroutinesub 94.4% 17 / 18 Podpod 100% 4 / 4 totaltotal 98.0% 154 / 157 CC 15 SCAR 9.9
CC
15
Cov
99%
host_ip_set
5.0
api_key_get
4.0
api_key_set
4.0
CRAP 2.7
↑ Summary
1 package Net::DynDNS::GoDaddy;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 our $VERSION = '0.04';
14  
15 5 our @EXPORT = qw(host_ip_get host_ip_set);
16 5 our @EXPORT_OK = qw(api_key_get api_key_set);
17 5 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
18  
19 5 my $home_dir;
20  
21 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 $home_dir = File::HomeDir->my_home;
23 }
24  
25 5 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 URL => 'https://api.godaddy.com',
27 API_KEY_FILE => "$home_dir/godaddy_api.json",
28 5 };
29  
30 5 my $client = HTTP::Tiny->new;
31 5 my ($key, $secret);
32  
33 6 ▶sub api_key_get {
Subroutine api_key_get: called CC=4 SCAR=13.9
Pod documented
34 6 ▶ return($key, $secret) if $key && $secret;
Branch: 1 branch
BranchTrueFalseTotal
if $key and $secret 1 5 6
Condition66%2/3
$key and $secret
!ll&&!rl&&r
501
MC/DC50%1/2
$key and $secret
$key $secret
Truth table66%2/3
A and B
A= $keyB= $secret
ABresult
0X0
100
111
35  
36 {
37 5 local $/;
38 5 ▶ open my $fh, '<', API_KEY_FILE
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "/home/cpancover/godaddy_api.json" or croak(qq[GoDaddy API key/secret file ${\"/home/cpancover/godaddy_api.json";} doesn't exist]) 4 1 5
39 1 or croak "GoDaddy API key/secret file ${\API_KEY_FILE} doesn't exist";
40  
41 4 my $data = decode_json(<$fh>);
42  
43 4 $key = $data->{api_key};
44 4 $secret = $data->{api_secret};
45 }
46  
47 4 return($key, $secret);
48 }
49 6 ▶sub api_key_set {
Subroutine api_key_set: called CC=4 SCAR=13.9
Pod documented
50 6 my ($key, $secret) = @_;
51  
52 6 ▶ if (! $key || ! $secret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key and $secret) 2 4 6
Condition100%3/3
$key and $secret
!ll&&!rl&&r
114
MC/DC100%2/2
$key and $secret
$key $secret
Truth table100%3/3
A and B
A= $keyB= $secret
ABresult
0X0
100
111
53 2 croak "api_key_set() requires an API key and an API secret sent in";
54 }
55  
56 4 my $data = {
57 api_key => $key,
58 api_secret => $secret,
59 };
60  
61 4 ▶ open my $fh, '>', API_KEY_FILE
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", "/home/cpancover/godaddy_api.json" or croak(qq[Can't open ${\"/home/cpancover/godaddy_api.json";} for writing]) 3 1 4
62 1 or croak "Can't open ${\API_KEY_FILE} for writing";
63  
64 3 print $fh JSON->new->pretty->encode($data);
65  
66 3 return 1;
67 }
68 4 ▶sub host_ip_get {
Subroutine host_ip_get: called CC=3 SCAR=11.0
Pod documented
69 4 my ($host, $domain) = @_;
70  
71 4 ▶ if (! defined $host || ! defined $domain) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $host and defined $domain) 2 2 4
Condition100%3/3
defined $host and defined $domain
!ll&&!rl&&r
112
MC/DC100%2/2
defined $host and defined $domain
defined $host defined $domain
Truth table100%3/3
A and B
A= defined $hostB= defined $domain
ABresult
0X0
100
111
72 2 croak "host_ip_get() requires a hostname and domain name sent in";
73 }
74  
75 2 my $ip = _get($host, $domain);
76  
77 2 return $ip;
78 }
79 6 ▶sub host_ip_set {
Subroutine host_ip_set: called CC=5 SCAR=16.1
Pod documented
80 6 my ($host, $domain, $ip) = @_;
81  
82 6 ▶ if (! defined $host || ! defined $domain || ! defined $ip) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $host and defined $domain and defined $ip) 3 3 6
Condition100%6/6
defined $host and defined $domain and defined $ip
!ll&&!rl&&r
213
defined $host and defined $domain
!ll&&!rl&&r
114
MC/DC100%3/3
defined $host and defined $domain and defined $ip
defined $host defined $domain defined $ip
Truth table100%4/4
A and B and C
A= defined $hostB= defined $domainC= defined $ip
ABCresult
0XX0
10X0
1100
1111
83 3 croak "host_ip_set() requires a hostname, domain and IP sent in";
84 }
85  
86 3 ▶ if ($ip !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) 1 2 3
87 1 croak "host_ip_get() received an invalid IP: $ip";
88 }
89  
90 2 my $response = _set($host, $domain, $ip);
91  
92 2 return $response;
93 }
94  
95 1 ▶sub _api_key_file {
Subroutine _api_key_file: called CC=1 SCAR=0.0
96 # Returns the path and filename of the API key file (used for testing)
97 1 return API_KEY_FILE;
98 }
99 2 ▶sub _get {
Subroutine _get: called CC=2 SCAR=6.9
100 # Fetch the current IP of the host/domain pair
101  
102 2 my ($host, $domain) = @_;
103  
104 2 my $route = "/v1/domains/$domain/records/A/$host";
105  
106 2 my $uri = URL . $route;
107  
108 2 my ($api_key, $api_secret) = api_key_get();
109  
110 2 my $api_auth = "$api_key:$api_secret";
111  
112 2 my $headers = {
113 'Authorization' => "sso-key $api_auth"
114 };
115  
116 2 my $response = $client->request('GET', $uri, {headers => $headers});
117  
118 2 my $status = $response->{status};
119  
120 2 ▶ if ($status != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status != 200) 1 1 2
121 1 warn "Failed to connect to $uri to get your address: $response->{content}";
122 1 return '';
123 }
124  
125 1 my $ip = decode_json($response->{content})->[0]{data};
126  
127 1 return $ip;
128 }
129 2 ▶sub _set {
Subroutine _set: called CC=2 SCAR=6.9
130 # Set the host to a new IP
131  
132 2 my ($host, $domain, $ip) = @_;
133  
134 2 my $route = "/v1/domains/$domain/records/A/$host";
135  
136 2 my $uri = URL . $route;
137  
138 2 my ($api_key, $api_secret) = api_key_get();
139  
140 2 my $api_auth = "$api_key:$api_secret";
141  
142 2 my $headers = {
143 'Authorization' => "sso-key $api_auth",
144 'Content-Type' => 'application/json',
145 };
146  
147 2 my $content = [{ data => $ip }];
148 2 my $content_json = encode_json($content);
149  
150 2 my $response = $client->request(
151 'PUT',
152 $uri,
153 {
154 headers => $headers,
155 content => $content_json
156 }
157 );
158  
159 2 my $status = $response->{status};
160  
161 2 ▶ if ($status != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status != 200) 1 1 2
162 1 warn "Failed to connect to $uri to get your address: $response->{content}";
163 1 return 0;
164 }
165  
166 1 return $response->{success};
167 }
168 0 ▶sub __placeholder {}
Subroutine __placeholder: not called CC=1 SCAR=0.0
169  
170 5 1;
171 __END__
↑ Summary