File Coverage

blib/lib/Bessarabv/Weight.pm
Criterion Covered Total %
statement 18 52 34.6
branch 0 12 0.0
condition 0 3 0.0
subroutine 6 11 54.5
pod 3 3 100.0
total 27 81 33.3


line stmt bran cond sub pod time code
1             package Bessarabv::Weight;
2             {
3             $Bessarabv::Weight::VERSION = '1.0.0';
4             }
5              
6             # ABSTRACT: get Ivan Bessarabov's weight data
7              
8              
9 1     1   58915 use strict;
  1         2  
  1         34  
10 1     1   5 use warnings;
  1         1  
  1         26  
11 1     1   4 use Carp;
  1         6  
  1         74  
12              
13 1     1   732 use LWP::Simple;
  1         90039  
  1         8  
14 1     1   1378 use JSON;
  1         15211  
  1         6  
15 1     1   174 use Time::Local;
  1         3  
  1         570  
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_weight {
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_weight {
49 0     0 1   my ($self, $date) = @_;
50              
51 0 0         if ($self->has_weight($date)) {
52 0           return $self->{__data}->{$date};
53             } else {
54 0 0         $date = '' if not defined $date;
55 0           croak "There is no weight 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/weight.json");
63 0           my $data = from_json($json);
64              
65 0           my $day_data = $data->[0]->{day};
66              
67 0           my %date2weight = map { $_->{label} => $_->{min_value} } @{ $day_data };
  0            
  0            
68              
69 0           $self->{__data} = \%date2weight;
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__