File Coverage

blib/lib/XML/RSS/FOXSports.pm
Criterion Covered Total %
statement 52 120 43.3
branch 6 78 7.6
condition 1 5 20.0
subroutine 19 38 50.0
pod 11 15 73.3
total 89 256 34.7


line stmt bran cond sub pod time code
1             #=========================================================i======================
2             # Copyright (c) Brian Perez 2005. All rights reserved.
3             #--------------------------------------------------------------------------------
4             # This library is free software; you can redistribute it
5             # and/or modify it under the same terms as Perl itself.
6             #================================================================================
7             package XML::RSS::FOXSports;
8              
9 1     1   20397 use 5.006;
  1         4  
  1         47  
10 1     1   6 use strict;
  1         2  
  1         46  
11 1     1   5 use warnings;
  1         16  
  1         32  
12              
13 1     1   1257 use XML::RSS::Parser;
  1         15870  
  1         34  
14 1     1   8 use base qw(XML::RSS::Parser);
  1         2  
  1         70  
15              
16 1     1   6 use Carp;
  1         2  
  1         56  
17 1     1   1652 use LWP::UserAgent;
  1         59671  
  1         40  
18 1     1   891 use XML::RSS::FOXSports::Constants qw(:feed_urls HEADLINE_BASE_URL TEAM_TO_ID);
  1         2  
  1         431  
