blib/lib/Finance/Google/Sector/Mean.pm

filter: Statementstmt 17.8% 21 / 118 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 22.2% 4 / 18 Podpod 0.0% 0 / 2 totaltotal 12.6% 25 / 197 CC 28 SCAR 42.5
CC
28
Cov
12%
sectorsummary
210.0
_findtable
12.0
_findResults
12.0
CRAP 70.4
↑ Summary
1 package Finance::Google::Sector::Mean;
2  
3 1 ▶use Statistics::Basic qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use LWP::Simple qw($ua get);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 require Exporter;
8  
9 1 $ua->timeout(15);
10  
11  
12 1 our @ISA = qw(Exporter);
13  
14 # Items to export into callers namespace by default. Note: do not export
15 # names by default without a very good reason. Use EXPORT_OK instead.
16 # Do not simply export all your public functions/methods/constants.
17  
18 # This allows declaration use Finance::NASDAQ::Markets ':all';
19 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
20 # will save memory.
21 1 our %EXPORT_TAGS = ( 'all' => [ qw(
22
23 ) ] );
24  
25 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
26  
27 1 our @EXPORT = qw(
28 sectorsummary
29 );
30  
31 1 our $VERSION = '0.08';
32  
33  
34  
35 0 ▶sub sectorsummary{
Subroutine sectorsummary: not called CC=14 SCAR=53.5
Pod undocumented
36
37 0 my ($symbol,$source) = @_;
38 0 my @buffer = ();
39 0 my $url = "";
40 0 $url = "http://www.google.com/finance";
41
42 0 my @ids = qw/secperf/;
43  
44 0 my $content = get $url;
45 0 ▶ return unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 0 0
46  
47  
48 0 my $tree = HTML::TreeBuilder->new;
49  
50
51 0 $tree->parse($content);
52 0 my %quote;
53 0 my %out=();
54
55 0 @quote{qw/secperf/} = map { _finddiv($tree,$_,'id') } @ids;
56
57
58 0 ▶ if(defined($quote{secperf})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $quote{"secperf"}) 0 0 0
59 # $quote{yfnc_modtitlew2} =~s/(&|-|\"|\'|\,|\.|amp;)//g;
60 # $quote{yfnc_modtitlew2} =~ m/Sector:(.*?)Industry:(.*?)Full/;
61
62
63 0 $out{SectorChange}{buffer}=[split "% |down / up",$quote{secperf}];
64
65 0 pop @{$out{SectorChange}{buffer}};
66 }
67  
68
69  
70 0 foreach(@{$out{SectorChange}{buffer}}){
71 0 my @set = ();
72 0 $_ = trim($_);
73 0 ▶ next if($_ =~/SectorChange/ || $_ eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /SectorChange/ or $_ eq "" 0 0 0
Condition0%0/3
$_ =~ /SectorChange/ or $_ eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ =~ /SectorChange/ or $_ eq ""
$_ =~ /SectorChange/ $_ eq ""
Truth table0%0/3
A or B
A= $_ =~ /SectorChange/B= $_ eq ""
ABresult
1X1
011
000
74
75 0 ▶ if($_ =~/Non*Cyclical/ && $_ =~/\+/){
Branch: 2 branches
BranchTrueFalseTotal
if ($_ =~ /Non*Cyclical/ and $_ =~ /\+/) { } 0 0 0
elsif ($_ =~ /Non*Cyclical/ and $_ =~ /\-/) { } 0 0 0
Condition0%0/3
$_ =~ /Non*Cyclical/ and $_ =~ /\+/
!ll&&!rl&&r
000
MC/DC0%0/2
$_ =~ /Non*Cyclical/ and $_ =~ /\+/
$_ =~ /Non*Cyclical/ $_ =~ /\+/
Truth table0%0/3
A and B
A= $_ =~ /Non*Cyclical/B= $_ =~ /\+/
ABresult
0X0
100
111
76 0 @set = split /\+/,$_;
77 0 $set[0] = "ConsNonCyclical";
78  
79  
80 0 $out{sectors}->{pos}->{$set[0]}=sprintf("%3.3f",$set[1]);
81 ▶ }elsif($_ =~/Non*Cyclical/ && $_ =~/\-/){
Condition0%0/3
$_ =~ /Non*Cyclical/ and $_ =~ /\-/
!ll&&!rl&&r
000
MC/DC0%0/2
$_ =~ /Non*Cyclical/ and $_ =~ /\-/
$_ =~ /Non*Cyclical/ $_ =~ /\-/
Truth table0%0/3
A and B
A= $_ =~ /Non*Cyclical/B= $_ =~ /\-/
ABresult
0X0
100
111
82 0 @set = split /\-/,$_;
83 0 $set[0] = "ConsNonCyclical";
84 0 $set[1] = $set[2];
85  
86
87 0 $out{sectors}->{pos}->{$set[0]}=sprintf("%3.3f",$set[1]);
88
89 }else{
90
91
92 0 ▶ if($_ =~ /\+/){
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /\+/) { } 0 0 0
93 0 @set = split /\+/,$_;
94
95 0 $out{sectors}->{pos}->{$set[0]}=sprintf("%3.3f",$set[1]);
96 }else{
97 0 @set = split /-/,$_;
98  
99
100
101 0 ▶ $set[1]=0.00 unless($set[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless $set[1] 0 0 0
102 0 $out{sectors}->{neg}->{$set[0]}=sprintf("%3.3f",$set[1]);
103 }
104
105 }
106
107 }
108
109 0 $tree = $tree->delete();
110  
111 0 my $i = 0;
112
113 0 $out{avgs}->{pos}->{mean} = mean(values %{$out{sectors}->{pos}});
114 0 $out{avgs}->{pos}->{max} = max(values %{$out{sectors}->{pos}});
115 0 $out{avgs}->{pos}->{min} = min(values %{$out{sectors}->{pos}});
116  
117 0 $out{avgs}->{neg}->{mean} = mean(values %{$out{sectors}->{neg}});
118 0 $out{avgs}->{neg}->{max} = max(values %{$out{sectors}->{neg}});
119 0 $out{avgs}->{neg}->{min} = min(values %{$out{sectors}->{neg}});
120  
121
122
123  
124  
125 0 foreach my $sec(keys %{$out{sectors}->{pos}}){
126
127 0 ▶ if($out{avgs}->{pos}->{mean} <= $out{sectors}->{pos}->{$sec}){
Branch: 1 branch
BranchTrueFalseTotal
if ($out{"avgs"}{"pos"}{"mean"} <= $out{"sectors"}{"pos"}{$sec}) 0 0 0
128 0 $out{sectors}->{overavg}->{$sec} = $out{sectors}->{pos}->{$sec};
129 }
130 }
131
132
133
134
135
136 0 return \%out;
137
138  
139
140 }
141  
142 sub trim
143 0 ▶{
Subroutine trim: not called CC=2 SCAR=17.9
Pod undocumented
144 0 my $string = shift;
145 0 ▶ $string = "" unless $string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 0 0 0
146 0 $string =~ s/^\s+//;
147 0 $string =~ s/\s+$//;
148 0 $string =~ s/\t//;
149 0 $string =~ s/^\s//;
150 0 return $string;
151 }
152  
153  
154 # for look_down
155 0 ▶sub _id {
Subroutine _id: not called CC=1 SCAR=6.9
156 0 my $id = shift;
157 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
158 0 my ($tag) = @_;
159 0 ▶ if (defined $tag->attr('id')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag->attr('id')) { } 0 0 0
160 0 return $tag->attr('id') eq $id;
161 } else {
162 0 return 0;
163 }
164 }
165 0 }
166  
167 # for look_down
168 0 ▶sub _class {
Subroutine _class: not called CC=1 SCAR=6.9
169 0 my $id = shift;
170 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
171 0 my ($tag) = @_;
172 0 ▶ if (defined $tag->attr('class')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag->attr('class')) { } 0 0 0
173 0 return $tag->attr('class') eq $id;
174 } else {
175 0 return 0;
176 }
177 }
178 0 }
179  
180 0 ▶sub _findGeneric {
Subroutine _findGeneric: not called CC=2 SCAR=17.9
181 0 my ($tree,$tag,$id) = @_;
182 0 my $elem = $tree->look_down('_tag',$tag, _id($id));
183 0 ▶ return defined $elem ? $elem->as_text : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $elem ? : 0 0 0
184 }
185  
186 0 ▶sub _findspan {
Subroutine _findspan: not called CC=2 SCAR=17.9
187 0 my ($tree,$id) = @_;
188 0 my $elem = $tree->look_down('_tag', 'span', _id($id));
189 0 ▶ return defined $elem ? $elem->as_text : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $elem ? : 0 0 0
190 }
191 0 ▶sub _findtd {
Subroutine _findtd: not called CC=2 SCAR=17.9
192 0 my ($tree,$id) = @_;
193 0 my $elem = $tree->look_down('_tag', 'td', _class($id));
194 0 ▶ return defined $elem ? $elem->as_text : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $elem ? : 0 0 0
195 }
196  
197 0 ▶sub _findtable {
Subroutine _findtable: not called CC=3 SCAR=24.8
198 0 my ($tree,$id,$type) = @_;
199 0 ▶ my $elem = $tree->look_down('_tag', 'table', $type eq 'id'? _id($id) : _class($id));
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'id' ? : 0 0 0
200  
201  
202  
203 0 ▶ return defined $elem ? $elem->as_text : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $elem ? : 0 0 0
204 }
205 0 ▶sub _findResults {
Subroutine _findResults: not called CC=3 SCAR=24.8
206 0 my ($tree,$id,$type) = @_;
207 0 ▶ my $elem = $tree->look_down('_tag', 'table', $type eq 'id'? _id($id) : _class($id));
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'id' ? : 0 0 0
208  
209  
210  
211 0 ▶ return defined $elem ? $elem : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $elem ? : 0 0 0
212 }
213  
214 0 ▶sub _finddiv {
Subroutine _finddiv: not called CC=3 SCAR=24.8
215 0 my ($tree,$id,$type) = @_;
216 0 ▶ my $elem = $tree->look_down('_tag', 'div', $type eq 'id'? _id($id) : _class($id));
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'id' ? : 0 0 0
217  
218  
219  
220 0 ▶ return defined $elem ? $elem->as_text : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $elem ? : 0 0 0
221 }
222  
223 0 ▶sub _findol {
Subroutine _findol: not called CC=3 SCAR=24.8
224 0 my ($tree,$id,$type) = @_;
225 0 ▶ my $elem = $tree->look_down('_tag', 'ol', $type eq 'id'? _id($id) : _class($id));
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'id' ? : 0 0 0
226  
227  
228  
229 0 ▶ return defined $elem ? $elem->as_text : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $elem ? : 0 0 0
230 }
231  
232 # format %quote as a string
233 0 ▶sub _as_text {
Subroutine _as_text: not called CC=1 SCAR=6.9
234 0 my ($symbol,%quote) = @_;
235 return sprintf ("%s: \$%2.2f, %s%s (%s%s), vol %s", $symbol,
236 0 @quote{qw/prc sgn net sgn pct vol/});
237 }
238  
239  
240  
241  
242  
243 1 1;
244 __DATA__
↑ Summary