blib/lib/Finance/Currency/Convert/DnB.pm

filter: Statementstmt 53.0% 26 / 49 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 70.0% 7 / 10 Podpod 100% 3 / 3 totaltotal 38.7% 36 / 93 CC 15 SCAR 40.4
CC
15
Cov
34%
currency
110.0
update_currency
42.0
currencies
2.0
CRAP 56.7
↑ Summary
1 package Finance::Currency::Convert::DnB;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our @ISA = qw/Exporter/;
6 2 our @EXPORT = qw/currency update_currency currencies/;
7 2 our $VERSION = '0.2';
8  
9 2 our $currency;
10 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub update_currency {
Subroutine update_currency: not called CC=6 SCAR=37.4
Pod documented
16 0 ▶ my $filename = File::Spec->tmpdir() . "/currency_list_" . ((defined $>) ? $> : "") . ".xml";
Branch: 1 branch
BranchTrueFalseTotal
defined $> ? : 0 0 0
17 #only download XML twice a day
18 0 ▶ if (!-e $filename || time()-43200 < -M $filename || $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $filename or time - 43200 < -M $filename or $_[0]) 0 0 0
Condition0%0/6
not -e $filename or time - 43200 < -M $filename or $_[0]
l!l&&r!l&&!r
000
not -e $filename or time - 43200 < -M $filename
l!l&&r!l&&!r
000
MC/DC0%0/3
not -e $filename or time - 43200 < -M $filename or $_[0]
not -e $filename time - 43200 < -M $filename $_[0]
Truth table0%0/4
not(A) or B or C
A= not -e $filenameB= time - 43200 < -M $filenameC= $_[0]
ABCresult
1XX1
01X1
0011
0000
19 0 ▶ is_success ($_=getstore('http://www.dnbnor.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml', $filename))
Branch: 1 branch
BranchTrueFalseTotal
&is_success($_ = &getstore("http://www.dnbnor.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml", $filename)) or die 'Failed to get list of currencies; http error code: ' . $_ 0 0 0
20 or die 'Failed to get list of currencies; http error code: ' . $_;
21  
22 }
23  
24 0 my $content = slurp $filename;
25 0 $currency = XMLin($content, KeyAttr => ["kode"]);
26 }
27  
28 0 ▶sub currencies {
Subroutine currencies: not called CC=1 SCAR=6.9
Pod documented
29 0 update_currency;
30 0 sort keys %{$currency->{valutakurs}}
31 }
32  
33 0 ▶sub currency {
Subroutine currency: not called CC=10 SCAR=47.0
Pod documented
34 0 my ($amount, $from, $to, $decimals) = @_;
35 0 ▶ $decimals = 2 unless defined $decimals;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decimals 0 0 0
36 0 update_currency;
37  
38 map {
39 0 my $res;
40 0 ▶ my $from_currency = $currency->{valutakurs}->{$from}->{overforsel}->{midtkurs} / $currency->{valutakurs}->{$from}->{enhet} if ($from ne "NOK");
Branch: 1 branch
BranchTrueFalseTotal
if $from ne "NOK" 0 0 0
41 0 ▶ my $to_currency = $currency->{valutakurs}->{$_}->{overforsel}->{midtkurs} / $currency->{valutakurs}->{$_}->{enhet} if ($_ ne "NOK");
Branch: 1 branch
BranchTrueFalseTotal
if $_ ne "NOK" 0 0 0
42
43 0 ▶ foreach my $amount ( (ref($amount) eq 'ARRAY') ? @$amount : $amount ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $amount eq 'ARRAY' ? : 0 0 0
44 0 ▶ if ($_ eq "NOK") {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq 'NOK') { } 0 0 0
elsif ($from eq 'NOK') { } 0 0 0
45 0 $res += $amount * $from_currency;
46 }
47 elsif ($from eq "NOK") {
48 0 $res += $amount / $to_currency;
49 }
50 else {
51 0 $res += $amount * $from_currency / $to_currency;
52 }
53 }
54 0 $res = sprintf('%.' . $decimals . 'f', $res);
55 0 ▶ ($to) ? return $res : $_ => $res
Branch: 2 branches
BranchTrueFalseTotal
$to ? : 0 0 0
$to ? : 0 0 0
56 0 } ($to) ? $to : keys %{$currency->{valutakurs}}
57 }
58  
59 2 1;
60  
61 =head1 NAME
62  
63 Finance::Currency::Convert::DnB - convert currencies with up to date currencies from dnbnor.no
64  
65 =head1 SYNOPSIS
66  
67 use Finance::Currency::Convert::DnB;
68
69 #get results with default number of decimals which is 2
70 $result = currency 20, "NOK", "GBP";
71 #3 decimals
72 $result = currency 20, "NOK", "GBP", 3;
73  
74 #convert several numbers
75 $result = currency \@values, "NOK", "GBP";
76 $result = currency [20, 50, 35], "NOK", "GBP";
77  
78 #store all results in a hash
79 my %all_currencies = currency 20, "NOK";
80 print "20 NOK in $_ is $all_currencies{$_}\n" foreach (keys %all_currencies);
81  
82 #get a list of available currencies
83 my @currencies = currencies;
84  
85 =head1 DESCRIPTION
86  
87 Finance::Currency::Convert::DnB uses a XML list from dnbnor.no to convert currencies. Caches XML list in a temporary file for quick access.
88  
89 =head1 AVAILABLE METHODS
90  
91 =head2 currency
92  
93 $result = convert 20, "NOK", "GBP", 2;
94 Amount can also be an array reference, and it will return a total of all elements.
95 If conversion currency is excluded, it will return a hash with all results for all currencies.
96 If number of decimals is excluded, if will default to 2 decimals.
97  
98 =head2 currencies
99  
100 Returns a list of available currencies sorted alphabetically.
101  
102 =head2 update_currency
103  
104 This is an internal function called automatically to update currencies. It is done automatically if the cache is non-existing or if it is older than 12 hours. You can force updating by calling it with a true argument.
105  
106 =cut
↑ Summary