Quote/GoldMoney.pm

filter: Statementstmt 26.2% 26 / 99 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 70.0% 7 / 10 Podpod 0.0% 0 / 4 totaltotal 21.5% 33 / 153 CC 21 SCAR 53.3
CC
21
Cov
19%
goldmoney
342.0
_goldmoney_time
12.0
_goldmoney_error
6.0
CRAP 207.0
« Quote/BSEIndia.pm ↑ Summary Quote/Finnhub.pm »
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 noai expandtab ic showmode showmatch:
3 #
4 # Copyright (C) 1998, Dj Padzensky <djpadz@padz.net>
5 # Copyright (C) 1998, 1999 Linas Vepstas <linas@linas.org>
6 # Copyright (C) 2000, Yannick LE NY <y-le-ny@ifrance.com>
7 # Copyright (C) 2000, Paul Fenwick <pjf@cpan.org>
8 # Copyright (C) 2000, Brent Neal <brentn@users.sourceforge.net>
9 # Copyright (C) 2000, Volker Stuerzl <volker.stuerzl@gmx.de>
10 # Copyright (C) 2006, Klaus Dahlke <klaus.dahlke@gmx.de>
11 # Copyright (C) 2008, Stephan Ebelt <stephan.ebelt@gmx.de>
12 #
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301, USA
27 #
28 # $Id: $
29  
30 package Finance::Quote::GoldMoney;
31 5 require 5.005;
32  
33 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 5 our $VERSION = '1.72'; # VERSION
40  
41 5 our $DISPLAY = 'GoldMoney - Gold, Silver, Platinum ';
42 5 our @LABELS = qw/exchange name date isodate price method/;
43 5 our $METHODHASH = {subroutine => \&goldmoney,
44 display => $DISPLAY,
45 labels => \@LABELS};
46  
47 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
48 return (
49 10 goldmoney => $METHODHASH,
50 );
51 }
52  
53 5 ▶sub labels {
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
54 5 my %m = methodinfo();
55 5 return map {$_ => [@{$m{$_}{labels}}] } keys %m;
56 }
57  
58 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
59 5 my %m = methodinfo();
60 5 return map {$_ => $m{$_}{subroutine} } keys %m;
61 }
62  
63 # goldmoney($quoter, @symbols)
64 #
65 # - get 'gold' and 'silver' spot rates from goldmoney.com
66 # - error out properly (that is: ignore) all other symbols
67 #
68 0 ▶sub goldmoney {
Subroutine goldmoney: not called CC=18 SCAR=58.3
Pod undocumented
69 0 my $quoter = shift;
70 0 my @symbols = @_;
71 0 ▶ return unless @symbols;
Branch: 1 branch
BranchTrueFalseTotal
unless @symbols 0 0 0
72  
73 0 my $ua = $quoter->user_agent;
74  
75 # Set the ua to be blank. GoldMOney are using CloudFlare who block
76 # the default useragent.
77 0 $ua->agent('');
78 0 my ( %symbolhash, @q, %info );
79 0 my ( $html_string, $te, $table_gold, $table_silver,
80 $table_platinum, $gold_gg, $gold_oz, $silver_oz,
81 $platinum_oz, $platinum_pg, $currency
82 );
83  
84 0 my $_want_gold = 0;
85 0 my $_want_silver = 0;
86 0 my $_want_platinum = 0;
87  
88 # - feed all requested symbols into %info (to be returned later)
89 # - set error state to false by default
90 # - see if a gold or silver rate is requested
91 0 foreach my $s (@symbols) {
92 0 $info{ $s, 'success' } = 0;
93 0 $info{ $s, 'exchange' } = 'goldmoney.com';
94 0 $info{ $s, 'method' } = 'goldmoney';
95 0 $info{ $s, 'symbol' } = $s;
96  
97 0 ▶ if ( $s eq 'gold' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($s eq 'gold') { } 0 0 0
elsif ($s eq 'silver') { } 0 0 0
elsif ($s eq 'platinum') { } 0 0 0
98 0 $_want_gold = 1;
99 }
100 elsif ( $s eq 'silver' ) {
101 0 $_want_silver = 1;
102 }
103 elsif ( $s eq 'platinum' ) {
104 0 $_want_platinum = 1;
105 }
106 else {
107 0 $info{ $s, 'errormsg' } =
108 "No data returned (note: this module only works for 'gold' and 'silver')";
109 }
110 }
111  
112 # get the JSON of the prices. Currently getting sell price,
113 0 ▶ if ( $_want_gold or $_want_silver or $_want_platinum ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_want_gold or $_want_silver or $_want_platinum) 0 0 0
Condition0%0/6
$_want_gold or $_want_silver or $_want_platinum
l!l&&r!l&&!r
000
$_want_gold or $_want_silver
l!l&&r!l&&!r
000
MC/DC0%0/3
$_want_gold or $_want_silver or $_want_platinum
$_want_gold $_want_silver $_want_platinum
Truth table0%0/4
A or B or C
A= $_want_goldB= $_want_silverC= $_want_platinum
ABCresult
1XX1
01X1
0011
0000
114  
115 0 ▶ my $currency = $quoter->{"currency"} || 'EUR';
Condition0%0/2
$quoter->{'currency'} || 'EUR'
l!l
00
MC/DC0%0/1
$quoter->{'currency'} || 'EUR'
$quoter->{'currency'}
Truth table0%0/2
A
A= $quoter->{'currency'}
Aresult
11
00
116 0 my $GOLDMONEY_URL =
117 "http://www.goldmoney.com/metal/prices/currentSpotPrices?currency="
118 . lc($currency)
119 . "&units=grams&price=bid";
120 0 my $response = $ua->request( GET $GOLDMONEY_URL);
121  
122 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
123 0 $html_string = $response->content;
124  
125 0 my $json = from_json($html_string);
126  
127 0 $table_gold = $json->{spotPrices}[0];
128 0 $table_silver = $json->{spotPrices}[1];
129 0 $table_platinum = $json->{spotPrices}[2];
130 }
131 else {
132 # retrieval error - flag an error and return right away
133 0 foreach my $s (@symbols) {
134 0 %info = _goldmoney_error( @symbols,
135 'HTTP error: ' . $response->status_line );
136 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
137 }
138 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
139 }
140  
141 # get gold rate
142 #
143 0 ▶ if ($_want_gold) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_want_gold) 0 0 0
144  
145 # assemble final dataset
146 # - take "now" as date/time as the site is always current and does
147 # not provide this explicitly - so there is a time-slip
148 0 $quoter->store_date( \%info, 'gold',
149 { isodate => _goldmoney_time('isodate') } );
150  
151 0 $info{ 'gold', 'time' } = _goldmoney_time('time');
152 0 $info{ 'gold', 'name' } = 'Gold Spot';
153 0 $info{ 'gold', 'last' } = $table_gold->{spotPrice};
154 0 $info{ 'gold', 'price' } = $table_gold->{spotPrice};
155 0 $info{ 'gold', 'currency' } = $currency;
156 0 $info{ 'gold', 'success' } = 1;
157 }
158  
159 # get silver rate
160 #
161 0 ▶ if ($_want_silver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_want_silver) 0 0 0
162  
163 0 $quoter->store_date( \%info, 'silver',
164 { isodate => _goldmoney_time('isodate') } );
165 0 $info{ 'silver', 'time' } = _goldmoney_time('time');
166 0 $info{ 'silver', 'name' } = 'Silver Spot';
167 0 $info{ 'silver', 'last' } = $table_silver->{spotPrice};
168 0 $info{ 'silver', 'price' } = $table_silver->{spotPrice};
169 0 $info{ 'silver', 'currency' } = $currency;
170 0 $info{ 'silver', 'success' } = 1;
171  
172 }
173  
174 # get platinum rate
175 #
176 0 ▶ if ($_want_platinum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_want_platinum) 0 0 0
177  
178 # assemble final dataset
179 # - take "now" as date/time as the site is always current and does
180 # not provide this explicitly - so there is a time-slip
181 0 $quoter->store_date( \%info, 'platinum',
182 { isodate => _goldmoney_time('isodate') } );
183 0 $info{ 'platinum', 'time' } = _goldmoney_time('time');
184 0 $info{ 'platinum', 'name' } = 'Platinum Spot';
185 0 $info{ 'platinum', 'last' } = $table_platinum->{spotPrice};
186 0 $info{ 'platinum', 'price' } = $table_platinum->{spotPrice};
187 0 $info{ 'platinum', 'currency' } = $currency;
188 0 $info{ 'platinum', 'success' } = 1;
189  
190 }
191 }
192  
193 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
194 }
195  
196 # - populate %info with errormsg and status code set for all requested symbols
197 # - return a hash ready to pass back to fetch()
198 0 ▶sub _goldmoney_error {
Subroutine _goldmoney_error: not called CC=2 SCAR=17.9
199 0 my @symbols = shift;
200 0 my $msg = shift;
201 0 my %info;
202  
203 0 foreach my $s (@symbols) {
204 0 $info{ $s, "success" } = 0;
205 0 $info{ $s, "errormsg" } = $msg;
206 }
207  
208 0 return (%info);
209 }
210  
211 # - return current 'isodate' and 'time' string
212 0 ▶sub _goldmoney_time {
Subroutine _goldmoney_time: not called CC=3 SCAR=24.8
213 0 my $want = shift;
214 0 my @now = localtime();
215 0 my $str;
216  
217 0 ▶ if ( $want eq 'isodate' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($want eq 'isodate') { } 0 0 0
elsif ($want eq 'time') { } 0 0 0
218 0 $str = sprintf( '%4d-%02d-%02d', $now[5] + 1900, $now[4] + 1, $now[3] );
219 }
220 elsif ( $want eq 'time' ) {
221 0 $str = sprintf( '%02d:%02d:%02d', $now[2], $now[1], $now[0] );
222 }
223  
224 0 return ($str);
225 }
226  
227 5 1;
228  
229 =head1 NAME
230  
231 Finance::Quote::GoldMoney - obtain spot rates from GoldMoney.
232  
233 =head1 SYNOPSIS
234  
235 use Finance::Quote;
236  
237 $q = Finance::Quote->new;
238  
239 %rates = $q->fetch('goldmoeny','gold', 'silver', 'platinum');
240  
241 =head1 DESCRIPTION
242  
243 This module obtains current spot rates for 'gold', 'silver' and
244 'platinum' from Goldmoney (http://www.goldmoney.com). All other
245 symbols are ignored.
246  
247 Information returned by this module is governed by Net Transactions
248 Ltd.'s terms and conditions. This module is *not* affiliated with the
249 company in any way. Use at your own risk.
250  
251 =head1 LABELS RETURNED
252  
253 The following labels are returned by Finance::Quote::GoldMoney:
254  
255 - exchange
256 - name
257 - date, time
258 - price (per gram),
259 - currency
260  
261 =head1 SEE ALSO
262  
263 GoldMoney (Net Transactions Ltd.), http://www.goldmoney.com/
264  
265 =cut
« Quote/BSEIndia.pm ↑ Summary Quote/Finnhub.pm »