File Coverage

blib/lib/Device/CableModem/Zoom5341J/Fetch.pm
Criterion Covered Total %
statement 19 39 48.7
branch 1 14 7.1
condition n/a
subroutine 6 7 85.7
pod 0 2 0.0
total 26 62 41.9


line stmt bran cond sub pod time code
1             # Fetch page from the cablemodem
2 7     7   30 use strict;
  7         8  
  7         217  
3 7     7   32 use warnings;
  7         8  
  7         170  
4              
5 7     7   28 use Carp;
  7         8  
  7         640  
6              
7              
8             =head1 NAME
9              
10             Device::CableModem::Zoom5341J::Fetch
11              
12             =head1 NOTA BENE
13              
14             This is part of the guts of Device::CableModem::Zoom5341J. 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             =cut
25              
26             # The URL's we grab
27             my $login_url = '/login.asp';
28             my $login_post = '/goform/login';
29             my $conn_url = '/RgConnect.asp';
30              
31 7     7   8457 use LWP::UserAgent;
  7         248160  
  7         241  
32 7     7   3525 use HTTP::Request::Common qw/POST/;
  7         11015  
  7         1914  
33             sub fetch_page_rows
34             {
35 0     0 0 0 my $self = shift;
36              
37 0         0 my $ua = LWP::UserAgent->new;
38              
39 0         0 my @uerrs;
40 0         0 my $ubase = "http://$self->{modem_addr}";
41              
42             # It seems like we have to have hit the base page first, or it fails
43             # us logging in...
44 0         0 $ua->get("$ubase/");
45              
46             # POST to the login page
47 0         0 my $res = $ua->post("${ubase}${login_post}", Content => {
48             loginUsername => $self->{username},
49             loginPassword => $self->{password},
50             });
51 0 0       0 croak "Expected redirect, not $res->code"
52             unless $res->code == 302;
53              
54 0         0 my $rloc = $res->header('Location');
55 0 0       0 croak "No redirect header location" unless $rloc;
56 0 0       0 croak "Login failed" unless $rloc =~ m#$conn_url#;
57              
58              
59             # Login succeeded. Go ahead and grab the connection stats.
60 0         0 my $url = "${ubase}${conn_url}";
61 0         0 $res = $ua->get($url);
62 0 0       0 croak "Stat page request to $url failed" unless $res->is_success;
63 0         0 my $html = $res->content;
64 0 0       0 croak "Got no data from $url" unless $html;
65              
66 0         0 return $html;
67             }
68              
69              
70             =head2 ->fetch_data
71              
72             Grabs and stashes the data.
73             =cut
74             sub fetch_data
75             {
76 1     1 0 2 my $self = shift;
77              
78             # Ensure everything's clear
79 1         2 $self->{conn_html} = undef;
80 1         1 $self->{conn_stats} = undef;
81              
82             # Backdoor for testing
83 1 50       5 return if $self->{__TESTING_NO_FETCH};
84              
85 0           my $html = $self->fetch_page_rows;
86 0 0         carp "Failed fetching page from modem" unless $html;
87 0           $self->{conn_html} = $html;
88              
89 0           return;
90             }
91              
92              
93             1;
94             __END__