File Coverage

blib/lib/WebService/TimeAndDateCom/NewYearsCountdown.pm
Criterion Covered Total %
statement 38 38 100.0
branch 9 14 64.2
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 56 61 91.8


line stmt bran cond sub pod time code
1             package WebService::TimeAndDateCom::NewYearsCountdown;
2              
3 2     2   229976 use Moo;
  2         17242  
  2         16  
4 2     2   3289 use Mojo::DOM;
  2         108644  
  2         83  
5 2     2   1410 use LWP::UserAgent;
  2         55122  
  2         103  
6             our $VERSION = '1.001';
7              
8 2         1062 use constant TIME_AND_DATE_SITE_URL
9 2     2   17 => 'http://www.timeanddate.com/counters/multicountdown.html';
  2         5  
10              
11              
12             has error => ( is => 'rw', init_arg => undef, );
13             has _ua => (
14             is => 'ro',
15             init_arg => undef,
16             default => sub {
17             LWP::UserAgent->new(
18             timeout => 30,
19             agent => 'Mozilla/5.0 (X11; Ubuntu; Linux i686; '
20             .'rv:26.0) Gecko/20100101 Firefox/26.0',
21             );
22             },
23             );
24              
25             sub hny {
26 3     3 1 40367 my ( $self, $tz ) = @_;
27              
28 3 50       14 my $data = $self->_get_data
29             or return;
30              
31 3 100       13 if ( $tz ) {
32 2         9 $tz = uc $tz;
33 2 50       9 $tz = 'GMT' if $tz eq 'UTC';
34              
35 2 100       52 my ( $tz_data ) = grep $_->{tz} eq $tz, @$data
36             or return $self->_set_error('Didn\'t find time zone ' . $tz);
37              
38 1         32 return $tz_data;
39             }
40              
41 1         6 return $data;
42             }
43              
44             sub _get_data {
45 3     3   5 my $self = shift;
46              
47 3         63 my $res = $self->_ua->get( TIME_AND_DATE_SITE_URL );
48              
49 3 50       372021 return $self->_set_error('Network error: ' . $res->status_line)
50             unless $res->is_success;
51              
52 3 50       61 my $parsed_data = $self->_parse( $res->content )
53             or return; # errored out and the error is already set
54              
55 3         253 return $parsed_data;
56             }
57              
58             sub _parse {
59 3     3   319 my ( $self, $content ) = @_;
60              
61 3         9 my @data;
62 3         7 eval {
63 3         34 my $dom = Mojo::DOM->new( $content );
64 3         315096 for my $tr ( $dom->find('table.mpad.tbbox tbody tr')->each ) {
65 120         98989 my %row;
66 120         443 @row{qw/countries tz cities time_left/}
67             = map $_->all_text, $tr->find('td')->each;
68 120         245654 push @data, +{ %row };
69             }
70             };
71              
72 3 50       2680 $@ and return $self->_set_error('Parse error: ' . $@);
73 3         30 return \@data;
74             }
75              
76             sub _set_error {
77 1     1   6 my ( $self, $error ) = @_;
78 1         25 $self->error( $error );
79 1         29 return;
80             }
81              
82             'HAPPY NEW YEAR!!!!';
83              
84             __END__