blib/lib/FixerIO/API.pm

filter: Statementstmt 94.8% 37 / 39 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 94.7% 54 / 57 CC 4 SCAR 5.1
CC
4
Cov
93%
api_call
3.0
new
2.0
latest
1.1
CRAP 1.7
↑ Summary
1 package FixerIO::API;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use JSON 'decode_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 FixerIO::API - Access to the fixer.io currency exchange rate API.
13  
14 =head1 VERSION
15  
16 Version 0.1.0
17  
18 =cut
19  
20 2 our $VERSION = '0.1.0';
21  
22  
23 =head1 SYNOPSIS
24  
25 use FixerIO::API;
26  
27 my $access_key = <your-key-here>;
28 my $fixer = FixerIO::API->new( $access_key );
29  
30 # get latest data
31 my $ld = $fixer->latest;
32  
33 use DDP hash_max=>5;
34 p $ld, as=>"Latest Data:";
35  
36 Will print,
37 Latest Data:
38 {
39 success 1 (JSON::PP::Boolean),
40 base "EUR",
41 date "2023-09-03" (dualvar: 2023),
42 timestamp 1693764783,
43 rates {
44 AED 3.965325,
45 AFN 79.575894,
46 ALL 108.330797,
47 AMD 418.325847,
48 ANG 1.954454,
49 (...skipping 165 keys...)
50 }
51 }
52  
53 =head1 DESCRIPTION
54  
55 This is a Perl module for accessing the API provided by fixer.io. See, F<"http://fixer.io/documentation">.
56  
57 This module doesn't export anything. Nor does it keep any data, other than your API access key. Your script will keep or do what it wants with the data.
58  
59 You have to obtain your own API key from the fixer.io web site. There is a free option.
60  
61 =head1 IMPLEMENTED ENDPOINTS
62  
63 Please note that depending on your subscription plan, certain API endpoints may not be available.
64  
65 =head2 LATEST RATES
66  
67 Returns real-time exchange rate data for all available or a specific set of currencies.
68  
69 Specifying symbols is not implemented. Changing the base is not implemented. The etags optimization is not implemented.
70  
71 =head1 EXPORT
72  
73 No exports.
74  
75 =head1 SUBROUTINES/METHODS
76  
77 =head2 new
78  
79 Instantiate a new API access object. Pass in your API access key as an argument.
80  
81 =cut
82  
83 2 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
84 2 my $class = shift;
85 2 my $access_key = shift;
86 2 ▶ return undef unless defined $access_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $access_key 1 1 2
87 1 return bless \$access_key, $class;
88 }
89  
90 =head2 api_call
91  
92 Perform the HTTP(S) request, return the response data.
93  
94 =cut
95  
96 1 ▶sub api_call {
Subroutine api_call: called CC=3 SCAR=11.0
Pod documented
97 1 my ($self, $options, $defaults) = @_;
98 1 my $ua = HTTP::Tiny->new(
99 agent => sprintf '%s/%s ', 'FixerIO-API', $VERSION
100 );
101 my %options = (
102 1 %{$defaults},
103 1 %{$options},
104 );
105 1 my $url = "http://data.fixer.io/api/latest?access_key=$$self";
106 1 ▶ while (my ($k, $v) = each %options) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %options) 1 1 2
107 1 $url .= sprintf '&%s=%s', $k, $v; # values always have no spaces
108 }
109 1 my $resp = $ua->get($url);
110 1 ▶ return $resp->{'content'} if $resp->{'success'};
Branch: 1 branch
BranchTrueFalseTotal
if $resp->{"success"} 0 1 1
111  
112 #TODO: Handle HTTP exceptions
113 1 croak 'HTTP Error!? How can that be?';
114 }
115  
116 =head2 latest
117  
118 Return the latest data.
119  
120 =cut
121  
122 1 ▶sub latest {
Subroutine latest: called CC=1 SCAR=0.6
Pod documented
123 1 my ($self, %options) = @_;
124 1 my $defaults = {format => 1};
125 1 my $json = $self->api_call( \%options, $defaults );
126 0 my $data = decode_json( $json );# could die
127 0 return $data;
128 }
129  
130 =head1 INSTALLATION
131  
132 To install this module, run the following commands:
133  
134 perl Makefile.PL
135 make
136 make test
137 make install
138  
139 =head1 AUTHOR
140  
141 Harry Wozniak, C<< <woznotwoz at cpan.org> >>
142  
143 =head1 BUGS
144  
145 Please report any bugs or feature requests to C<bug-fixerio-api at rt.cpan.org>, or through
146 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=FixerIO-API>. I will be notified, and then you'll
147 automatically be notified of progress on your bug as I make changes.
148  
149 =head1 SUPPORT
150  
151 You can find documentation for this module with the perldoc command.
152  
153 perldoc FixerIO::API
154  
155  
156 You can also look for information at:
157  
158 =over 4
159  
160 =item * RT: CPAN's request tracker (report bugs here)
161  
162 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=FixerIO-API>
163  
164 =item * CPAN Ratings
165  
166 L<https://cpanratings.perl.org/d/FixerIO-API>
167  
168 =item * Search CPAN
169  
170 L<https://metacpan.org/release/FixerIO-API>
171  
172 =back
173  
174  
175 =head1 ACKNOWLEDGEMENTS
176  
177  
178 =head1 LICENSE AND COPYRIGHT
179  
180 This software is copyright (c) 2023 by Harry Wozniak.
181  
182 This is free software; you can redistribute it and/or modify it under
183 the same terms as the Perl 5 programming language system itself.
184  
185 =cut
186  
187 2 'That is all.';
↑ Summary