File Coverage

blib/lib/Finance/Quote/Troweprice.pm
Criterion Covered Total %
statement 17 61 27.8
branch 0 16 0.0
condition n/a
subroutine 7 10 70.0
pod 0 3 0.0
total 24 90 26.6


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2             #
3             # Copyright (C) 1998, Dj Padzensky <djpadz@padz.net>
4             # Copyright (C) 1998, 1999 Linas Vepstas <linas@linas.org>
5             # Copyright (C) 2000, Yannick LE NY <y-le-ny@ifrance.com>
6             # Copyright (C) 2000, Paul Fenwick <pjf@cpan.org>
7             # Copyright (C) 2000, Brent Neal <brentn@users.sourceforge.net>
8             #
9             # This program is free software; you can redistribute it and/or modify
10             # it under the terms of the GNU General Public License as published by
11             # the Free Software Foundation; either version 2 of the License, or
12             # (at your option) any later version.
13             #
14             # This program is distributed in the hope that it will be useful,
15             # but WITHOUT ANY WARRANTY; without even the implied warranty of
16             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17             # GNU General Public License for more details.
18             #
19             # You should have received a copy of the GNU General Public License
20             # along with this program; if not, write to the Free Software
21             # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22             # 02110-1301, USA
23             #
24             #
25             # This code derived from Padzensky's work on package Finance::YahooQuote,
26             # but extends its capabilites to encompas a greater number of data sources.
27             #
28             # This code was developed as part of GnuCash <http://www.gnucash.org/>
29              
30             package Finance::Quote::Troweprice;
31             require 5.005;
32              
33 5     5   3006 use strict;
  5         19  
  5         169  
34              
35 5     5   29 use vars qw( $TROWEPRICE_URL);
  5         12  
  5         216  
36              
37 5     5   30 use LWP::UserAgent;
  5         25  
  5         56  
38 5     5   123 use Time::Piece;
  5         15  
  5         36  
39 5     5   386 use Try::Tiny;
  5         17  
  5         3657  
40              
41             our $VERSION = '1.57_03'; # TRIAL VERSION
42              
43             # URLs of where to obtain information.
44              
45             $TROWEPRICE_URL = ("https://www3.troweprice.com/fb2/ppfweb/downloadPrices.do");
46              
47 5     5 0 29 sub methods { return (troweprice => \&troweprice,
48             troweprice_direct => \&troweprice); }
49              
50             {
51             my @labels = qw/method exchange name nav date isodate price/;
52              
53 5     5 0 20 sub labels { return (troweprice => \@labels,
54             troweprice_direct => \@labels); }
55             }
56              
57             # =======================================================================
58              
59             sub troweprice {
60              
61 0     0 0   my $quoter = shift;
62 0           my @symbols = @_;
63              
64 0 0         return if (! scalar @symbols);
65              
66             # for T Rowe Price, we get them all.
67 0           my %info;
68 0           my $url = $TROWEPRICE_URL;
69 0           my $ua = $quoter->user_agent;
70 0           my $reply = $ua->get( $url, 'Accept-Language' => 'en-US,en' );
71              
72 0 0         if (! $reply->is_success) {
73              
74 0           for my $stock (@symbols) {
75 0           $info{ $stock, "success" } = 0;
76 0           $info{ $stock, "errormsg" } =
77             "Error retrieving quote for $stock. Attempt to fetch the URL
78             $url resulted in HTTP response:i " . $reply->status_line;
79             }
80 0 0         return wantarray() ? %info : \%info;
81             }
82              
83 0           my $quotes;
84              
85 0           my $csv = $reply->content;
86 0           open my $in, '<', \$csv;
87             RECORD:
88 0           while (my $line = <$in>) {
89 0 0         next RECORD if ($line !~ /\S/);
90             #$line =~ s/\s+$//;
91 0           my @q = $quoter->parse_csv($line);
92 0           my $symbol = $q[0];
93             next RECORD
94 0 0         if (! grep {$_ eq $symbol} @symbols);
  0            
95              
96 0           my $date;
97             try {
98 0     0     $date = Time::Piece->strptime($q[2], "%m/%d/%Y");
99             } catch {
100 0     0     $info{ $symbol, "success" } = 0;
101 0           $info{ $symbol, "errormsg" } =
102             "Failed to parse quote date. Please contact developers";
103 0           next RECORD;
104 0           };
105 0           $quotes->{$symbol} = {
106             price => $q[1],
107             date => $date,
108             }
109             }
110              
111             SYMBOL:
112 0           for my $symbol (@symbols) {
113            
114             # skip if already defined due to earlier parsing error
115 0 0         next SYMBOL if (defined $info{ $symbol, 'success' });
116              
117 0 0         if (! defined $quotes->{$symbol}) {
118 0           $info{ $symbol, "success" } = 0;
119 0           $info{ $symbol, "errormsg" } =
120             "Error retrieving quote for $symbol - no listing for this"
121             . " name found. Please check symbol.";
122 0           next SYMBOL;
123             }
124              
125 0           $info{ $symbol, "success" } = 1;
126 0           $info{ $symbol, 'symbol' } = $symbol;
127 0           $info{ $symbol, "exchange" } = "T. Rowe Price";
128 0           $info{ $symbol, "method" } = "troweprice";
129 0           $info{ $symbol, "name" } = $symbol; # no name supplied ...
130 0           $info{ $symbol, "nav" } = $quotes->{$symbol}->{price};
131 0           $info{ $symbol, "price" } = $info{$symbol,"nav"};
132 0           $info{ $symbol, "currency" } = "USD";
133             $quoter->store_date(
134             \%info,
135             $symbol,
136             {isodate => $quotes->{$symbol}->{date}->ymd}
137 0           );
138             }
139              
140 0 0         return wantarray() ? %info : \%info;
141              
142             }
143              
144             1;
145              
146             =head1 NAME
147              
148             Finance::Quote::Troweprice - Obtain quotes from T. Rowe Price
149              
150             =head1 SYNOPSIS
151              
152             use Finance::Quote;
153              
154             $q = Finance::Quote->new;
155              
156             %stockinfo = $q->fetch("troweprice","PRFDX");
157              
158             =head1 DESCRIPTION
159              
160             This module obtains information about managed funds from T. Rowe Price.
161             Information about T. Rowe Price funds is available from a variety of
162             sources. This module fetches information directly from T. Rowe Price.
163              
164             =head1 LABELS RETURNED
165              
166             Information available from T. Rowe Price may include the following
167             labels: exchange, name, nav, date, price.
168              
169             =head1 SEE ALSO
170              
171             T. Rowe Price website - http://www.troweprice.com/
172              
173             =cut