Debian Codegraph @

lib/Finance/Quote/CurrencyRates/Fixer.pm

raw
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 use strict;
21 use warnings;
22
23 use constant DEBUG => $ENV{DEBUG};
24 use if DEBUG, 'Smart::Comments';
25
26 use JSON;
27
28 our $VERSION = '1.71'; # VERSION
29
30 sub parameters {
31 return ('API_KEY');
32 }
33
34 sub new
35 {
36 my $self = shift;
37 my $class = ref($self) || $self;
38
39 my $this = {};
40 bless $this, $class;
41
42 my $args = shift;
43
44 ### Fixer->new args : $args
45
46 $this->{API_KEY} = $ENV{'FIXER_API_KEY'};
47
48 $this->{API_KEY} = $args->{API_KEY} if (ref $args eq 'HASH') and (exists $args->{API_KEY});
49 $this->{refresh} = 0;
50 $this->{refresh} = not $args->{cache} if exists $args->{cache};
51
52 # Return nothing if API_KEY not set
53 return unless ($this->{API_KEY});
54
55 ### API_KEY: $this->{API_KEY}
56
57 return $this;
58 }
59
60 sub multipliers
61 {
62 my ($this, $ua, $from, $to) = @_;
63
64 if ($this->{refresh} or not exists $this->{cache}) {
65 my $url = "http://data.fixer.io/api/latest?access_key=$this->{API_KEY}";
66 my $reply = $ua->get($url);
67
68 return unless ($reply->code == 200);
69
70 my $body = $reply->content;
71
72 my $json_data = JSON::decode_json $body;
73 if ( !$json_data || $json_data->{error} || not exists $json_data->{rates}) {
74 ### fixer error : $json_data->{error}->{info}
75 return;
76 }
77
78 $this->{cache} = $json_data->{rates};
79 ### Fixer rates: $this->{cache}
80 }
81
82
83 if (exists $this->{cache}->{$from} and exists $this->{cache}->{$to}) {
84 ### from : $from, $this->{cache}->{$from}
85 ### to : $to, $this->{cache}->{$to}
86 return ($this->{cache}->{$from}, $this->{cache}->{$to});
87 }
88
89 ### At least one code not found: $from, $to
90
91 return;
92 }
93
94 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