File Coverage

blib/lib/WWW/Google/Time.pm
Criterion Covered Total %
statement 30 37 81.0
branch 2 6 33.3
condition 1 3 33.3
subroutine 7 8 87.5
pod 2 2 100.0
total 42 56 75.0


line stmt bran cond sub pod time code
1             package WWW::Google::Time;
2              
3 1     1   80275 use warnings;
  1         2  
  1         30  
4 1     1   4 use strict;
  1         1  
  1         36  
5              
6             our $VERSION = '1.001004'; # VERSION
7              
8 1     1   4 use LWP::UserAgent;
  1         1  
  1         23  
9 1     1   3 use URI;
  1         2  
  1         18  
10 1     1   4 use base 'Class::Accessor::Grouped';
  1         1  
  1         540  
11              
12             __PACKAGE__->mk_group_accessors( simple => qw/
13             error
14             data
15             where
16             ua
17             /);
18              
19             sub new {
20 1     1 1 1062 my ( $class, %args ) = @_;
21              
22 1         3 my $self = bless {}, $class;
23 1   33     13 $self->ua( $args{ua} || LWP::UserAgent->new( agent => "Mozilla", timeout => 30, ) );
24              
25 1         15717 return $self;
26             }
27              
28             sub get_time {
29 1     1 1 1296 my ( $self, $where ) = @_;
30 1         9 my $uri = URI->new("http://google.com/search");
31              
32             $self->$_(undef)
33 1         7120 for qw/error data/;
34              
35 1         351 $self->where( $where );
36              
37 1         129 $uri->query_form(
38             num => 100,
39             hl => 'en',
40             safe => 'off',
41             btnG => 'Search',
42             meta => '',
43             'q' => "time in $where",
44             );
45 1         290 my $response = $self->ua->get($uri);
46 1 50       701541 unless ( $response->is_success ) {
47 0         0 return $self->_set_error( $response, 'net' );
48             }
49              
50             # open my $fh, '>', 'out.txt' or die;
51             # print $fh $response->decoded_content.'\n';
52             # close $fh;
53              
54 1         16 my %data;
55             # print $response->content;
56             @data{ qw/time day_of_week month month_day year time_zone where/ } = $response->content
57             =~ m{
(.+?)
(\w+), (\w+) (\d+), (\d+) \((.+?)\)
\s+Time in (.+?) }
58             #
12:23 PM
Wednesday, December 31, 2014 (EST)
Time in Toronto, ON
59 1 50       16 or do {
60 0         0 return $self->_set_error("Could not find time data for that location");
61             };
62              
63              
64             # 2:29pm Sunday (EST) - Time in Toronto, ON, Canada
65              
66              
67 1         248 $data{where} =~ s{|}{}g;
68              
69 1         76 return $self->data( \%data );
70             }
71              
72             sub _set_error {
73 0     0     my ( $self, $error_or_response, $is_response ) = @_;
74              
75 0 0         if ( $is_response ) {
76 0           $self->error( "Network error: " . $error_or_response->status_line );
77             }
78             else {
79 0           $self->error( $error_or_response );
80             }
81 0           return;
82             }
83              
84             1;
85             __END__