19              
20             our $VERSION = '0.02';
21              
22             my $ua = LWP::UserAgent->new();
23             $ua->agent('XML::RSS::FOXSports/'.$VERSION);
24              
25             my $DEBUG = 0;
26              
27             sub new {
28 1     1 1 14 my $proto = shift;
29 1   33     8 my $class = ref($proto) || $proto;
30 1         3 my %opts = @_;
31 1         14 my $self = $class->SUPER::new();
32 1 50       120839 $self->_set_fsutil unless $opts{NoUtil};
33 1 50       27 $self->_set_fsmeta if $opts{Meta};
34 1 50       6 $self->_set_debug if $opts{Debug};
35 1         7 return $self;
36             }
37              
38             #-- AUTOLOAD ------
39             #-- used for method proxies
40             sub AUTOLOAD {
41 0     0   0 our $AUTOLOAD;
42 0         0 my ($method, $feed);
43 0         0 ($method = $AUTOLOAD) =~ s/.*:://;
44 0 0       0 warn "[dbg] method called: $method" if $DEBUG;
45 0 0       0 die "$method is not a currently supported method" unless $method =~ /^parse_/;
46 0         0 $feed = uc ((split('_', $method, 2))[1]);
47             #-- headlines ----
48 0 0       0 if (defined HEADLINE_FEED_URLS->{$feed}) {
    0          
    0          
49             {
50 1     1   7 no strict 'refs';
  1         2  
  1         189  
  0         0  
51 0         0 *{$AUTOLOAD} =
52             sub {
53 0     0   0 my ($rss_xml, $message) = $_[0]->_get_rss_feed(HEADLINE_FEED_URLS->{$feed});
54 0 0       0 croak "rss xml feed is empty with message $message" unless defined $rss_xml;
55 0 0       0 $_[0]->_save_meta_data($feed, HEADLINE_FEED_URLS->{$feed}) if $_[0]->{_fsmeta};
56             #-- for xml::rss::parser 4.0 compatibility
57 0 0       0 return $_[0]->SUPER::parse_string($rss_xml) if $_[0]->SUPER::can('parse_string');
58 0         0 return $_[0]->SUPER::parse($rss_xml);
59 0         0 };
60 0         0 goto &{$AUTOLOAD};
  0         0  
61             }
62             }
63             #-- video ----
64             elsif (defined VIDEO_FEED_URLS->{$feed}) {
65             {
66 1     1   4 no strict 'refs';
  1         2  
  1         202  
  0         0  
67 0         0 *{$AUTOLOAD} =
68             sub {
69 0     0   0 my ($rss_xml, $message) = $_[0]->_get_rss_feed(VIDEO_FEED_URLS->{$feed});
70 0 0       0 croak "rss xml feed is empty with message $message" unless defined $rss_xml;
71 0 0       0 $_[0]->_save_meta_data($feed, VIDEO_FEED_URLS->{$feed}) if $_[0]->{_fsmeta};
72             #-- for xml::rss::parser 4.0 compatibility
73 0 0       0 return $_[0]->SUPER::parse_string($rss_xml) if $_[0]->SUPER::can('parse_string');
74 0         0 return $_[0]->SUPER::parse($rss_xml);
75 0         0 };
76 0         0 goto &{$AUTOLOAD};
  0         0  
77             }
78             } #-- specific team (e.g., Giants, 49ers, Galaxy)
79             elsif ($feed =~ /_TEAM$/) {
80 0         0 my ($league, $ext) = split('_', $feed, 2);
81 0 0       0 if (defined TEAM_TO_ID->{$league}) {
82 0 0       0 croak 'sports team was not found, perhaps you forgot to supply one.' unless defined $_[1];
83             {
84 1     1   4 no strict 'refs';
  1         1  
  1         1086  
  0         0  
85 0         0 *{$AUTOLOAD} =
86             sub {
87 0     0   0 my $url = HEADLINE_BASE_URL . TEAM_TO_ID->{$league}->{lc $_[1]};
88 0         0 my ($rss_xml, $message) = $_[0]->_get_rss_feed($url);
89 0 0       0 croak "rss xml feed is empty with message $message" unless defined $rss_xml;
90 0 0       0 $_[0]->_save_meta_data($league.'_'.lc $_[1], $url) if $_[0]->{_fsmeta};
91             #-- for xml::rss::parser 4.0 compatibility
92 0 0       0 return $_[0]->SUPER::parse_string($rss_xml) if $_[0]->SUPER::can('parse_string');
93 0         0 return $_[0]->SUPER::parse($rss_xml);
94 0         0 };
95 0         0 goto &{$AUTOLOAD};
  0         0  
96             }
97             }
98             }
99             else {
100 0         0 die "$method is not a currently supported method";
101             }
102             }
103              
104             #-- parse_team ------
105             sub parse_team {
106 0 0   0 1 0 warn "[dbg] (parse_team) league:$_[1] team:$_[2]" if $DEBUG;
107 0 0       0 TEAM_TO_ID->{uc $_[1]} or die "the league $_[1] is not currently available";
108 0 0       0 TEAM_TO_ID->{uc $_[1]}{lc $_[2]} or die "the team $_[2] is not currently available";
109 0         0 my $sub = join('','parse_', $_[1], '_team');
110 0 0       0 warn "[dbg] (parse_team) sub:$sub" if $DEBUG;
111 0         0 $_[0]->$sub($_[2]);
112             }
113              
114              
115             #-- util methods ------
116 1 50   1 1 10 sub http_timeout { $_[1] ? $ua->timeout($_[1]) : $ua->timeout }
117 0 0   0 0 0 sub last_parsed_url { $_[0]->{_fsmeta}{last_parsed_url} or '' }
118 0 0   0 0 0 sub last_parsed_feed { $_[0]->{_fsmeta}{last_parsed_feed} or '' }
119 0 0   0 0 0 sub parsed_feed { $_[0]->{_fsmeta}{parsed_feeds} or {} }
120              
121             sub get_feed_url {
122 0 0   0 1 0 $_[0]->{_fsutil}
123             ? $_[0]->{_fsutil}->get_feed_url($_[1])
124             : $_[0]->_na;
125             }
126              
127             sub get_feed_urls {
128 0 0   0 1 0 $_[0]->{_fsutil}
129             ? $_[0]->{_fsutil}->get_feed_urls
130             : $_[0]->_na;
131             }
132              
133             sub get_available_feeds {
134 0 0   0 1 0 $_[0]->{_fsutil}
135             ? $_[0]->{_fsutil}->get_available_feeds
136             : $_[0]->_na;
137             }
138              
139             sub get_available_teams {
140 0 0   0 1 0 $_[0]->{_fsutil}
141             ? $_[0]->{_fsutil}->get_available_teams
142             : $_[0]->_na;
143             }
144              
145             sub get_available_leagues {
146 1 50   1 1 232 $_[0]->{_fsutil}
147             ? $_[0]->{_fsutil}->get_available_leagues
148             : $_[0]->_na;
149             }
150              
151             sub get_available_headline_feeds {
152 0 0   0 1 0 $_[0]->{_fsutil}
153             ? $_[0]->{_fsutil}->get_available_headline_feeds
154             : $_[0]->_na;
155             }
156              
157             sub get_available_video_feeds {
158 0 0   0 1 0 $_[0]->{_fsutil}
159             ? $_[0]->{_fsutil}->get_available_video_feeds
160             : $_[0]->_na;
161             }
162              
163             sub get_available_leagues_teams {
164 0 0   0 1 0 $_[0]->{_fsutil}
165             ? $_[0]->{_fsutil}->get_available_leagues_teams
166             : $_[0]->_na;
167             }
168              
169 1 50   1 0 6 sub debug { $_[1] ? $_[0]->_set_debug($_[1]) : $DEBUG }
170              
171             sub DESTROY {
172 1     1   365 my $self = shift;
173 1         8 delete $self->{_fsutil};
174 1         133 delete $self->{_fsmeta};
175             #$self->SUPER::DESTROY;
176             }
177              
178              
179             #== PRIVATE METHODS ===================================
180              
181             sub _get_rss_feed {
182 0     0   0 my $res = $ua->get($_[1]);
183 0 0       0 warn "[dbg] url requested: $_[1]" if $DEBUG;
184 0 0       0 warn "[dbg] status line: $res->status_line" if $DEBUG;
185 0 0       0 return $res->content, undef if $res->is_success;
186 0         0 return undef, $res->status_line;
187             }
188              
189             #-- avoid clashes
190             sub _set_fsutil {
191 1     1   749 use XML::RSS::FOXSports::Utils;
  1         3  
  1         179  
192 1     1   11 $_[0]->{_fsutil} = XML::RSS::FOXSports::Utils->new();
193             }
194              
195             sub _set_fsmeta {
196 0     0   0 $_[0]->{_fsmeta} = {
197             last_parsed_feed => '',
198             last_parsed_url => '',
199             parsed_feeds => {},
200             };
201             }
202              
203 1     1   416 sub _na { "not available" }
204              
205 0   0 0     sub _set_debug { $DEBUG = $_[1] || 1; }
206              
207             sub _save_meta_data {
208 0     0     $_[0]->{_fsmeta}{last_parsed_feed} = $_[1];
209 0           $_[0]->{_fsmeta}{last_parsed_url} = $_[2];
210 0           $_[0]->{_fsmeta}{parsed_feeds}{$_[1]}++;
211             }
212              
213              
214             1;
215              
216              
217             __END__