blib/lib/Net/DynDNS/GoDaddy.pm

filter: Statementstmt 77.4% 72 / 93 Branchbran 72.2% 13 / 18 Conditioncond 86.6% 13 / 15 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 94.4% 17 / 18 Podpod 100% 4 / 4 totaltotal 80.2% 126 / 157 CC 15 SCAR 10.7
CC
15
Cov
78%
host_ip_set
5.0
api_key_get
4.1
api_key_set
4.0
CRAP 2.9
↑ 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.03';
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 BEGIN {
22 5 ▶ $home_dir = File::HomeDir->my_home;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 }
24  
25 use constant {
26 5 URL => 'https://api.godaddy.com',
27 API_KEY_FILE => "$home_dir/godaddy_api.json",
28 5 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 5 my $client = HTTP::Tiny->new;
31 5 my ($key, $secret);
32  
33 sub api_key_get {
34 5 ▶ 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 5 5
Condition33%1/3
$key and $secret
!ll&&!rl&&r
500
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 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 4 or croak "GoDaddy API key/secret file ${\API_KEY_FILE} doesn't exist";
40  
41 1 my $data = decode_json(<$fh>);
42  
43 1 $key = $data->{api_key};
44 1 $secret = $data->{api_secret};
45 }
46  
47 1 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 4 ▶ my ($host, $domain) = @_;
Subroutine host_ip_get: called CC=3 SCAR=11.0
Pod documented
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 0 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 0 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 2 ▶ my ($host, $domain) = @_;
Subroutine _get: called CC=2 SCAR=12.7
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 0 my $api_auth = "$api_key:$api_secret";
111  
112 0 my $headers = {
113 'Authorization' => "sso-key $api_auth"
114 };
115  
116 0 my $response = $client->request('GET', $uri, {headers => $headers});
117  
118 0 my $status = $response->{status};
119  
120 0 ▶ if ($status != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status != 200) 0 0 0
121 0 warn "Failed to connect to $uri to get your address: $response->{content}";
122 0 return '';
123 }
124  
125 0 my $ip = decode_json($response->{content})->[0]{data};
126  
127 0 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=13.0
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 0 my $api_auth = "$api_key:$api_secret";
141  
142 0 my $headers = {
143 'Authorization' => "sso-key $api_auth",
144 'Content-Type' => 'application/json',
145 };
146  
147 0 my $content = [{ data => $ip }];
148 0 my $content_json = encode_json($content);
149  
150 0 my $response = $client->request(
151 'PUT',
152 $uri,
153 {
154 headers => $headers,
155 content => $content_json
156 }
157 );
158  
159 0 my $status = $response->{status};
160  
161 0 ▶ if ($status != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status != 200) 0 0 0
162 0 warn "Failed to connect to $uri to get your address: $response->{content}";
163 0 return 0;
164 }
165  
166 0 return $response->{success};
167 }
168 ▶sub __placeholder {}
Subroutine __placeholder: not called CC=1 SCAR=0.0
169  
170 5 1;
171 __END__
↑ Summary