blib/lib/Finance/Quote/IEX.pm

filter: Statementstmt 24.2% 17 / 70 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.5% 5 / 8 Podpod 0.0% 0 / 3 totaltotal 24.7% 22 / 89 CC 6 SCAR 30.0
CC
6
Cov
22%
iex
42.0
methods
2.0
labels
2.0
CRAP 20.1
↑ Summary
1 package Finance::Quote::IEX;
2  
3 # ABSTRACT: Retrieve stock quotes using the IEX API
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTTP::Status qw(status_message);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.001001'; # VERSION
12  
13 sub methods {
14 return (
15 0 ▶ iex => \&iex,
Subroutine methods: not called CC=1 SCAR=6.9
Pod undocumented
16 usa => \&iex,
17 nasdaq => \&iex,
18 nyse => \&iex,
19 );
20 }
21  
22 sub labels {
23 0 ▶ my @labels = qw/
Subroutine labels: not called CC=1 SCAR=6.9
Pod undocumented
24 name
25 last
26 date
27 isodate
28 time
29 net
30 p_change
31 volume
32 close
33 open
34 year_range
35 pe
36 cap
37 exchange
38 method
39 price
40 currency
41 /;
42 return (
43 0 iex => \@labels,
44 usa => \@labels,
45 nasdaq => \@labels,
46 nyse => \@labels,
47 );
48 }
49  
50 sub iex {
51 0 ▶ my $quoter = shift;
Subroutine iex: not called CC=6 SCAR=37.4
Pod undocumented
52 0 my @stocks = @_;
53  
54 0 my $iex_url = 'https://api.iextrading.com/1.0/stock/%s/quote';
55 0 my $errormsg = 'Error retrieving quote for "%s": GET "%s" resulted in'
56 . ' HTTP response %d (%s)';
57  
58 0 my $ua = $quoter->user_agent();
59 0 my %info;
60  
61 0 foreach my $symbol (@stocks) {
62 0 my $url = sprintf( $iex_url, $symbol );
63 0 my $response = $ua->get($url);
64  
65 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
66 0 my $code = $response->code;
67 0 my $desc = status_message($code);
68 0 $info{ $symbol, 'success' } = 0;
69 0 $info{ $symbol, 'errormsg' }
70 = sprintf( $errormsg, $symbol, $url, $code, $desc );
71 0 next;
72 }
73  
74 0 my $data = decode_json( $response->decoded_content );
75  
76 0 ▶ if ( !defined $data->{latestPrice} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data->{"latestPrice"}) 0 0 0
77 0 my $code = $response->code;
78 0 my $desc = status_message($code);
79 0 $info{ $symbol, 'success' } = 0;
80 0 $info{ $symbol, 'errormsg' } = sprintf(
81 'Error retrieving quote for "%s":'
82 . ' no price found in response data',
83 $symbol
84 );
85 0 next;
86 }
87  
88 0 ▶ if ( !defined $data->{latestUpdate} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data->{"latestUpdate"}) 0 0 0
89 0 my $code = $response->code;
90 0 my $desc = status_message($code);
91 0 $info{ $symbol, 'success' } = 0;
92 0 $info{ $symbol, 'errormsg' } = sprintf(
93 'Error retrieving quote for "%s":'
94 . ' no date found in response data',
95 $symbol
96 );
97 0 next;
98 }
99  
100 0 $info{ $symbol, 'success' } = 1;
101 0 $info{ $symbol, 'method' } = 'iex';
102 0 $info{ $symbol, 'source' } = 'Finance::Quote::IEX';
103 0 $info{ $symbol, 'currency' } = 'USD';
104 0 $info{ $symbol, 'symbol' } = $data->{symbol};
105 $info{ $symbol, 'name' }
106 0 = $symbol . ' (' . $data->{companyName} . ')';
107 0 $info{ $symbol, 'last' } = $data->{latestPrice};
108 0 $info{ $symbol, 'price' } = $data->{latestPrice};
109 0 $info{ $symbol, 'net' } = $data->{change};
110 0 $info{ $symbol, 'p_change' } = $data->{changePercent};
111 0 $info{ $symbol, 'volume' } = $data->{latestVolume};
112 0 $info{ $symbol, 'close' } = $data->{close};
113 0 $info{ $symbol, 'open' } = $data->{open};
114 $info{ $symbol, 'year_range' }
115 0 = $data->{week52Low} . ' - ' . $data->{week52High};
116 0 $info{ $symbol, 'pe' } = $data->{peRatio};
117 0 $info{ $symbol, 'cap' } = $data->{marketCap};
118 0 $info{ $symbol, 'exchange' } = $data->{exchange};
119  
120 # The Finance::Quote documentation indicates that the date shouldn't
121 # be parsed, but store_date does not support epoch time.
122 my $dt
123 0 = DateTime->from_epoch( epoch => $data->{latestUpdate} / 1000 );
124 0 $info{ $symbol, 'time' } = $dt->hms;
125 0 $info{ $symbol, 'date' } = $dt->strftime('%m/%d/%y');
126 0 $info{ $symbol, 'isodate' } = $dt->ymd;
127 }
128  
129 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
130 }
131  
132 1 1;
133  
134 __END__
135  
136 =pod
137  
138 =encoding UTF-8
139  
140 =head1 NAME
141  
142 Finance::Quote::IEX - Retrieve stock quotes using the IEX API
143  
144 =head1 VERSION
145  
146 version 0.001001
147  
148 =head1 SYNOPSIS
149  
150 use Finance::Quote;
151 my $q = Finance::Quote->new('IEX');
152 my %info = Finance::Quote->fetch( 'iex', 'AAPL' );
153  
154 =head1 DESCRIPTION
155  
156 This module fetches information from the IEX API.
157  
158 This module is not loaded by default on a Finance::Quote object. It
159 must be loaded explicitly by placing C<'IEX'> in the argument list to
160 C<< Finance::Quote->new() >>.
161  
162 This module provides the C<iex> fetch method.
163  
164 =head1 ATTRIBUTION
165  
166 If you redistribute IEX API data:
167  
168 =over 4
169  
170 =item *
171  
172 Cite IEX using the following text and link: "Data provided for free by
173 L<IEX|https://iextrading.com/developer>."
174  
175 =item *
176  
177 Provide a link to L<https://iextrading.com/api-exhibit-a> in your terms of
178 service.
179  
180 =back
181  
182 Additionally, if you display our TOPS price data, cite
183 "L<IEX Real-Time Price|https://iextrading.com/developer>" near the price.
184  
185 =head1 LABELS RETURNED
186  
187 The following labels may be returned by C<Finance::Quote::IEX>: name, last, date,
188 time, net, p_change, volume, close, open, year_range, pe, cap, exchange, method
189 and price.
190  
191 =head1 SEE ALSO
192  
193 =over 4
194  
195 =item * L<Finance::Quote>
196  
197 =item * L<https://iextrading.com/developer/docs/>
198  
199 =back
200  
201 =head1 AUTHOR
202  
203 Jeffrey T. Palmer <jtpalmer@cpan.org>
204  
205 =head1 COPYRIGHT AND LICENSE
206  
207 This software is Copyright (c) 2017 by Jeffrey T. Palmer.
208  
209 This is free software, licensed under:
210  
211 The MIT (X11) License
212  
213 =cut
↑ Summary