File Coverage

blib/lib/LBMA/Statistics/SilverFixing/Daily.pm
Criterion Covered Total %
statement 55 71 77.4
branch 10 20 50.0
condition n/a
subroutine 13 15 86.6
pod 5 5 100.0
total 83 111 74.7


line stmt bran cond sub pod time code
1             package LBMA::Statistics::SilverFixing::Daily;
2              
3 15     15   195555 use warnings;
  15         30  
  15         467  
4 15     15   78 use strict;
  15         29  
  15         712  
5              
6             our $VERSION = '0.061';
7              
8 15     15   9772 use WWW::Mechanize;
  15         1447233  
  15         611  
9 15     15   10268 use HTML::TableExtract;
  15         77281  
  15         132  
10 15     15   612 use Encode;
  15         35  
  15         1637  
11 15     15   11758 use Log::Log4perl qw/:easy/;
  15         491388  
  15         118  
12              
13             =head1 NAME
14              
15             LBMA::Statistics::SilverFixing::Daily - Daily Prices Silver Fixings London Bullion Market (Internal only)
16              
17             =head1 DESCRIPTION
18              
19             Does the hard work.
20              
21              
22             =head1 SYNOPSIS
23              
24              
25             This modul is for internal use only. There's no need to use it directly.
26              
27             Everthing is done by LBMA::Statistics (See L).
28              
29             =head2 new - Constructor
30              
31             use strict;
32              
33             use warnings;
34              
35             use LBMA::Statistics::SilverFixing::Daily;
36              
37             my $lbma = LBMA::Statistics::GoldFixing::Daily->new(
38             year => $year,
39             day_pattern => $day_pattern
40             ) or die $!;
41              
42             =cut
43              
44             sub new {
45 9     9 1 3719 my $class = shift;
46 9         29 my $self = {};
47 9         34 bless $self, $class;
48 9         46 $self->_init(@_);
49 6         19 return $self;
50             }
51              
52             =head2 _init
53              
54             private method to initialize the object
55              
56             =cut
57              
58             sub _init {
59 9     9   17 my $self = shift;
60 9         44 my %args = @_;
61 9         59 $self->{year} = $args{year};
62 9         25 $self->{day_pattern} = $args{day_pattern};
63 9 100       46 LOGDIE "Missing mandantory parameter year" unless $self->{year};
64 7 100       43 LOGDIE "Missing mandantory parameter day_pattern"
65             unless $self->{day_pattern};
66              
67             }
68              
69             =head2 year
70              
71             returns the year to look for
72              
73             =cut
74              
75             sub year {
76 1     1 1 6 my $self = shift;
77 1         10 return $self->{year};
78             }
79              
80             =head2 day_pattern
81              
82             returns the day_pattern to look for
83              
84             =cut
85              
86             sub day_pattern {
87 3     3 1 13 my $self = shift;
88 3         14 return $self->{day_pattern};
89             }
90              
91             =head2 dailystatsurl
92              
93             determines url for daily silverstats
94              
95             =cut
96              
97             sub dailystatsurl {
98 0     0 1 0 my $self = shift;
99 0         0 my $url = 'http://lbma.oblive.co.uk/table?metal=silver&year=';
100 0         0 $url .= $self->year();
101 0         0 $url .= '&type=daily';
102 0         0 DEBUG("url: $url");
103 0         0 return $url;
104             }
105              
106             =head2 retrieve_row
107              
108             Returns an array of fixings
109             The number and order of elements varies depending on the year data is retrieved.
110             There is no EUR before 1999.
111              
112             # @fixings 1999 --
113             # 0 date
114             # 1 SILVER USD
115             # 2 SILVER GBP
116             # 3 SILVER EUR
117             #
118             # @fixings 1968 -- 1998
119             # 0 date
120             # 1 SILVER USD
121             # 2 SILVER GBP
122              
123             =cut
124              
125             sub retrieve_row {
126 0     0 1 0 my $self = shift;
127 0         0 my $url = $self->dailystatsurl();
128              
129 0 0       0 my $browser = WWW::Mechanize->new(
130             stack_depth => 0,
131             autocheck => 1,
132             ) or LOGDIE $!;
133              
134 0         0 $browser->agent_alias('Windows IE 6'); # Hide crawler
135              
136 0 0       0 $browser->get($url) or LOGDIE $!;
137              
138 0         0 my $fixings = $self->_parse( $browser->content() );
139             {
140 15     15   15130 no warnings;
  15         41  
  15         3195  
  0         0  
141 0         0 foreach my $fixing ( @$fixings ) {
142 0         0 TRACE("Fixing: $fixing");
143             }
144             }
145 0 0       0 return wantarray ? @$fixings : $fixings;
146             }
147              
148             =head2 _parse
149              
150             parses the content of the retrieved HTML page
151              
152             =cut
153              
154             sub _parse {
155 2     2   512 my $self = shift;
156 2         26 my $content = shift @_;
157 2         17 $content = decode_utf8($content);
158 2         2457 my $day_pattern = $self->day_pattern();
159 2         6 my @fixings = ();
160 2 50       19 my $te = HTML::TableExtract->new() or LOGDIE $!;
161 2 50       308 $te->parse($content) or LOGDIE $!;
162 2         319223 TABLE: foreach my $ts ( $te->tables ) {
163 2         26 ROW: foreach my $row ( $ts->rows ) {
164 274 50       30857 next ROW unless defined @$row[0];
165             {
166 15     15   102 no warnings;
  15         37  
  15         1997  
  274         293  
167 274         858 TRACE( "Current Row: ", join( "|", @$row ) );
168             }
169 274 100       2241 next ROW unless @$row[0] =~ m/$day_pattern/;
170 2         6 @fixings = @$row;
171 2         8 last TABLE;
172             }
173             }
174 2 50       801 return wantarray ? @fixings : \@fixings;
175             }
176              
177             1;
178             __END__