File Coverage

blib/lib/Calendar/Japanese/Acme/Syukujitsu.pm
Criterion Covered Total %
statement 51 60 85.0
branch 7 18 38.8
condition 0 6 0.0
subroutine 13 14 92.8
pod 3 3 100.0
total 74 101 73.2


line stmt bran cond sub pod time code
1             package Calendar::Japanese::Acme::Syukujitsu;
2              
3 1     1   65747 use 5.006;
  1         5  
4 1     1   8 use strict;
  1         3  
  1         29  
5 1     1   6 use warnings;
  1         7  
  1         46  
6 1     1   485 use utf8;
  1         16  
  1         6  
7              
8 1     1   43 use Carp qw(croak);
  1         3  
  1         68  
9 1     1   351 use Class::Accessor::Lite (ro => [qw(max_year min_year syukujitsus)]);
  1         1287  
  1         8  
10 1     1   559 use Encode;
  1         16487  
  1         114  
11 1     1   431 use File::Slurp;
  1         15853  
  1         123  
12 1     1   641 use Furl;
  1         35850  
  1         51  
13 1     1   12 use List::Util qw(max min);
  1         4  
  1         124  
14 1     1   408 use Smart::Args;
  1         39312  
  1         766  
15              
16             our $VERSION = '0.01';
17             our $DEFAULT_ENDPOINT = 'http://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv';
18              
19             sub new {
20 1     1 1 848 args
21             my $class,
22             my $cachefile => {optional => 1},
23             my $endpoint => {optional => 1};
24              
25 1         146 my $data;
26 1 50       7 if ($cachefile) {
27 1         10 $data = read_file($cachefile);
28             } else {
29 0   0     0 $endpoint = $endpoint || $DEFAULT_ENDPOINT;
30 0         0 $data = Furl->new()->get($endpoint)->content;
31             }
32 1         122 Encode::from_to($data, 'sjis', 'utf-8');
33              
34 1         9608 my $syukujitsus = {};
35 1         36 for my $line (split /\n/, $data) {
36 49 100       416 chop($line) if $line =~ /(\n|\r|\n\r)$/;
37 49         312 my ($date, $syukujitsu_name) = split /,/, $line;
38 49         329 my ($year, $month, $day) = split /-/, $date;
39 49 100       223 next unless $day;
40              
41 48         231 $month =~ s/^0//;
42 48         227 $day =~ s/^0//;
43 48         317 $syukujitsus->{$year}{$month}{$day} = $syukujitsu_name;
44             }
45              
46             bless +{
47 1         38 max_year => max(keys %$syukujitsus),
48             min_year => min(keys %$syukujitsus),
49             syukujitsus => $syukujitsus,
50             } => $class
51             }
52              
53             sub get_syukujitsus {
54 0     0 1 0 args
55             my $self,
56             my $year,
57             my $month => {optional => 1},
58             my $day => {optional => 1};
59              
60 0 0       0 croak "$year is too old for Japanese government calendar." if $year < $self->min_year;
61 0 0       0 croak "$year is too new for Japanese government calendar." if $year > $self->max_year;
62              
63 0 0 0     0 if (!$day and !$month) {
    0          
64 0         0 return $self->syukujitsus->{$year};
65             } elsif (!$day) {
66 0         0 return $self->syukujitsus->{$year}{$month};
67             } else {
68 0         0 return $self->syukujitsus->{$year}{$month}{$day};
69             }
70             }
71              
72             sub is_syukujitsu {
73 1     1 1 16 args
74             my $self,
75             my $year,
76             my $month,
77             my $day;
78              
79 1 50       146 croak "$year is too old for Japanese government calendar." if $year < $self->min_year;
80 1 50       24 croak "$year is too new for Japanese government calendar." if $year > $self->max_year;
81              
82 1         22 return $self->syukujitsus->{$year}{$month}{$day};
83             }
84              
85             1;
86              
87             __END__
88              
89             =head1 NAME
90              
91             Calendar::Japanese::Acme::Syukujitsu - Japanese Syukujitsu in calender
92              
93             =head1 VERSION
94              
95             Version 0.01
96              
97             =head1 SYNOPSIS
98              
99             use Calendar::Japanese::Acme::Syukujitsu;
100              
101             # Getting a list of Syukujitsus
102             $holidays = get_syukujitsus(2017, 8);
103             $holidays = get_syukujitsus(2017, 8, 18);
104              
105             # Examining whether it is holiday or not.
106             $name = is_syukujitsu(2017, 9, 11);
107              
108             =head1 DESCRIPTION
109              
110             This module read Syukujitsu information from
111             F<syukujitsu.csv> that published by Japanese government.
112             With interface that referenced to C<Calendar::Japanese::Holiday>.
113              
114             =head1 METHODS
115              
116             =head2 new([cachefile => $cachefile][endpoint => $endpoint])
117             Constructor.
118              
119             =head2 get_syukujitsus(year => $year [, month => $month [, day => $day]])
120              
121             =head2 is_syukujitsu(year => $year, month => $month, day => $day)
122              
123             =head1 AUTHOR
124              
125             Nao Muto <n@o625.com>
126              
127             =cut