blib/lib/Finance/Quote/IEX.pm

filter: Statementstmt 25.3% 18 / 71 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.5% 5 / 8 Podpod 0.0% 0 / 3 totaltotal 25.5% 23 / 90 CC 6 SCAR 30.0
CC
6
Cov
23%
iex
42.0
methods
2.0
labels
2.0
CRAP 20.1
↑ Summary
1 package Finance::Quote::IEX;
2  
3 # ABSTRACT: (DEPRECATED) 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 warnings::warnif( 'deprecated',
12 'Finance::Quote::IEX is deprecated and should no longer be used' );
13  
14 1 our $VERSION = '0.002000'; # VERSION
15  
16 0 ▶sub methods {
Subroutine methods: not called CC=1 SCAR=6.9
Pod undocumented
17 return (
18 0 iex => \&iex,
19 usa => \&iex,
20 nasdaq => \&iex,
21 nyse => \&iex,
22 );
23 }
24  
25 0 ▶sub labels {
Subroutine labels: not called CC=1 SCAR=6.9
Pod undocumented
26 0 my @labels = qw/
27 name
28 last
29 date
30 isodate
31 time
32 net
33 p_change
34 volume
35 close
36 open
37 year_range
38 pe
39 cap
40 exchange
41 method
42 price
43 currency
44 /;
45 return (
46 0 iex => \@labels,
47 usa => \@labels,
48 nasdaq => \@labels,
49 nyse => \@labels,
50 );
51 }
52  
53 0 ▶sub iex {
Subroutine iex: not called CC=6 SCAR=37.4
Pod undocumented
54 0 my $quoter = shift;
55 0 my @stocks = @_;
56  
57 0 my $iex_url = 'https://api.iextrading.com/1.0/stock/%s/quote';
58 0 my $errormsg = 'Error retrieving quote for "%s": GET "%s" resulted in'
59 . ' HTTP response %d (%s)';
60  
61 0 my $ua = $quoter->user_agent();
62 0 my %info;
63  
64 0 foreach my $symbol (@stocks) {
65 0 my $url = sprintf( $iex_url, $symbol );
66 0 my $response = $ua->get($url);
67  
68 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
69 0 my $code = $response->code;
70 0 my $desc = status_message($code);
71 0 $info{ $symbol, 'success' } = 0;
72 0 $info{ $symbol, 'errormsg' }
73 = sprintf( $errormsg, $symbol, $url, $code, $desc );
74 0 next;
75 }
76  
77 0 my $data = decode_json( $response->decoded_content );
78  
79 0 ▶ if ( !defined $data->{latestPrice} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data->{"latestPrice"}) 0 0 0
80 0 my $code = $response->code;
81 0 my $desc = status_message($code);
82 0 $info{ $symbol, 'success' } = 0;
83 0 $info{ $symbol, 'errormsg' } = sprintf(
84 'Error retrieving quote for "%s":'
85 . ' no price found in response data',
86 $symbol
87 );
88 0 next;
89 }
90  
91 0 ▶ if ( !defined $data->{latestUpdate} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data->{"latestUpdate"}) 0 0 0
92 0 my $code = $response->code;
93 0 my $desc = status_message($code);
94 0 $info{ $symbol, 'success' } = 0;
95 0 $info{ $symbol, 'errormsg' } = sprintf(
96 'Error retrieving quote for "%s":'
97 . ' no date found in response data',
98 $symbol
99 );
100 0 next;
101 }
102  
103 0 $info{ $symbol, 'success' } = 1;
104 0 $info{ $symbol, 'method' } = 'iex';
105 0 $info{ $symbol, 'source' } = 'Finance::Quote::IEX';
106 0 $info{ $symbol, 'currency' } = 'USD';
107 0 $info{ $symbol, 'symbol' } = $data->{symbol};
108 $info{ $symbol, 'name' }
109 0 = $symbol . ' (' . $data->{companyName} . ')';
110 0 $info{ $symbol, 'last' } = $data->{latestPrice};
111 0 $info{ $symbol, 'price' } = $data->{latestPrice};
112 0 $info{ $symbol, 'net' } = $data->{change};
113 0 $info{ $symbol, 'p_change' } = $data->{changePercent};
114 0 $info{ $symbol, 'volume' } = $data->{latestVolume};
115 0 $info{ $symbol, 'close' } = $data->{close};
116 0 $info{ $symbol, 'open' } = $data->{open};
117 $info{ $symbol, 'year_range' }
118 0 = $data->{week52Low} . ' - ' . $data->{week52High};
119 0 $info{ $symbol, 'pe' } = $data->{peRatio};
120 0 $info{ $symbol, 'cap' } = $data->{marketCap};
121 0 $info{ $symbol, 'exchange' } = $data->{exchange};
122  
123 # The Finance::Quote documentation indicates that the date shouldn't
124 # be parsed, but store_date does not support epoch time.
125 my $dt
126 0 = DateTime->from_epoch( epoch => $data->{latestUpdate} / 1000 );
127 0 $info{ $symbol, 'time' } = $dt->hms;
128 0 $info{ $symbol, 'date' } = $dt->strftime('%m/%d/%y');
129 0 $info{ $symbol, 'isodate' } = $dt->ymd;
130 }
131  
132 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
133 }
134  
135 1 1;
136  
137 __END__
↑ Summary