Fritz/Box.pm

filter: Statementstmt 83.9% 68 / 81 Branchbran 60.0% 6 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 14 / 16 Podpod 75.0% 3 / 4 totaltotal 81.9% 91 / 111 CC 6 SCAR 7.1
CC
6
Cov
81%
dump
6.0
call
3.2
BUILDARGS
2.0
CRAP 2.0
« Fritz/Action.pm ↑ Summary Fritz/Data.pm »
1 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 # Copyright (C) 2015 Christian Garbs <mitch@cgarbs.de>
4 # Licensed under GNU GPL v2 or later.
5  
6 package Net::Fritz::Box;
7 # ABSTRACT: main configuration and entry point for L<Net::Fritz> distribution
8 5 $Net::Fritz::Box::VERSION = 'v0.0.9';
9  
10 # We need LWP::UserAgent 6.00 because of ssl_opts. We could work with
11 # an older version as seen in https://github.com/rhuss/jmx4perl/issues/28
12 # but SOAP::Lite might then have the same problem... For now just
13 # stick with the versioned dependency.
14 5 ▶use LWP::UserAgent 6.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use XML::Simple qw(:strict);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 $XML::Simple::PREFERRED_PARSER = 'XML::Parser';
18  
19 5 ▶use AppConfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 5 ▶use Net::Fritz::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Net::Fritz::Device;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use Net::Fritz::ConfigFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 5 with 'Net::Fritz::IsNoError';
28  
29  
30 5 has upnp_url => ( is => 'ro', default => 'https://fritz.box:49443' );
31  
32  
33 5 has trdesc_path => ( is => 'ro', default => '/tr64desc.xml' );
34  
35  
36 5 has username => ( is => 'ro' );
37  
38  
39 5 has password => ( is => 'ro' );
40  
41  
42 5 has configfile => ( is => 'ro' );
43  
44  
45 18 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=6.9
Pod undocumented
46 18 my ( $class, %args ) = @_;
47  
48 18 ▶ if (exists $args{configfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"configfile"}) 3 15 18
49  
50 3 my $config = Net::Fritz::ConfigFile->new( $args{configfile} );
51  
52 # get configuration variables
53 3 my %config_vars = %{$config->configuration};
54  
55 # merge both hashes: %args wins over %config_vars for duplicate keys
56 3 %args = ( %config_vars, %args );
57
58 # always update 'configfile' from $config (might be expanded or auto-selected)
59 3 $args{'configfile'} = $config->configfile;
60 }
61  
62 18 return \%args;
63 };
64  
65 # internal XML::Simple instance (lazy)
66 5 has _xs => ( is => 'lazy', init_arg => undef );
67  
68 3 ▶sub _build__xs {
Subroutine _build__xs: called CC=1 SCAR=0.0
69 3 return XML::Simple->new(ForceArray => 1, KeyAttr => []);
70 }
71  
72 # internal LWP::UserAgent instance (lazy)
73 # overwriting the default might be handy for setting proxy variables or the like
74 5 has _ua => ( is => 'lazy' );
75  
76 9 ▶sub _build__ua {
Subroutine _build__ua: called CC=1 SCAR=0.0
77 # Depending on your SSL setup (propably which SSL modules LWP::UserAgent uses
78 # for transport), this is also needed to disable certificate checks.
79 # I have one machine that needs it and another that doesn't.
80 9 $ENV{HTTPS_DEBUG} = 1;
81
82 9 my $ua = LWP::UserAgent->new(keep_alive => 1);
83 9 $ua->conn_cache({total_capacity => 4});
84 # disable SSL certificate checks, Fritz!Box has no verifiable SSL certificate
85 9 $ua->ssl_opts(verify_hostname => 0 ,SSL_verify_mode => 0x00);
86
87 9 return $ua;
88 }
89  
90 # internal SSL attributes for SOAP::Lite instances (lazy)
91 5 has _sslopts => ( is => 'lazy' );
92  
93 0 ▶sub _build__sslopts {
Subroutine _build__sslopts: not called CC=1 SCAR=6.9
94 0 my $self = shift;
95  
96 # use the SSL options from the LWP::UserAgent instance
97 # copy ALL keys including undefined values
98 0 return [ map { $_ => $self->_ua->ssl_opts($_) } $self->_ua->ssl_opts ];
99 }
100  
101  
102 4 ▶sub discover {
Subroutine discover: called CC=2 SCAR=6.9
Pod documented
103 4 my $self = shift;
104  
105 4 my $url = $self->upnp_url . $self->trdesc_path;
106
107 4 my $response = $self->_ua->get($url);
108
109 4 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 2 2 4
110 return Net::Fritz::Device->new(
111 2 xmltree => $self->_xs->parse_string($response->decoded_content)->{device}->[0],
112 fritz => $self
113 );
114 }
115 else {
116 2 return Net::Fritz::Error->new($response->status_line)
117 }
118 }
119  
120  
121 5 has _device_cache => ( is => 'rw' );
122  
123 2 ▶sub call {
Subroutine call: called CC=3 SCAR=11.7
Pod documented
124 2 my $self = shift;
125 2 my $service = shift;
126 2 my $action = shift;
127 2 my %call_args = (@_);
128  
129 2 my $device = $self->_device_cache;
130 2 ▶ if (!defined $device) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $device) 2 0 2
131 2 $device = $self->discover();
132 1 ▶ return $device if $device->error;
Branch: 1 branch
BranchTrueFalseTotal
if $device->error 1 0 1
133 0 $self->_device_cache($device);
134 }
135  
136 0 return $device->call($service, $action, %call_args);
137 }
138  
139  
140 0 ▶sub dump {
Subroutine dump: not called CC=2 SCAR=17.9
Pod documented
141 0 my $self = shift;
142 0 my $indent = shift;
143 0 ▶ $indent = '' unless defined $indent;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $indent 0 0 0
144  
145 0 my $text = "${indent}Net::Fritz::Box:\n";
146 0 $indent .= ' ';
147 0 $text .= "${indent}upnp_url = " . $self->upnp_url . "\n";
148 0 $text .= "${indent}trdesc_path = " . $self->trdesc_path . "\n";
149  
150 0 return $text;
151 }
152  
153  
154 5 1;
155  
156 __END__
« Fritz/Action.pm ↑ Summary Fritz/Data.pm »