| 1 | #!/usr/bin/perl -w | ||||||||||||||||||||||||||||
| 2 | # vi: set ts=2 sw=2 noai ic showmode showmatch: | ||||||||||||||||||||||||||||
| 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 2 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, write to the Free Software | ||||||||||||||||||||||||||||
| 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||||||||||||||||||||||||||||
| 17 | # 02110-1301, USA | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | package Finance::Quote::CurrencyRates::UniRate; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use constant DEBUG => $ENV{DEBUG}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use if DEBUG, 'Smart::Comments'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | our $VERSION = '1.72'; # VERSION | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | sub parameters { | ||||||||||||||||||||||||||
|
Subroutine
parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 1 | return ('API_KEY'); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub new | ||||||||||||||||||||||||||||
| 36 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=15.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 38 | 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
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | my $this = {}; | |||||||||||||||||||||||||||
| 41 | 1 | bless $this, $class; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | my $args = shift; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | ### UniRate->new args : $args | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # UniRate supports an environment variable for the API key for parity | ||||||||||||||||||||||||||||
| 48 | # with the Twelvedata and FinanceAPI currency-rate modules. An explicit | ||||||||||||||||||||||||||||
| 49 | # API_KEY in $args overrides the environment variable. | ||||||||||||||||||||||||||||
| 50 | 1 | $this->{API_KEY} = $ENV{'UNIRATEAPI_API_KEY'}; | |||||||||||||||||||||||||||
| 51 | 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"}
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | return $this; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub multipliers | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
multipliers: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 0 | my ($this, $ua, $from, $to) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | my $url = 'https://api.unirateapi.com/api/rates' | ||||||||||||||||||||||||||||
| 61 | . '?from=' . ${from} | ||||||||||||||||||||||||||||
| 62 | . '&to=' . ${to} | ||||||||||||||||||||||||||||
| 63 | 0 | . '&api_key=' . $this->{API_KEY}; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | my $reply = $ua->get($url, | |||||||||||||||||||||||||||
| 66 | 'Accept' => 'application/json', | ||||||||||||||||||||||||||||
| 67 | 'User-Agent' => 'finance-quote-unirate/0.01'); | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | return unless ($reply->code == 200); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | my $body = $reply->content; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | ### HTTP body: $body | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | my $json_data = eval { JSON::decode_json $body }; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | return if $@ or !$json_data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$@ or not $json_data
MC/DC0%0/2
$@ or not $json_data
$@
not $json_data
Truth table0%0/3
A or not(B)
A= $@B= not $json_data
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | return unless exists $json_data->{'rate'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $rate = $json_data->{'rate'}; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | ### Rate from JSON: $rate | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | return unless $rate + 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # For small rates, request the inverse. | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if ($rate < 0.001) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | ### Rate is too small, requesting inverse : $rate | ||||||||||||||||||||||||||||
| 89 | 0 | my ($a, $b) = $this->multipliers($ua, $to, $from); | |||||||||||||||||||||||||||
| 90 | 0 | return ($b, $a); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | return (1.0, $rate); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 1 | 1; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||||