blib/lib/Net/DynDNS/GoDaddy.pm

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