Zoom5341/Fetch.pm

filter: Statementstmt 47.5% 19 / 40 Branchbran 8.3% 1 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 2 totaltotal 41.6% 25 / 60 CC 8 SCAR 27.7
CC
8
Cov
38%
fetch_page_rows
31.6
fetch_connection
4.8
BEGIN
1.0
CRAP 16.0
↑ Summary Zoom5341/Parse.pm »
1 # Fetch page from the cablemodem
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 7 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 7 my $url; # Chosen one for this modem
37  
38 sub fetch_page_rows
39 0 ▶{
Subroutine fetch_page_rows: not called CC=6 SCAR=34.5
Pod undocumented
40 0 my $self = shift;
41  
42 7 ▶ use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 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 ▶ my @uopts = $url ? ($url) : (@urls);
Branch: 1 branch
BranchTrueFalseTotal
$url ? : 0 0 0
48  
49 # Try each of our candidates until one succeeds or we run out
50 0 my @uerrs;
51 my $res;
52 0 for my $u (@uopts)
53 {
54 0 $url = "http://$self->{modem_addr}/$u";
55 0 my $req = HTTP::Request->new(GET => $url);
56 0 $res = $ua->request($req);
57  
58 # Got it? Go ahead.
59 0 ▶ last if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 0 0 0
60  
61 # Otherwise rack up an error, and loop back around.
62 0 push @uerrs, "Failed HTTP GET on $url: @{[$res->status_line]}";
63 }
64  
65 # If things failed, dump out all the errors
66 0 ▶ croak join "\n", @uerrs unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
67  
68 # Make sure we got actual data
69 0 my $html = $res->content;
70 0 ▶ croak "Got no data from $url" unless($html);
Branch: 1 branch
BranchTrueFalseTotal
unless $html 0 0 0
71  
72 # Put it together and hand it back.
73 0 my @html = split /\n/, $html;
74 0 chomp @html;
75  
76 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 1 ▶{
Subroutine fetch_connection: called CC=3 SCAR=15.7
Pod undocumented
86 1 my $self = shift;
87  
88 # Ensure everything's clear
89 1 $self->{conn_html} = undef;
90 1 $self->{conn_stats} = undef;
91  
92 # Backdoor for testing
93 1 ▶ return if $self->{__TESTING_NO_FETCH};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__TESTING_NO_FETCH"} 1 0 1
94  
95 0 my @html = $self->fetch_page_rows;
96 0 ▶ carp "Failed fetching page from modem" unless @html;
Branch: 1 branch
BranchTrueFalseTotal
unless @html 0 0 0
97 0 $self->{conn_html} = \@html;
98  
99 0 return;
100 }
101  
102  
103 7 1;
104 __END__
↑ Summary Zoom5341/Parse.pm »