File Coverage

blib/lib/Finance/Quote/CSE.pm
Criterion Covered Total %
statement 23 48 47.9
branch 0 6 0.0
condition n/a
subroutine 9 10 90.0
pod 0 3 0.0
total 32 67 47.7


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::CSE;
19              
20 5     5   2814 use strict;
  5         12  
  5         159  
21 5     5   28 use warnings;
  5         7  
  5         172  
22              
23 5     5   30 use constant DEBUG => $ENV{DEBUG};
  5         9  
  5         314  
24 5     5   29 use if DEBUG, 'Smart::Comments';
  5         10  
  5         25  
25              
26 5     5   155 use LWP::UserAgent;
  5         14  
  5         60  
27 5     5   115 use JSON qw( decode_json );
  5         12  
  5         25  
28 5     5   501 use String::Util qw(trim);
  5         9  
  5         2293  
29              
30             our $VERSION = '1.58'; # VERSION
31              
32             our @labels = qw/last date isodate/;
33              
34             sub labels {
35 5     5 0 13 return ( cse => \@labels );
36             }
37              
38             sub methods {
39 5     5 0 25 return ( cse => \&cse );
40             }
41              
42             sub cse {
43 0     0 0   my $quoter = shift;
44 0           my @symbols = @_;
45 0           my $ua = $quoter->user_agent();
46 0           my %info;
47              
48 0           foreach my $symbol (@_) {
49 0           eval {
50 0           my $url = 'https://www.cse.lk/api/companyInfoSummery';
51 0           my $form = {'symbol' => $symbol};
52 0           my $reply = $ua->post($url, $form);
53 0           my $search = JSON::decode_json $reply->content;
54              
55             ### Search : $url, $form, $reply->code
56             ### Search : $search
57              
58 0 0         my $data = $search->{reqSymbolInfo} or die('query did not return expected data');
59              
60 0           $info{$symbol, 'isin'} = $data->{isin};
61 0           $info{$symbol, 'close'} = $data->{closingPrice};
62 0           $info{$symbol, 'last'} = $data->{lastTradedPrice};
63 0           $info{$symbol, 'high'} = $data->{hiTrade};
64 0           $info{$symbol, 'low'} = $data->{lowTrade};
65 0           $info{$symbol, 'cap'} = $data->{marketCap};
66 0           $info{$symbol, 'name'} = $data->{name};
67 0           $info{$symbol, 'currency'} = 'LKR';
68 0           $info{$symbol, 'success'} = 1;
69             };
70            
71 0 0         if ($@) {
72 0           my $error = "CSE failed: $@";
73 0           $info{$symbol, 'success'} = 0;
74 0           $info{$symbol, 'errormsg'} = trim($error);
75             }
76             }
77              
78 0 0         return wantarray() ? %info : \%info;
79             }
80              
81             1;
82              
83             =head1 NAME
84              
85             Finance::Quote::CSE - Obtain quotes from Colombo Stock Exchange in Sri Lanka
86              
87             =head1 SYNOPSIS
88              
89             use Finance::Quote;
90              
91             $q = Finance::Quote->new;
92              
93             %info = Finance::Quote->fetch('cse', 'YORK.N0000');
94              
95             =head1 DESCRIPTION
96              
97             This module fetches information from the Colombo Stock Exchange (CSE)
98             in Sri Lanka http://www.cse.lk.
99              
100             This module is loaded by default on a Finance::Quote object. It's also possible
101             to load it explicitly by placing 'CSE' in the argument list to
102             Finance::Quote->new().
103              
104             =head1 LABELS RETURNED
105              
106             The following labels may be returned by Finance::Quote::CSE :
107             isin name currency date isodate ask close high low open volume success
108              
109             =head1 TERMS & CONDITIONS
110              
111             Use of www.cse.lk is governed by any terms & conditions of that site.
112              
113             Finance::Quote is released under the GNU General Public License, version 2,
114             which explicitly carries a "No Warranty" clause.
115              
116             =cut
117