| 1 | #!/usr/bin/perl -w | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # (at your option) any later version. | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # GNU General Public License for more details. | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # along with this program; if not, write to the Free Software | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # 02110-1301, USA | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | package Finance::Quote::CurrencyRates::Fixer; | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use constant DEBUG => $ENV{DEBUG}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use if DEBUG, 'Smart::Comments'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | our $VERSION = '1.72_01'; # TRIAL VERSION | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | sub parameters { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | return ('API_KEY'); | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | sub new | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=20.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | my $this = {}; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | bless $this, $class; | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | my $args = shift; | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | ### Fixer->new args : $args | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | $this->{API_KEY} = $ENV{'FIXER_API_KEY'}; | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | ▶ | $this->{API_KEY} = $args->{API_KEY} if (ref $args eq 'HASH') and (exists $args->{API_KEY}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $args eq "HASH" and exists $args->{"API_KEY"}
MC/DC0%0/2
ref $args eq "HASH" and exists $args->{"API_KEY"}
ref $args eq "HASH"
exists $args->{"API_KEY"}
Truth table33%1/3
A and B
A= ref $args eq "HASH"B= exists $args->{"API_KEY"}
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | $this->{refresh} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | $this->{refresh} = not $args->{cache} if exists $args->{cache}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | # Return nothing if API_KEY not set | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | return unless ($this->{API_KEY}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | ### API_KEY: $this->{API_KEY} | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | return $this; | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | sub multipliers | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
multipliers: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | my ($this, $ua, $from, $to) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if ($this->{refresh} or not exists $this->{cache}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$this->{"refresh"} or not exists $this->{"cache"}
MC/DC0%0/2
$this->{"refresh"} or not exists $this->{"cache"}
$this->{"refresh"}
not exists $this->{"cache"}
Truth table0%0/3
A or not(B)
A= $this->{"refresh"}B= not exists $this->{"cache"}
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my $url = "http://data.fixer.io/api/latest?access_key=$this->{API_KEY}"; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my $reply = $ua->get($url); | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | return unless ($reply->code == 200); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my $body = $reply->content; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my $json_data = JSON::decode_json $body; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ( !$json_data || $json_data->{error} || not exists $json_data->{rates}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not $json_data or $json_data->{"error"} or not exists $json_data->{"rates"}
not $json_data or $json_data->{"error"}
MC/DC0%0/3
not $json_data or $json_data->{"error"} or not exists $json_data->{"rates"}
not $json_data
$json_data->{"error"}
not exists $json_data->{"rates"}
Truth table0%0/4
not(A) or B or not(C)
A= not $json_dataB= $json_data->{"error"}C= not exists $json_data->{"rates"}
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | ### fixer error : $json_data->{error}->{info} | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $this->{cache} = $json_data->{rates}; | |||||||||||||||||||||||||||||||||||||||||
| 79 | ### Fixer rates: $this->{cache} | ||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if (exists $this->{cache}->{$from} and exists $this->{cache}->{$to}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $this->{"cache"}{$from} and exists $this->{"cache"}{$to}
MC/DC0%0/2
exists $this->{"cache"}{$from} and exists $this->{"cache"}{$to}
exists $this->{"cache"}{$from}
exists $this->{"cache"}{$to}
Truth table0%0/3
A and B
A= exists $this->{"cache"}{$from}B= exists $this->{"cache"}{$to}
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | ### from : $from, $this->{cache}->{$from} | ||||||||||||||||||||||||||||||||||||||||||
| 85 | ### to : $to, $this->{cache}->{$to} | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | return ($this->{cache}->{$from}, $this->{cache}->{$to}); | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | ### At least one code not found: $from, $to | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | Finance::Quote::CurrencyRates::Fixer - Obtain currency rates from https://fixer.io | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | use Finance::Quote; | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | $q = Finance::Quote->new(currency_rates => {order => ['Fixer'], | ||||||||||||||||||||||||||||||||||||||||||
| 105 | fixer => {API_KEY => ...}}); | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | $value = $q->currency('18.99 EUR', 'USD'); | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | This module fetches currency rates from https://fixer.io and provides data to | ||||||||||||||||||||||||||||||||||||||||||
| 112 | Finance::Quote to convert the first argument to the equivalent value in the | ||||||||||||||||||||||||||||||||||||||||||
| 113 | currency indicated by the second argument. | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | This module caches the currency rates for the lifetime of the quoter object, | ||||||||||||||||||||||||||||||||||||||||||
| 116 | unless 'cache => 0' is included in the 'fixer' options hash. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | =head1 API_KEY | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | https://fixer.io requires users to register and obtain an API key. | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | The API key can be set by setting the Environment variable "FIXER_API_KEY" or providing a 'fixer' hash inside the 'currency_rates' | ||||||||||||||||||||||||||||||||||||||||||
| 123 | hash to Finance::Quote->new as in the above example. | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | =head1 Terms & Conditions | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | Use of https://fixer.io is governed by any terms & conditions of that site. | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | Finance::Quote is released under the GNU General Public License, version 2, | ||||||||||||||||||||||||||||||||||||||||||
| 130 | which explicitly carries a "No Warranty" clause. | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||||||||||||||||