File Coverage

blib/lib/Finance/Quote/CurrencyRates/Fixer.pm
Criterion Covered Total %
statement 22 39 56.4
branch 1 12 8.3
condition 2 18 11.1
subroutine 7 8 87.5
pod 0 3 0.0
total 32 80 40.0


line stmt bran cond sub pod time code
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     1   520 use strict;
  1         3  
  1         29  
21 1     1   5 use warnings;
  1         3  
  1         34  
22              
23 1     1   5 use constant DEBUG => $ENV{DEBUG};
  1         3  
  1         60  
24 1     1   6 use if DEBUG, 'Smart::Comments';
  1         3  
  1         6  
25              
26 1     1   31 use JSON;
  1         2  
  1         17  
27              
28             our $VERSION = '1.58_01'; # TRIAL VERSION
29              
30             sub parameters {
31 1     1 0 3 return ('API_KEY');
32             }
33              
34             sub new
35             {
36 1     1 0 2 my $self = shift;
37 1   33     7 my $class = ref($self) || $self;
38              
39 1         3 my $this = {};
40 1         3 bless $this, $class;
41              
42 1         2 my $args = shift;
43              
44             ### Fixer->new args : $args
45              
46 1 50 33     11 return unless (ref $args eq 'HASH') and (exists $args->{API_KEY});
47              
48 0           $this->{API_KEY} = $args->{API_KEY};
49 0           $this->{refresh} = 0;
50 0 0         $this->{refresh} = not $args->{cache} if exists $args->{cache};
51              
52 0           return $this;
53             }
54              
55             sub multipliers
56             {
57 0     0 0   my ($this, $ua, $from, $to) = @_;
58              
59 0 0 0       if ($this->{refresh} or not exists $this->{cache}) {
60 0           my $url = "http://data.fixer.io/api/latest?access_key=$this->{API_KEY}";
61 0           my $reply = $ua->get($url);
62              
63 0 0         return unless ($reply->code == 200);
64              
65 0           my $body = $reply->content;
66              
67 0           my $json_data = JSON::decode_json $body;
68 0 0 0       if ( !$json_data || $json_data->{error} || not exists $json_data->{rates}) {
      0        
69             ### fixer error : $json_data->{error}->{info}
70 0           return;
71             }
72              
73 0           $this->{cache} = $json_data->{rates};
74             ### Fixer rates: $this->{cache}
75             }
76              
77              
78 0 0 0       if (exists $this->{cache}->{$from} and exists $this->{cache}->{$to}) {
79             ### from : $from, $this->{cache}->{$from}
80             ### to : $to, $this->{cache}->{$to}
81 0           return ($this->{cache}->{$from}, $this->{cache}->{$to});
82             }
83              
84             ### At least one code not found: $from, $to
85              
86 0           return;
87             }
88              
89             1;
90              
91             =head1 NAME
92              
93             Finance::Quote::CurrencyRates::Fixer - Obtain currency rates from https://fixer.io
94              
95             =head1 SYNOPSIS
96              
97             use Finance::Quote;
98            
99             $q = Finance::Quote->new(currency_rates => {order => ['Fixer'],
100             fixer => {API_KEY => ...}});
101              
102             $value = $q->currency('18.99 EUR', 'USD');
103              
104             =head1 DESCRIPTION
105              
106             This module fetches currency rates from https://fixer.io and provides data to
107             Finance::Quote to convert the first argument to the equivalent value in the
108             currency indicated by the second argument.
109              
110             This module caches the currency rates for the lifetime of the quoter object,
111             unless 'cache => 0' is included in the 'fixer' options hash.
112              
113             =head1 API_KEY
114              
115             https://fixer.io requires users to register and obtain an API key.
116              
117             The API key must be set by providing a 'fixer' hash inside the 'currency_rates'
118             hash to Finance::Quote->new as in the above example.
119              
120             =head1 Terms & Conditions
121              
122             Use of https://fixer.io is governed by any terms & conditions of that site.
123              
124             Finance::Quote is released under the GNU General Public License, version 2,
125             which explicitly carries a "No Warranty" clause.
126              
127             =cut