File Coverage

blib/lib/WWW/Baseball/NPB.pm
Criterion Covered Total %
statement 37 39 94.8
branch 3 4 75.0
condition n/a
subroutine 10 11 90.9
pod 0 3 0.0
total 50 57 87.7


\n)?(?:)?
line stmt bran cond sub pod time code
1             package WWW::Baseball::NPB;
2              
3 4     4   99766 use strict;
  4         11  
  4         212  
4 4     4   20 use vars qw($VERSION);
  4         9  
  4         221  
5             $VERSION = 0.03;
6              
7 4     4   3218 use LWP::Simple;
  4         391526  
  4         40  
8 4     4   4656 use WWW::Baseball::NPB::Game;
  4         9  
  4         116  
9              
10 4     4   53 use vars qw($YahooURL);
  4         7  
  4         2205  
11             $YahooURL = 'http://sports.yahoo.co.jp/baseball/';
12              
13 0     0 0 0 sub croak { require Carp; Carp::croak(@_) }
  0         0  
14              
15             sub new {
16 3     3 0 80 my $class = shift;
17 3         15 my $self = bless { games => [] }, $class;
18 3         17 $self->_parse_body($YahooURL);
19 3         12 return $self;
20             }
21              
22             sub games {
23 4     4 0 501 my($self, $league) = @_;
24 4 100       27 unless ($league) {
25 3         5 return @{$self->{games}};
  3         18  
26             }
27 1         1 return grep { $_->league eq $league } @{$self->{games}};
  6         15  
  1         5  
28             }
29              
30             sub _parse_body {
31 3     3   7 my($self, $url) = @_;
32 3 50       11 my $body = LWP::Simple::get($url) or croak("Can't get content of $url");
33 3         53569 my $re = $self->_match_pattern;
34 3         240 while ($body =~ m/$re/g) {
35 18         55 $self->_add_game($1, $2, $3, $4, $5, $6, $7);
36             }
37             }
38              
39             my %league = (cl => 'central', pl => 'pacific');
40              
41             sub _add_game {
42 18     18   85 my($self, $home, $home_score, $visitor_score, $league, $status, $visitor, $stadium) = @_;
43 18         31 for ($home_score, $visitor_score) {
44 36         92 s,(.*),$1,;
45             }
46 18         24 push @{$self->{games}}, WWW::Baseball::NPB::Game->new(
  18         169  
47             home => $home,
48             visitor => $visitor,
49             score => {
50             $home => $home_score,
51             $visitor => $visitor_score,
52             },
53             league => $league{$league},
54             status => $status,
55             stadium => $stadium,
56             );
57             }
58              
59             sub _match_pattern {
60 3     3   11 return <<'RE';
61            
62            
63             (.*?)
64            
65            
66             (\S*) ?- ?(\S*)
(.*?)
67            
68             (.*?)
69            
70             (?:
71            
\[ ещеде╓ \]
72             (.*?)
73            
74            
75             RE
76             ;
77             }
78              
79             1;
80             __END__