File Coverage

blib/lib/Finance/Quote/MorningstarJP.pm
Criterion Covered Total %
statement 17 47 36.1
branch 0 6 0.0
condition 0 6 0.0
subroutine 7 8 87.5
pod 0 3 0.0
total 24 70 34.2


line stmt bran cond sub pod time code
1             #
2             # Copyright (C) 2012, Christopher Hill
3             #
4             # This program is free software: you can redistribute it and/or modify
5             # it under the terms of the GNU General Public License as published by
6             # the Free Software Foundation, either version 3 of the License, or
7             # (at your option) any later version.
8             #
9             # This program is distributed in the hope that it will be useful,
10             # but WITHOUT ANY WARRANTY; without even the implied warranty of
11             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12             # GNU General Public License for more details.
13             #
14             # You should have received a copy of the GNU General Public License
15             # along with this program. If not, see <http://www.gnu.org/licenses/>.
16             #
17             # $Id: $
18             #
19              
20             package Finance::Quote::MorningstarJP;
21             require 5.006;
22              
23 5     5   2769 use strict;
  5         16  
  5         166  
24 5     5   32 use warnings;
  5         28  
  5         166  
25 5     5   31 use base 'Exporter';
  5         10  
  5         514  
26 5     5   44 use DateTime;
  5         11  
  5         42  
27              
28 5     5   213 use vars qw( $MORNINGSTAR_JP_URL);
  5         21  
  5         2673  
29              
30             our @EXPORT_OK = qw(morningstarjp methods labels);
31             our $VERSION = '1.58_01'; # TRIAL VERSION
32              
33             # NAV information (basis price)
34             $MORNINGSTAR_JP_URL =
35             ('https://www.wealthadvisor.co.jp/FundData/DownloadStdYmd.do?fnc=');
36              
37 5     5 0 29 sub methods { return ( morningstarjp => \&morningstarjp ); }
38 5     5 0 20 sub labels { return ( morningstarjp => [qw/symbol date nav/] ); }
39              
40             sub morningstarjp
41             {
42 0     0 0   my $quoter = shift;
43 0           my @symbols = @_;
44              
45             my (
46 0           $ua, $response, %info, $date, $nav, $year,
47             $month, $day, $fmyear, $fmmonth, $fmday, @data
48             );
49              
50 0           $ua = $quoter->user_agent;
51              
52             # calculate beginning and end date
53             # Starting from 10 days prior (to cover any recent holiday gaps)
54              
55 0           my $calcDay = DateTime->now();
56 0           $year = $calcDay->year();
57 0           $month = $calcDay->month();
58 0           $day = $calcDay->day();
59 0           $calcDay->subtract( days => 10 );
60 0           $fmyear = $calcDay->year();
61 0           $fmmonth = $calcDay->month();
62 0           $fmday = $calcDay->day();
63              
64             # Iterate over each symbol as site only permits query by single security
65 0           foreach my $symbol (@symbols)
66             {
67             # Query the server via a POST request
68 0           $response = $ua->post(
69             $MORNINGSTAR_JP_URL . $symbol,
70             [
71             selectStdYearFrom => $fmyear,
72             selectStdMonthFrom => $fmmonth,
73             selectStdDayFrom => $fmday,
74             selectStdYearTo => $year,
75             selectStdMonthTo => $month,
76             selectStdDayTo => $day,
77             base => '0' # 0 is daily, 1 is week ends (Friday), 2 is month ends only
78             ],
79             );
80              
81             # Check response, CSV data is in an octet-stream
82 0 0 0       if ( $response->is_success
    0 0        
83             && $response->content_type eq 'application/octet-stream' )
84             {
85              
86             # Parse...
87             # First row (in Shift-JIS) is fixed. It means "date","basis price"
88             # 日付,基準価額
89             # Subsequent rows are in ascending chronological order
90             # date(yyyymmdd),nav
91             #
92             # Split the data on CRLF or LF boundaries
93 0           @data = split( '\015?\012', $response->content );
94              
95             # We only care about the final row as that has the most recent data
96 0           ( $date, $nav ) = $quoter->parse_csv( $data[-1] );
97              
98             # Store the retrieved data into the hash
99 0           ( $year, $month, $day ) = ( $date =~ m/(\d{4})(\d{2})(\d{2})/ );
100 0           $quoter->store_date( \%info, $symbol,
101             { year => $year, month => $month, day => $day } );
102 0           $info{ $symbol, 'currency' } = 'JPY';
103 0           $info{ $symbol, 'method' } = 'MorningstarJP';
104 0           $info{ $symbol, 'name' } = $symbol;
105 0           $info{ $symbol, 'nav' } = $nav;
106 0           $info{ $symbol, 'success' } = 1;
107 0           $info{ $symbol, 'symbol' } = $symbol;
108             } elsif ( $response->is_success
109             && $response->content_type eq 'text/html' )
110             {
111              
112             # HTML response that means POST was invalid and/or rejected.
113 0           $info{ $symbol, 'errormsg' } = 'Invalid search criteria';
114 0           $info{ $symbol, 'success' } = 0;
115             } else
116             {
117              
118             # Unknown error encountered.
119 0           $info{ $symbol, 'errormsg' } = 'Search unavailable';
120 0           $info{ $symbol, 'success' } = 0;
121             }
122             }
123              
124 0 0         return wantarray() ? %info : \%info;
125             }
126              
127             1;
128              
129             __END__
130              
131             =head1 NAME
132              
133             Finance::Quote::MorningstarJP - Obtain price data from Morningstar (Japan).
134              
135             =head1 VERSION
136              
137             This documentation describes version 1.00 of MorningstarJP.pm, October 13, 2012.
138              
139             =head1 SYNOPSIS
140              
141             use Finance::Quote;
142              
143             $q = Finance::Quote->new;
144              
145             %info = $q->fetch("morningstarjp", "2009100101");
146              
147             =head1 DESCRIPTION
148              
149             This module obtains information from Morningstar (Japan),
150             L<http://www.wealthadvisor.co.jp/>.
151              
152             Information returned by this module is governed by Morningstar
153             (Japan)'s terms and conditions.
154              
155             =head1 FUND SYMBOLS
156              
157             Use the numeric symbol shown in the URL on the "SnapShot" page
158             of the security of interest.
159              
160             e.g. For L<http://www.wealthadvisor.co.jp/FundData/SnapShot.do?fnc=2009100101>,
161             one would supply 2009100101 as the symbol argument on the fetch API call.
162              
163             =head1 LABELS RETURNED
164              
165             The following labels may be returned by Finance::Quote::MorningstarJP:
166             symbol, date, nav.
167              
168             =head1 REQUIREMENTS
169              
170             Perl 5.006
171             Date/Calc.pm
172             Exporter.pm (included with Perl)
173              
174             =head1 ACKNOWLEDGEMENTS
175              
176             Inspired by other modules already present with Finance::Quote
177              
178             =head1 AUTHOR
179              
180             Christopher Hill
181              
182             =head1 LICENSE AND COPYRIGHT
183              
184             Copyright (C) 2012, Christopher Hill.
185              
186             This program is free software: you can redistribute it and/or modify
187             it under the terms of the GNU General Public License as published by
188             the Free Software Foundation, either version 3 of the License, or
189             (at your option) any later version.
190              
191             =head1 DISCLAIMER
192              
193             This program is distributed in the hope that it will be useful,
194             but WITHOUT ANY WARRANTY; without even the implied warranty of
195             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
196             GNU General Public License for more details.
197              
198             =head1 SEE ALSO
199              
200             Morningstar (Japan), L<http://www.wealthadvisor.co.jp/>
201              
202             =cut