line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bessarabv::Sleep; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Bessarabv::Sleep::VERSION = '1.0.0'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: get Ivan Bessarabov's sleep data |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
47535
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
10
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
11
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
91
|
|
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
1395
|
use LWP::Simple; |
|
1
|
|
|
|
|
106393
|
|
|
1
|
|
|
|
|
12
|
|
14
|
1
|
|
|
1
|
|
2878
|
use JSON; |
|
1
|
|
|
|
|
17194
|
|
|
1
|
|
|
|
|
5
|
|
15
|
1
|
|
|
1
|
|
183
|
use Time::Local; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
816
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my $true = 1; |
18
|
|
|
|
|
|
|
my $false = ''; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new { |
22
|
0
|
|
|
0
|
1
|
|
my ($class, %opts) = @_; |
23
|
|
|
|
|
|
|
|
24
|
0
|
0
|
|
|
|
|
croak "new() does not need any parameters. Stopped" if %opts; |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
my $self = {}; |
27
|
0
|
|
|
|
|
|
bless $self, $class; |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
$self->__get_data(); |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
|
return $self; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub has_sleep_data { |
36
|
0
|
|
|
0
|
1
|
|
my ($self, $date) = @_; |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
$self->__die_if_date_is_incorrect($date); |
39
|
|
|
|
|
|
|
|
40
|
0
|
0
|
0
|
|
|
|
if (exists $self->{__data}->{$date} and defined $self->{__data}->{$date}) { |
41
|
0
|
|
|
|
|
|
return $true; |
42
|
|
|
|
|
|
|
} else { |
43
|
0
|
|
|
|
|
|
return $false; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub get_sleep_hours { |
49
|
0
|
|
|
0
|
1
|
|
my ($self, $date) = @_; |
50
|
|
|
|
|
|
|
|
51
|
0
|
0
|
|
|
|
|
if ($self->has_sleep_data($date)) { |
52
|
0
|
|
|
|
|
|
return $self->{__data}->{$date}; |
53
|
|
|
|
|
|
|
} else { |
54
|
0
|
0
|
|
|
|
|
$date = '' if not defined $date; |
55
|
0
|
|
|
|
|
|
croak "There is no sleep info for the date '$date'"; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub __get_data { |
60
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
my $json = get("http://ivan.bessarabov.ru/sleep.json"); |
63
|
0
|
|
|
|
|
|
my $data = from_json($json); |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
my $day_data = $data->[0]->{day}; |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
my %date2sleep = map { $_->{label} => $_->{min_value} } @{ $day_data }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
$self->{__data} = \%date2sleep; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
return $false; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub __die_if_date_is_incorrect { |
75
|
0
|
|
|
0
|
|
|
my ($self, $date) = @_; |
76
|
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
|
$date = '' if not defined $date; |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
if ($date =~ /^(\d{4})-(\d\d)-(\d\d)$/) { |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
my $year = $1; |
82
|
0
|
|
|
|
|
|
my $month = $2; |
83
|
0
|
|
|
|
|
|
my $day = $3; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# It dies with more or less fiendly message in case of error |
86
|
0
|
|
|
|
|
|
timelocal(0,0,0, $day, $month-1, $year); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
} else { |
89
|
0
|
|
|
|
|
|
croak "Incorrect date '$date'. Stopped"; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
return $false; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
__END__ |