blib/lib/Finance/YahooJPN/QuoteDetail.pm

filter: Statementstmt 57.8% 88 / 152 Branchbran 27.7% 10 / 36 Conditioncond 22.2% 6 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 62.9% 17 / 27 Podpod 0.0% 0 / 12 totaltotal 44.9% 121 / 269 CC 31 SCAR 22.0
CC
31
Cov
48%
_extract_data_from_table
20.7
new
18.2
get_volume
12.0
CRAP 9.0
↑ Summary
1 package Finance::YahooJPN::QuoteDetail;
2  
3 1 ▶use 5.008003;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTML::TableExtract;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 require Exporter;
10  
11 1 our $VERSION = '0.01'; # 2004-04-12
12  
13 1 ▶sub new(){
Subroutine new: called CC=11 SCAR=29.0
Pod undocumented
14 1 my ($class,$option) = @_;
15 1 my $self = {};
16 1 bless $self,$class;
17  
18 1 foreach my $key (keys %{$option}) {
19 2 my $lowercase = $key;
20 2 $lowercase =~ tr/A-Z/a-z/;
21 2 ▶ unless ($lowercase eq 'symbol' or $lowercase eq 'proxy' or $lowercase eq 'market') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lowercase eq "symbol" or $lowercase eq "proxy" or $lowercase eq "market") 0 2 2
Condition66%4/6
$lowercase eq "symbol" or $lowercase eq "proxy" or $lowercase eq "market"
l!l&&r!l&&!r
110
$lowercase eq "symbol" or $lowercase eq "proxy"
l!l&&r!l&&!r
101
MC/DC0%0/3
$lowercase eq "symbol" or $lowercase eq "proxy" or $lowercase eq "market"
$lowercase eq "symbol" $lowercase eq "proxy" $lowercase eq "market"
Truth table50%2/4
A or B or C
A= $lowercase eq "symbol"B= $lowercase eq "proxy"C= $lowercase eq "market"
ABCresult
1XX1
01X1
0011
0000
22 0 croak "Invalid attribute name: $key";
23 }
24 2 $$self{$lowercase} = $$option{$key};
25 }
26  
27 1 ▶ unless ($$self{'symbol'} =~ /^\d{4}$/ || $$self{'symbol'} =~ /^\d{4}\.[a-zA-Z]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"symbol"} =~ /^\d{4}$/ or $self->{"symbol"} =~ /^\d{4}\.[a-zA-Z]$/) 0 1 1
Condition33%1/3
$self->{"symbol"} =~ /^\d{4}$/ or $self->{"symbol"} =~ /^\d{4}\.[a-zA-Z]$/
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{"symbol"} =~ /^\d{4}$/ or $self->{"symbol"} =~ /^\d{4}\.[a-zA-Z]$/
$self->{"symbol"} =~ /^\d{4}$/ $self->{"symbol"} =~ /^\d{4}\.[a-zA-Z]$/
Truth table33%1/3
A or B
A= $self->{"symbol"} =~ /^\d{4}$/B= $self->{"symbol"} =~ /^\d{4}\.[a-zA-Z]$/
ABresult
1X1
011
000
28 0 croak "The 'symbol' attribute must not be omitted .A stock symbol should be given with four numbers. (ex. `6758' or '6758.t' )";
29 }
30  
31 1 ▶ if($$self{symbol} =~ /^(\d{4})\.([a-zA-Z])$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'symbol'} =~ /^(\d{4})\.([a-zA-Z])$/) { } 0 1 1
elsif ($self->{'symbol'} =~ /^(\d{4})$/) { } 1 0 1
32 0 $$self{'symbol'} = $1;
33 0 $$self{'market'} = $2;
34 }elsif($$self{symbol} =~ /^(\d{4})$/){
35 1 ▶ unless($$self{'market'} && $$self{'market'} =~ /^[a-zA-Z]$/){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"market"} and $self->{"market"} =~ /^[a-zA-Z]$/) 0 1 1
Condition33%1/3
$self->{"market"} and $self->{"market"} =~ /^[a-zA-Z]$/
!ll&&!rl&&r
001
MC/DC0%0/2
$self->{"market"} and $self->{"market"} =~ /^[a-zA-Z]$/
$self->{"market"} $self->{"market"} =~ /^[a-zA-Z]$/
Truth table33%1/3
A and B
A= $self->{"market"}B= $self->{"market"} =~ /^[a-zA-Z]$/
ABresult
0X0
100
111
36 0 croak "The 'market' attribute must not be omitted ";
37 }
38 }
39  
40 1 $$self{'url'} = 'http://quote.yahoo.co.jp/q?s='.$$self{'symbol'}.'&d='.$$self{'market'};
41  
42 1 return $self;
43 }
44  
45 0 ▶sub check(){
Subroutine check: not called CC=1 SCAR=6.9
Pod undocumented
46 0 my $self = shift;
47  
48 0 print "proxy is ";
49 0 print $$self{'proxy'};
50 0 print "\n";
51  
52 0 print "symbol is ";
53 0 print $$self{'symbol'};
54 0 print "\n";
55  
56 0 print "url is ";
57 0 print $$self{'url'};
58 0 print "\n";
59  
60 }
61  
62 1 ▶sub quote(){
Subroutine quote: called CC=1 SCAR=0.0
Pod undocumented
63 1 my $self = shift;
64 1 my $html_string = $self->_access_to_yahoo;
65 1 $$self{'string_ref'} = $self->_extract_data_from_table();
66  
67 1 $self->_set_last_trade();
68 1 $self->_set_high_price();
69 1 $self->_set_low_price();
70 1 $self->_set_prev_close();
71 1 $self->_set_volume();
72 1 $self->_set_change();
73  
74 }
75  
76 0 ▶sub quick_quote(){
Subroutine quick_quote: not called CC=1 SCAR=6.9
Pod undocumented
77 0 my $self = shift;
78 0 my $html_string = $self->_access_to_yahoo;
79 0 $$self{'string_ref'} = $self->_extract_data_from_table();
80 }
81  
82 1 ▶sub _access_to_yahoo(){
Subroutine _access_to_yahoo: called CC=3 SCAR=11.4
83 1 my $self = shift;
84  
85 #Create a user agent object
86 1 my $ua = LWP::UserAgent->new(env_proxy => 1,
87 keep_alive => 1,
88 timeout => 30,
89 );
90 1 ▶ $ua->proxy(['http'], $$self{'proxy'}) if $$self{'proxy'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"proxy"} 0 1 1
91  
92 1 $ua->agent("MyApp/0.1");
93  
94 1 my $req = HTTP::Request->new(GET=>$$self{'url'});
95
96 1 $req->content_type('application/x-www-form-urlencoded');
97 1 $req->content('');
98
99 # Pass request to the user agent and get a response back
100 1 my $res = $ua->request($req);
101
102 1 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 1 1
103 0 $$self{'_html_string'} = $res->content;
104 0 return 1;
105 } else {
106 1 $$self{'error_msg'} = "Cannot access to Yahoo!";
107 1 return 0;
108 }
109 }
110  
111 1 ▶sub _extract_data_from_table(){
Subroutine _extract_data_from_table: called CC=7 SCAR=30.3
112 1 my $self = shift;
113  
114 1 my $te = new HTML::TableExtract;
115 1 my $string = "";
116 1 my $ts = "";
117 1 my $line = "";
118  
119 1 $te->parse($$self{'_html_string'});
120
121 # Examine all matching tables
122 1 foreach $ts ($te->table_states) {
123
124 0 ▶ if( $ts->depth eq "1" && $ts->count eq "3" ||
Branch: 1 branch
BranchTrueFalseTotal
if ($ts->depth eq 1 and $ts->count eq 3 or $ts->depth eq 0 and $ts->count eq 5) 0 0 0
Condition0%0/9
$ts->depth eq 1 and $ts->count eq 3 or $ts->depth eq 0 and $ts->count eq 5
l!l&&r!l&&!r
000
$ts->depth eq 1 and $ts->count eq 3
!ll&&!rl&&r
000
$ts->depth eq 0 and $ts->count eq 5
!ll&&!rl&&r
000
MC/DC0%0/4
$ts->depth eq 1 and $ts->count eq 3 or $ts->depth eq 0 and $ts->count eq 5
$ts->depth eq 1 $ts->count eq 3 $ts->depth eq 0 $ts->count eq 5
Truth table0%0/7
A and B or C and D
A= $ts->depth eq 1B= $ts->count eq 3C= $ts->depth eq 0D= $ts->count eq 5
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
125 $ts->depth eq "0" && $ts->count eq "5" ){
126  
127 0 foreach my $row ($ts->rows) {
128 0 $string = join(' ', @$row);
129 0 $string =~ s/\n//g;
130 0 $string =~ s/\r//g;
131 0 $line .= $string;
132 }
133 }
134 }
135 1 $$self{'_string'} = $line;
136 1 return \$line;
137 }
138  
139 0 ▶sub get_symbol_name(){
Subroutine get_symbol_name: not called CC=2 SCAR=17.9
Pod undocumented
140 0 my ($self) = @_;
141 0 ▶ return $$self{'symbol_name'} if $$self{'symbol_name'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"symbol_name"} 0 0 0
142 0 $self->_set_symbol_name();
143 0 return $$self{'symbol_name'};
144 }
145  
146 0 ▶sub _set_symbol_name(){
Subroutine _set_symbol_name: not called CC=1 SCAR=6.9
147 0 my ($self) = @_;
148 0 $$self{'_string'} =~ m/^.([^\x20]+)/o;
149 0 my $name = $1;
150 0 $name =~ s/\(/ \(/;
151 0 $name =~ s/\)/\) /;
152 0 $$self{'symbol_name'} = $name;
153 }
154  
155 0 ▶sub get_last_trade_price(){
Subroutine get_last_trade_price: not called CC=2 SCAR=17.9
Pod undocumented
156 0 my ($self) = @_;
157 0 ▶ return $$self{'last_trade_price'} if $$self{'last_trade_price'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"last_trade_price"} 0 0 0
158 0 $$self{'_string'} =~ m/(\xbc\xe8\xb0\xfa\xc3\xcd)(\d+:\d+) ([\d\,]+)/o;
159 0 $self->_set_last_trade();
160 0 return $3;
161 }
162  
163 0 ▶sub get_last_trade_time(){
Subroutine get_last_trade_time: not called CC=2 SCAR=17.9
Pod undocumented
164 0 my ($self) = @_;
165 0 ▶ return $$self{'last_trade_time'} if $$self{'last_trade_time'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"last_trade_time"} 0 0 0
166 0 $$self{'_string'} =~ m/(\xbc\xe8\xb0\xfa\xc3\xcd)(\d+:\d+) ([\d\,]+)/o;
167 0 $self->_set_last_trade();
168 0 return $2;
169 }
170  
171 1 ▶sub _set_last_trade(){
Subroutine _set_last_trade: called CC=1 SCAR=0.0
172 1 my ($self) = @_;
173 1 $$self{'_string'} =~ m/(\xbc\xe8\xb0\xfa\xc3\xcd)(\d+:\d+) ([\d\,]+)/o;
174 1 $$self{'last_trade_time'} = $2;
175 1 $$self{'last_trade_price'} = $3;
176 }
177  
178 0 ▶sub get_high_price(){
Subroutine get_high_price: not called CC=2 SCAR=17.9
Pod undocumented
179 0 my ($self) = @_;
180 0 ▶ return $$self{'high_price'} if $$self{'high_price'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"high_price"} 0 0 0
181 0 $self->_set_high_price();
182 0 return $2;
183 }
184  
185 1 ▶sub _set_high_price(){
Subroutine _set_high_price: called CC=1 SCAR=0.0
186 1 my ($self) = @_;
187 1 $$self{'_string'} =~ m/(\xb9\xe2\xc3\xcd)([\s\d\,]+)/o;
188 1 $$self{'high_price'} = $2;
189 }
190  
191 0 ▶sub get_low_price(){
Subroutine get_low_price: not called CC=2 SCAR=17.9
Pod undocumented
192 0 my ($self) = @_;
193 0 ▶ return $$self{'low_price'} if $$self{'low_price'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"low_price"} 0 0 0
194 0 $self->_set_low_price();
195 0 return $2;
196 }
197  
198 1 ▶sub _set_low_price(){
Subroutine _set_low_price: called CC=1 SCAR=0.0
199 1 my ($self) = @_;
200 1 $$self{'_string'} =~ m/(\xb0\xc2\xc3\xcd)([\d\,]+)/o;
201 1 $$self{'low_price'} = $2;
202 }
203  
204 1 ▶sub get_prev_close(){
Subroutine get_prev_close: called CC=2 SCAR=7.0
Pod undocumented
205 1 my ($self) = @_;
206 1 ▶ return $$self{'prev_close'} if $$self{'prev_close'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prev_close"} 0 1 1
207 1 $self->_set_prev_close();
208 1 return $2;
209 }
210  
211 2 ▶sub _set_prev_close(){
Subroutine _set_prev_close: called CC=1 SCAR=0.0
212 2 my ($self) = @_;
213 2 $$self{'_string'} =~ m/(\xbd\xaa\xc3\xcd)([\d\,]+)/o;
214 2 $$self{'prev_close'} = $2;
215 }
216  
217 0 ▶sub get_volume(){
Subroutine get_volume: not called CC=3 SCAR=24.8
Pod undocumented
218 0 my ($self) = @_;
219 0 ▶ return $$self{'volume'} if scalar($$self{'volume'}) || scalar($$self{'volume'}) eq "0";
Branch: 1 branch
BranchTrueFalseTotal
if scalar $self->{"volume"} or scalar $self->{"volume"} eq 0 0 0 0
Condition0%0/3
scalar $self->{"volume"} or scalar $self->{"volume"} eq 0
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar $self->{"volume"} or scalar $self->{"volume"} eq 0
scalar $self->{"volume"} scalar $self->{"volume"} eq 0
Truth table0%0/3
A or B
A= scalar $self->{"volume"}B= scalar $self->{"volume"} eq 0
ABresult
1X1
011
000
220 0 $self->_set_volume();
221 0 return $2;
222 }
223  
224 1 ▶sub _set_volume(){
Subroutine _set_volume: called CC=2 SCAR=7.4
225 1 my ($self) = @_;
226 1 $$self{'_string'} =~ m/(\xbd\xd0\xcd\xe8\xb9\xe2)([\d\,]+)/o;
227 1 ▶ unless($2){
Branch: 1 branch
BranchTrueFalseTotal
if ($2) { } 0 1 1
228 1 $$self{'volume'} = "0";
229 }else{
230 0 $$self{'volume'} = $2;
231 }
232 }
233  
234 0 ▶sub get_change(){
Subroutine get_change: not called CC=3 SCAR=24.8
Pod undocumented
235 0 my ($self) = @_;
236 0 ▶ return $$self{'change'} if scalar($$self{'change'}) || scalar($$self{'change'}) eq "0";
Branch: 1 branch
BranchTrueFalseTotal
if scalar $self->{"change"} or scalar $self->{"change"} eq 0 0 0 0
Condition0%0/3
scalar $self->{"change"} or scalar $self->{"change"} eq 0
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar $self->{"change"} or scalar $self->{"change"} eq 0
scalar $self->{"change"} scalar $self->{"change"} eq 0
Truth table0%0/3
A or B
A= scalar $self->{"change"}B= scalar $self->{"change"} eq 0
ABresult
1X1
011
000
237 0 $self->_set_change();
238 0 return $2;
239 }
240  
241 1 ▶sub _set_change(){
Subroutine _set_change: called CC=2 SCAR=7.4
242 1 my ($self) = @_;
243 1 $$self{'_string'} =~ m/(\xc1\xb0\xc6\xfc\xc8\xe6)([\+\-][\d\,]+)/o;
244 1 ▶ unless($2){
Branch: 1 branch
BranchTrueFalseTotal
if ($2) { } 0 1 1
245 1 $$self{'change'} = "0";
246 }else{
247 0 $$self{'change'} = $2;
248 }
249 }
250  
251 1 1;
252  
253 __END__
↑ Summary