blib/lib/Gas/Prices.pm

filter: Statementstmt 19.1% 17 / 89 Branchbran 0.0% 0 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 45.4% 5 / 11 Podpod 100% 6 / 6 totaltotal 17.9% 28 / 156 CC 28 SCAR 39.1
CC
28
Cov
12%
new
72.0
is_greater
72.0
is_less
72.0
CRAP 49.9
↑ Summary
1 package Gas::Prices;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use version; our $VERSION = qv('0.0.4');
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Other recommended modules (uncomment to use):
10 # use IO::Prompt;
11 # use Perl6::Export;
12 # use Perl6::Slurp;
13 # use Perl6::Say;
14 2 ▶use HTTP::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Module implementation here
17  
18 sub new
19 0 ▶{
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
20 0 my $class = shift;
21 0 my $self = {};
22  
23 0 bless $self;
24 0 $self->{zip} = shift;
25  
26 0 my $http = new HTTP::Lite;
27 my $req = $http->request(
28 "http://autos.msn.com/everyday/GasStations.aspx?m=1&l=1&zip=" .
29 0 $self->{zip});
30 0 my $body = $http->body();
31 0 $body =~ s/\n/ /g;
32 0 $body =~ s/\r/ /g;
33 0 $body =~ s/\cM/ /g;
34  
35 0 $self->{units} = undef;
36
37 0 ▶ if($body =~ /<table id="tblDetail".*?<\/tr>(.*?)<\/table>/)
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ m[<table id="tblDetail".*?</tr>(.*?)</table>]) 0 0 0
38 {
39 0 my $data = $1;
40 0 $data =~ s/^\s+//;
41 0 $data =~ s/\s+$//;
42
43 0 my @units = ();
44
45 0 ▶ while ($data =~ /<tr>(.*?)<\/tr>/g)
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ m[<tr>(.*?)</tr>]g) 0 0 0
46 {
47 0 my $unit = $1;
48 0 ▶ if($unit =~
Branch: 1 branch
BranchTrueFalseTotal
if ($unit =~ m[<span class="text-subheadBold">(.*?)</span>.*?<span class="text-captionPlain">(.*?)</span>.*?<span class="text-captionPlain">(.*?)</span>.*?<span class="text-subheadBold">(.*?)</span>.*?<span class="text-captionPlain">(.*?)</span>.*?<span class="text-subheadBold">(.*?)</span>.*?<span class="text-captionPlain">(.*?)</span>.*?<span class="text-subheadBold">(.*?)</span>.*?<span class="text-captionPlain">(.*?)</span>.*?<span class="text-subheadBold">(.*?)</span>.*?<span class="text-captionPlain">(.*?)</span>]) 0 0 0
49 /<span class="text-subheadBold">(.*?)<\/span>.*?<span class="text-captionPlain">(.*?)<\/span>.*?<span class="text-captionPlain">(.*?)<\/span>.*?<span class="text-subheadBold">(.*?)<\/span>.*?<span class="text-captionPlain">(.*?)<\/span>.*?<span class="text-subheadBold">(.*?)<\/span>.*?<span class="text-captionPlain">(.*?)<\/span>.*?<span class="text-subheadBold">(.*?)<\/span>.*?<span class="text-captionPlain">(.*?)<\/span>.*?<span class="text-subheadBold">(.*?)<\/span>.*?<span class="text-captionPlain">(.*?)<\/span>/)
50 {
51 0 my $station_info =
52 {
53 station_name => $1,
54 station_address => "$2, $3",
55 unleaded_price => $4,
56 unleaded_date => $5,
57 plus_price => $6,
58 plus_date => $7,
59 premium_price => $8,
60 premium_date => $9,
61 diesel_price => $10,
62 diesel_date => $11
63 };
64
65
66 $station_info->{unleaded_price} = $1
67 if ($station_info->{unleaded_price} =~
68 0 ▶ /\$(.*)/);
Branch: 1 branch
BranchTrueFalseTotal
if $station_info->{"unleaded_price"} =~ /\$(.*)/ 0 0 0
69
70 $station_info->{plus_price} = $1
71 if ($station_info->{plus_price} =~
72 0 ▶ /\$(.*)/);
Branch: 1 branch
BranchTrueFalseTotal
if $station_info->{"plus_price"} =~ /\$(.*)/ 0 0 0
73
74 $station_info->{premium_price} = $1
75 if ($station_info->{premium_price} =~
76 0 ▶ /\$(.*)/);
Branch: 1 branch
BranchTrueFalseTotal
if $station_info->{"premium_price"} =~ /\$(.*)/ 0 0 0
77  
78 $station_info->{diesel_price} = $1
79 if ($station_info->{diesel_price} =~
80 0 ▶ /\$(.*)/);
Branch: 1 branch
BranchTrueFalseTotal
if $station_info->{"diesel_price"} =~ /\$(.*)/ 0 0 0
81
82 0 push @units, $station_info;
83 }
84 }
85
86 0 $self->{units} = \@units;
87  
88 #use Data::Dumper;
89 #print Dumper @units;
90
91 0 return $self;
92 }
93 0 return undef;
94 }
95  
96 sub get_stations
97 0 ▶{
Subroutine get_stations: not called CC=1 SCAR=6.9
Pod documented
98 0 my $self = shift;
99 0 return $self->{units};
100 }
101  
102 sub get_cheapest_station
103 0 ▶{
Subroutine get_cheapest_station: not called CC=4 SCAR=30.0
Pod documented
104 0 my $self = shift;
105 0 my $type = shift;
106
107 0 my @units = @{$self->{units}};
108  
109 0 my $lowest_price = undef;
110 0 my $cheapest_station = undef;
111
112  
113 0 foreach(@units)
114 {
115 0 ▶ $cheapest_station = $_ if(!$cheapest_station);
Branch: 1 branch
BranchTrueFalseTotal
unless $cheapest_station 0 0 0
116 0 ▶ if($self->is_less( $_->{$type . "_price"},
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_less($$_{$type . '_price'}, $$cheapest_station{$type . '_price'})) 0 0 0
117 $cheapest_station->{$type . "_price"}))
118 {
119 #print "Replacing " . $cheapest_station->{$type . "_price"} . " with " . $_->{$type . "_price"} . "\n";
120 0 $cheapest_station = $_;
121 }
122 #print $_->{$type . "_price"} . "\n";
123 0 $lowest_price = $cheapest_station->{$type . "_price"};
124 }
125  
126 0 return $cheapest_station;
127 }
128  
129 sub get_most_expensive_station
130 0 ▶{
Subroutine get_most_expensive_station: not called CC=4 SCAR=30.0
Pod documented
131 0 my $self = shift;
132 0 my $type = shift;
133
134 0 my @units = @{$self->{units}};
135
136 0 my $highest_price = undef;
137 0 my $most_expensive_station = undef;
138
139
140 0 foreach(@units)
141 {
142 0 ▶ $most_expensive_station = $_ if(!$most_expensive_station);
Branch: 1 branch
BranchTrueFalseTotal
unless $most_expensive_station 0 0 0
143 0 ▶ if($self->is_greater($_->{$type . "_price"},
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_greater($$_{$type . '_price'}, $$most_expensive_station{$type . '_price'})) 0 0 0
144 $most_expensive_station->{$type . "_price"}))
145 {
146 0 $most_expensive_station = $_;
147 }
148
149 0 $highest_price = $most_expensive_station->{$type . "_price"};
150 }
151
152 0 return $most_expensive_station;
153  
154 }
155  
156 sub is_greater
157 0 ▶{
Subroutine is_greater: not called CC=8 SCAR=42.8
Pod documented
158 0 my ($self, $first, $second) = @_;
159  
160 0 ▶ $first = $1 if($first =~ /\$(.*)/);
Branch: 1 branch
BranchTrueFalseTotal
if $first =~ /\$(.*)/ 0 0 0
161 0 ▶ $second = $1 if($second =~ /\$(.*)/);
Branch: 1 branch
BranchTrueFalseTotal
if $second =~ /\$(.*)/ 0 0 0
162
163 #print "Comparing $first with $second\n";
164
165 0 ▶ return 0 if($first =~ /N\/A/);
Branch: 1 branch
BranchTrueFalseTotal
if $first =~ m[N/A] 0 0 0
166 0 ▶ return 0 if($first =~/nbsp/);
Branch: 1 branch
BranchTrueFalseTotal
if $first =~ /nbsp/ 0 0 0
167  
168 0 ▶ return 1 if($second =~ /N\/A/);
Branch: 1 branch
BranchTrueFalseTotal
if $second =~ m[N/A] 0 0 0
169 0 ▶ return 1 if($second =~/nbsp/);
Branch: 1 branch
BranchTrueFalseTotal
if $second =~ /nbsp/ 0 0 0
170  
171 0 ▶ return 1 if($first > $second);
Branch: 1 branch
BranchTrueFalseTotal
if $first > $second 0 0 0
172 0 return 0;
173 }
174  
175 sub is_less
176 0 ▶{
Subroutine is_less: not called CC=8 SCAR=42.8
Pod documented
177 0 my ($self, $first, $second) = @_;
178
179 0 ▶ $first = $1 if($first =~ /\$(.*)/);
Branch: 1 branch
BranchTrueFalseTotal
if $first =~ /\$(.*)/ 0 0 0
180 0 ▶ $second = $1 if($second =~ /\$(.*)/);
Branch: 1 branch
BranchTrueFalseTotal
if $second =~ /\$(.*)/ 0 0 0
181
182 #print "Comparing $first with $second\n";
183
184 0 ▶ return 0 if($first =~ /N\/A/);
Branch: 1 branch
BranchTrueFalseTotal
if $first =~ m[N/A] 0 0 0
185 0 ▶ return 0 if($first =~/nbsp/);
Branch: 1 branch
BranchTrueFalseTotal
if $first =~ /nbsp/ 0 0 0
186
187 0 ▶ return 1 if($second =~ /N\/A/);
Branch: 1 branch
BranchTrueFalseTotal
if $second =~ m[N/A] 0 0 0
188 0 ▶ return 1 if($second =~/nbsp/);
Branch: 1 branch
BranchTrueFalseTotal
if $second =~ /nbsp/ 0 0 0
189
190 0 ▶ return 1 if($first < $second);
Branch: 1 branch
BranchTrueFalseTotal
if $first < $second 0 0 0
191 0 return 0;
192 }
193
194 2 1; # Magic true value required at end of module
195 __END__
↑ Summary