File Coverage

blib/lib/Device/CableModem/Zoom5341/Fetch.pm
Criterion Covered Total %
statement 16 37 43.2
branch 1 12 8.3
condition n/a
subroutine 5 6 83.3
pod 0 2 0.0
total 22 57 38.6


line stmt bran cond sub pod time code
1             # Fetch page from the cablemodem
2 7     7   43 use strict;
  7         16  
  7         226  
3 7     7   42 use warnings;
  7         13  
  7         2036  
4              
5 7     7   40 use Carp;
  7         15  
  7         1014  
6              
7              
8             =head1 NAME
9              
10             Device::CableModem::Zoom5341::Fetch
11              
12             =head1 NOTA BENE
13              
14             This is part of the guts of Device::CableModem::Zoom5341. If you're
15             reading this, you're either developing the module, writing tests, or
16             coloring outside the lines; consider yourself warned.
17              
18             =cut
19              
20              
21             =head2 ->fetch_page_rows
22              
23             Grabs the connection status page from the modem, returns the given HTML
24             as an array of lines.
25             =cut
26              
27             # The URL's have changed over time
28             my @urls = (
29             # This one exists in SW version 3.1.0.1pre3 and possibly earlier
30             'admin/cable-status.asp',
31              
32             # This one existed in older stuff, back in 2011 and for some time
33             # after.
34             'status_connection.asp',
35             );
36             my $url; # Chosen one for this modem
37              
38             sub fetch_page_rows
39             {
40 0     0 0 0 my $self = shift;
41              
42 7     7   12233 use LWP::UserAgent;
  7         420696  
  7         2482  
43 0         0 my $ua = LWP::UserAgent->new;
44              
45             # Use the URL we already found, if we found one; otherwise try 'em
46             # all.
47 0 0       0 my @uopts = $url ? ($url) : (@urls);
48              
49             # Try each of our candidates until one succeeds or we run out
50 0         0 my @uerrs;
51             my $res;
52 0         0 for my $u (@uopts)
53             {
54 0         0 $url = "http://$self->{modem_addr}/$u";
55 0         0 my $req = HTTP::Request->new(GET => $url);
56 0         0 $res = $ua->request($req);
57              
58             # Got it? Go ahead.
59 0 0       0 last if $res->is_success;
60              
61             # Otherwise rack up an error, and loop back around.
62 0         0 push @uerrs, "Failed HTTP GET on $url: @{[$res->status_line]}";
  0         0  
63             }
64              
65             # If things failed, dump out all the errors
66 0 0       0 croak join "\n", @uerrs unless $res->is_success;
67              
68             # Make sure we got actual data
69 0         0 my $html = $res->content;
70 0 0       0 croak "Got no data from $url" unless($html);
71              
72             # Put it together and hand it back.
73 0         0 my @html = split /\n/, $html;
74 0         0 chomp @html;
75              
76 0         0 return @html;
77             }
78              
79              
80             =head2 ->fetch_connection
81              
82             Grabs and stashes the connection status page.
83             =cut
84             sub fetch_connection
85             {
86 1     1 0 3 my $self = shift;
87              
88             # Ensure everything's clear
89 1         2 $self->{conn_html} = undef;
90 1         2 $self->{conn_stats} = undef;
91              
92             # Backdoor for testing
93 1 50       6 return if $self->{__TESTING_NO_FETCH};
94              
95 0           my @html = $self->fetch_page_rows;
96 0 0         carp "Failed fetching page from modem" unless @html;
97 0           $self->{conn_html} = \@html;
98              
99 0           return;
100             }
101              
102              
103             1;
104             __END__