File Coverage

blib/lib/Finance/Quote/Fidelity.pm
Criterion Covered Total %
statement 20 52 38.4
branch 0 26 0.0
condition n/a
subroutine 8 9 88.8
pod 0 3 0.0
total 28 90 31.1


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             # Copyright (C) 2020, Caleb Begly <calebbegly@gmail.com>
9             #
10             # This program is free software; you can redistribute it and/or modify
11             # it under the terms of the GNU General Public License as published by
12             # the Free Software Foundation; either version 2 of the License, or
13             # (at your option) any later version.
14             #
15             # This program is distributed in the hope that it will be useful,
16             # but WITHOUT ANY WARRANTY; without even the implied warranty of
17             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18             # GNU General Public License for more details.
19             #
20             # You should have received a copy of the GNU General Public License
21             # along with this program; if not, write to the Free Software
22             # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23             # 02110-1301, USA
24             #
25             #
26             # This code derived from Padzensky's work on package Finance::YahooQuote,
27             # but extends its capabilites to encompas a greater number of data sources.
28             #
29             # This code was developed as part of GnuCash <http://www.gnucash.org/>
30              
31             package Finance::Quote::Fidelity;
32             require 5.005;
33              
34 5     5   2724 use strict;
  5         14  
  5         169  
35 5     5   38 use vars qw/$FIDELITY_URL /;
  5         13  
  5         219  
36              
37 5     5   41 use LWP::UserAgent;
  5         10  
  5         30  
38 5     5   105 use HTTP::Request::Common;
  5         10  
  5         398  
39              
40 5     5   35 use constant DEBUG => $ENV{DEBUG};
  5         10  
  5         381  
41 5     5   34 use if DEBUG, 'Smart::Comments';
  5         13  
  5         26  
42              
43             our $VERSION = '1.57_03'; # TRIAL VERSION
44              
45             $FIDELITY_URL = ("https://fundresearch.fidelity.com/mutual-funds/fidelity-funds-daily-pricing-yields/download");
46              
47 5     5 0 24 sub methods {return (fidelity => \&fidelity,
48             fidelity_direct => \&fidelity);}
49              
50             {
51             my @labels = qw/exchange name number nav change ask
52             date isodate yield price method/;
53              
54 5     5 0 19 sub labels { return (fidelity => \@labels,
55             fidelity_direct => \@labels); }
56             }
57              
58             # =======================================================================
59             # the fidelity routine gets quotes from fidelity investments
60             #
61             sub fidelity
62             {
63 0     0 0   my $quoter = shift;
64 0           my @symbols = @_;
65 0 0         return unless @symbols;
66 0           my(%aa, @q, $sym, $k, $ua, $reply);
67              
68             # Build a small hash of symbols people want, because it provides a
69             # quick and easy way to only return desired symbols.
70              
71 0           my %symbolhash;
72 0           %symbolhash = map{$_, 1} @symbols;
  0            
73              
74             # the fidelity pages are comma-separated-values (csv's)
75             # Grab the page with all funds listed
76 0           $ua = $quoter->user_agent;
77 0           $reply = $ua->request(GET $FIDELITY_URL);
78 0 0         if ($reply->is_success) {
79 0           foreach (split('\015?\012',$reply->content)) {
80 0 0         my @q = $quoter->parse_csv($_) or next;
81            
82 0 0         $sym = $q[1] or next;
83 0           $sym =~ s/^ +//;
84              
85             # Skip symbols we didn't ask for.
86 0 0         next unless (defined($symbolhash{$sym}));
87              
88             ### q : @q
89              
90 0           $aa {$sym, "exchange"} = "Fidelity"; # Fidelity
91 0           $aa {$sym, "method"} = "fidelity_direct";
92 0           ($aa {$sym, "name"} = $q[0]) =~ s/^\s+//;
93 0           $aa {$sym, "name"} =~ s/\s+$//;
94 0           $aa {$sym, "symbol"} = $sym;
95 0 0         ($aa {$sym, "nav"} = $q[2]) =~ s/^\s+// if defined($q[2]);
96 0 0         ($aa {$sym, "div"} = $q[3]) =~ s/^\s+// if defined($q[3]);
97 0 0         ($aa {$sym, "p_change"} = $q[5]) =~ s/^\s+// if defined($q[5]);
98 0 0         ($aa {$sym, "yield"} = $q[6]) =~ s/^\s+// if defined($q[6]);
99 0 0         ($aa {$sym, "yield"} = $q[7]) =~ s/^\s+// if defined($q[7]);
100 0 0         ($aa {$sym, "yield"} = $q[8]) =~ s/^\s+// if defined($q[8]);
101 0 0         $aa {$sym, "price"} = $aa{$sym, "nav"} if defined($q[2]);
102 0           $aa {$sym, "success"} = 1;
103 0           $aa {$sym, "currency"} = "USD";
104 0           $quoter->store_date(\%aa, $sym, {usdate => $q[12]});
105             }
106             }
107              
108 0 0         return %aa if wantarray;
109 0           return \%aa;
110             }
111              
112              
113             1;
114              
115             =head1 NAME
116              
117             Finance::Quote::Fidelity - Obtain information from Fidelity Investments.
118              
119             =head1 SYNOPSIS
120              
121             use Finance::Quote;
122              
123             $q = Finance::Quote->new;
124              
125             %info = Finance::Quote->fetch("fidelity","FBGRX");
126             %info = Finance::Quote->fetch("fidelity_direct","FBGRX");
127              
128             =head1 DESCRIPTION
129              
130             This module obtains information from Fidelity Investments,
131             http://www.fidelity.com/. This module is loaded by default on
132             the Finance::Quote object. It is also possible to load this
133             module explicitly by passing "Fidelity" as one of
134             Finance::Quote->new()'s parameters.
135              
136             The "fidelity" fetch method may make use of failover modules.
137             The "fidelity_direct" method will only obtain information
138             directly from Fidelity.
139              
140             Information returned by this module is governed by Fidelity
141             Investment's terms and conditions.
142              
143             =head1 LABELS RETURNED
144              
145             The following labels may be returned by Finance::Quote::Fidelity:
146             exchange, name, nav, p_change, date, yield, price.
147              
148             =head1 SEE ALSO
149              
150             Fidelity Investments, http://www.fidelity.com/
151              
152             =cut