1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
package Cepis::Health;
use strict;
use warnings;
use Scalar::Util ();
our $VERSION = '0.2.0';
sub from_json {
my ( $class, $data ) = @_;
my %raw = %{ $data || {} };
return bless {
status => _string( $raw{status} ),
dne => _string( $raw{dne} ),
ceps => _integer( $raw{ceps} ),
geo => _integer( $raw{geo} ),
base_gerada_em => _string( $raw{base_gerada_em} ),
raw => \%raw,
}, $class;
}
sub status { return $_[0]->{status} }
sub dne { return $_[0]->{dne} }
sub ceps { return $_[0]->{ceps} }
sub geo { return $_[0]->{geo} }
sub base_gerada_em { return $_[0]->{base_gerada_em} }
sub raw { return $_[0]->{raw} }
sub ok { return $_[0]->{status} eq 'ok' ? 1 : 0 }
sub to_hash {
my ($self) = @_;
return {
status => $self->{status},
dne => $self->{dne},
ceps => $self->{ceps},
geo => $self->{geo},
base_gerada_em => $self->{base_gerada_em},
};
}
sub _string {
my ($value) = @_;
return '' unless defined $value;
return '' if ref $value;
return "$value";
}
sub _integer {
my ($value) = @_;
return 0 unless defined $value;
return 0 unless Scalar::Util::looks_like_number($value);
return int( 0 + $value );
}
1;
__END__
=encoding UTF-8
=head1 NAME
Cepis::Health - the answer of C<GET /health>
=head1 SYNOPSIS
my $h = Cepis::health();
$h->ok; # 1
$h->dne; # "26071" identifier of the fortnightly database
$h->ceps; # 1596168
$h->base_gerada_em; # "2026-07-24T03:48:28Z"
=head1 DESCRIPTION
Good for two things: checking that the API answers, and knowing B<which version
of the database> is live. The DNE database changes every two weeks; L</dne>
identifies it.
An API reporting itself degraded does not raise — that arrives in L</status>.
=head1 CONSTRUCTOR
=head2 from_json
my $health = Cepis::Health->from_json( $hashref );
Builds from the decoded JSON object the API returned.
=head1 ACCESSORS
=head2 status
C<"ok"> when the API considers itself healthy, something else otherwise.
=head2 ok
True when L</status> is exactly C<"ok">.
=head2 dne
Identifier of the DNE database currently loaded. It changes every fortnight.
=head2 ceps
How many CEPs the database holds.
=head2 geo
How many of those CEPs carry a coordinate. Note that unlike
L<Cepis::Address/geo> this is a count, not an object — that is what the API
sends.
=head2 base_gerada_em
When the database was generated, as an ISO 8601 timestamp string.
=head2 raw
The decoded response exactly as it arrived, as a hashref.
=head2 to_hash
The five named fields as a plain hashref.
=head1 SEE ALSO
L<Cepis>, L<Cepis::Client>
=head1 LICENCE
ISC. Copyright (c) 2026 Murilo Ijanc E<lt>[email protected]<gt>.
=cut
|