File Coverage

blib/lib/Finance/Quote/CurrencyRates/OpenExchange.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::OpenExchange;
19              
20 1     1   522 use strict;
  1         2  
  1         36  
21 1     1   16 use warnings;
  1         2  
  1         34  
22              
23 1     1   8 use constant DEBUG => $ENV{DEBUG};
  1         2  
  1         66  
24 1     1   6 use if DEBUG, 'Smart::Comments';
  1         2  
  1         6  
25              
26 1     1   30 use JSON;
  1         2  
  1         16  
27              
28             our $VERSION = '1.58'; # VERSION
29              
30             sub parameters {
31 1     1 0 4 return ('API_KEY');
32             }
33              
34             sub new
35             {
36 1     1 0 3 my $self = shift;
37 1   33     10 my $class = ref($self) || $self;
38              
39 1         2 my $this = {};
40 1         3 bless $this, $class;
41              
42 1         4 my $args = shift;
43              
44             ### OpenExchange->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 = "https://openexchangerates.org/api/latest.json?app_id=$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             ### OpenExchange error : $json_data->{description}
70 0           return;
71             }
72              
73 0           $this->{cache} = $json_data->{rates};
74             ### OpenExchange 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::OpenExchange - Obtain currency rates from
94             https://openexchangerates.org
95              
96             =head1 SYNOPSIS
97              
98             use Finance::Quote;
99            
100             $q = Finance::Quote->new(currency_rates => {order => ['OpenExchange'],
101             openexchange => {API_KEY => ...}});
102              
103             $value = $q->currency('18.99 EUR', 'USD');
104              
105             =head1 DESCRIPTION
106              
107             This module fetches currency rates from https://openexchangerates.org and
108             provides data to Finance::Quote to convert the first argument to the equivalent
109             value in the currency indicated by the second argument.
110              
111             This module caches the currency rates for the lifetime of the quoter object,
112             unless 'cache => 0' is included in the 'openexchange' options hash.
113              
114             =head1 API_KEY
115              
116             https://openexchangerates.org requires users to register and obtain an API key.
117              
118             The API key must be set by providing a 'openexchange' hash inside the
119             'currency_rates' hash to Finance::Quote->new as in the above example.
120              
121             =head1 Terms & Conditions
122              
123             Use of https://openexchangerates.org is governed by any terms & conditions of
124             that site.
125              
126             Finance::Quote is released under the GNU General Public License, version 2,
127             which explicitly carries a "No Warranty" clause.
128              
129             =cut