File Coverage

blib/lib/App/bovespa.pm
Criterion Covered Total %
statement 34 35 97.1
branch 3 4 75.0
condition n/a
subroutine 7 7 100.0
pod 1 3 33.3
total 45 49 91.8


line stmt bran cond sub pod time code
1 1     1   459 use strict;
  1         1  
  1         24  
2 1     1   4 use warnings;
  1         2  
  1         26  
3             package App::bovespa;
4              
5 1     1   377 use LWP::UserAgent;
  1         30765  
  1         26  
6 1     1   401 use JSON;
  1         5240  
  1         5  
7              
8             my $agent_string = "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0";
9              
10             sub new {
11 1     1 0 419 my ( $class ) = @_;
12              
13 1         4 bless {
14             }, $class;
15             }
16              
17             sub stock {
18 2     2 1 471 my ( $self, $stock ) = @_;
19              
20 2         7 return $self->yahoo( $stock );
21              
22             }
23              
24             sub yahoo {
25 2     2 0 4 my ( $self, $stock ) = @_;
26              
27 2         3 my $url = "https://query.yahooapis.com/v1/public/yql";
28              
29             #$stock = uc $stock . ".sa";
30 2         4 $stock = uc $stock;
31 2         4 my $yahoo_query = "select * from yahoo.finance.quotes where symbol in ( \"$stock\" )";
32              
33 2         10 my $ua = LWP::UserAgent->new();
34 2         2083 $ua->ssl_opts( verify_hostname => 0 );
35 2         48 $ua->timeout( 10 );
36              
37 2         27 $DB::single = 1;
38              
39 2         9 my %url_params = (
40             q => $yahoo_query,
41             format => "json",
42             env => "store://datatables.org/alltableswithkeys",
43             callback => undef,
44             );
45              
46              
47 2         2 my $query_params = "?";
48 2         10 while ( my ( $key, $value ) = each( %url_params ) ) {
49 8 100       28 $query_params .= "$key=" . ( $value ? $value : '' ). "\&";
50             }
51              
52 2         8 my $response = $ua->get( $url . $query_params );
53              
54 2         860397 my $raw_html;
55 2 50       8 if ( $response->is_success ){
56 2         26 $raw_html = $response->decoded_content;
57             }else{
58 0         0 die;
59             }
60              
61 2         478 my $data = decode_json $raw_html;
62 2         107 return $data->{ query }{ results }{ quote }{ Bid };
63              
64             }
65              
66             =head1 NAME
67              
68             App::bovespa - Simple tool to follow up your stocks at Bovespa Stock Exchange
69              
70             =head1 VERSION
71              
72             Version 0.003
73              
74             =head1 SYNOPSIS
75              
76             This module is a crawler to get the cotation of the Bovespa Stock Exchange. Now
77             it gets information one of the main portals.
78              
79             This module is just for follow up, really, nothing realtime, nothing too serious.
80             I wrote it to keep my eyes on my own stocks. It is easier run a script that login
81             on my homebroker agent, with tokens and etc just to see the cotation.
82              
83             To get a stock cotation is plain simple:
84              
85             use App::bovespa;
86              
87             my $exchange = App::bovespa->new();
88             my $cotation = $exchange->stock( "PETR4.SA" );
89              
90             Also inside this distribution, comes a small tool called bovespa_report, it has
91             a wrapper for cotation and a parser for a file with a list of cotations and prices to
92             compare.
93              
94             bovespa_report --help
95              
96             Will display all options related with the tool
97              
98             =head1 SUBROUTINES/METHODS
99              
100             =head2 stock
101              
102             Receives a stock name and returns it cotation. The name must the symbol name for the stock and the type digit.
103             Like "PETR4.SA" or "PETR3.SA"
104              
105             =head1 AUTHOR
106              
107             RECSKY, C<< recsky at cpan.org >>
108              
109             =head1 BUGS
110              
111             In case of one of providers change it page or something like that, the lib may broke. In this case
112             contact me at recsky@cpan.org
113              
114             =head1 LICENSE AND COPYRIGHT
115              
116             Copyright 2015
117              
118             This program is free software; you can redistribute it and/or modify it
119             under the terms of the the Artistic License (2.0). You may obtain a
120             copy of the full license at:
121              
122             L
123             =cut
124              
125             1;