File Coverage

blib/lib/Finance/Bitcoin/Feed.pm
Criterion Covered Total %
statement 29 38 76.3
branch 0 2 0.0
condition n/a
subroutine 9 10 90.0
pod 1 2 50.0
total 39 52 75.0


line stmt bran cond sub pod time code
1             package Finance::Bitcoin::Feed;
2              
3 2     2   32885 use strict;
  2         4  
  2         64  
4 2     2   8 use warnings;
  2         2  
  2         46  
5              
6 2     2   815 use Mojo::Base 'Mojo::EventEmitter';
  2         13708  
  2         12  
7 2     2   4809 use AnyEvent;
  2         8227  
  2         69  
8 2     2   1034 use Module::Runtime qw(require_module);
  2         2535  
  2         10  
9 2     2   95 use Carp;
  2         3  
  2         112  
10              
11 2     2   8 use feature qw(say);
  2         3  
  2         564  
12              
13             our $VERSION = '0.03';
14              
15             has 'sites' => sub { [qw(Hitbtc BtcChina CoinSetter LakeBtc BitStamp)] };
16             has 'output' => sub {
17             sub { shift; say join " ", @_ }
18             };
19              
20             sub new {
21 2     2 1 240 my $class = shift;
22 2         14 my $self = $class->SUPER::new(@_);
23 2         62 $self->on('output', $self->output);
24 2         17 return $self;
25             }
26              
27             sub run {
28 1     1 0 1 my $self = shift;
29              
30 1         2 my @sites;
31              
32 1         1 for my $site_class (@{$self->sites}) {
  1         17  
33 0           $site_class = 'Finance::Bitcoin::Feed::Site::' . $site_class;
34 0 0         eval { require_module($site_class) }
  0            
35             || croak("No such module $site_class");
36 0           my $site = $site_class->new;
37 0     0     $site->on('output', sub { shift, $self->emit('output', @_) });
  0            
38 0           $site->go;
39 0           push @sites, $site;
40             }
41              
42 0           AnyEvent->condvar->recv;
43             }
44              
45             1;
46              
47             __END__
48              
49             =head1 NAME
50              
51             Finance::Bitcoin::Feed - Collect bitcoin real-time price from many sites' streaming data source
52              
53             =head1 SYNOPSIS
54              
55             use Finance::Bitcoin::Feed;
56              
57             #default output is to print to the stdout
58             Finance::Bitcoin::Feed->new->run();
59             # will print output to the stdout:
60             # BITSTAMP BTCUSD 123.00
61            
62              
63             #or custom your stdout
64             open my $fh, ">out.txt";
65             $fh->autoflush();
66             my $feed = Finance::Bitcoin::Feed->new(output => sub{
67             my ($self, $site, $currency, $price) = @_;
68             print $fh "the price currency $currency on site $site is $price\n";
69             });
70             # let's go!
71             $feed->run();
72              
73             #you can also custom which site you want to connect
74             Finance::Bitcoin::Feed->new(sites => [qw(LakeBtc)])->go;
75              
76             =head1 DESCRIPTION
77              
78             L<Finance::Bitcoin::Feed> is a bitcoin realtime data source which collect real time data source from these sites:
79              
80             =over 4
81              
82             =item * L<HitBtc|https://hitbtc.com/api#socketio>
83              
84             =item * L<BtcChina|http://btcchina.org/websocket-api-market-data-documentation-en>
85              
86             =item * L<CoinSetter|https://www.coinsetter.com/api/websockets/last>
87              
88             =item * L<<lakebtc api|https://www.lakebtc.com/s/api>
89              
90             =back
91              
92             The default output format to the stdout by this format:
93              
94             site_name TIMESTAMP CURRENCY price
95              
96             For example:
97              
98             COINSETTER 1418173081724 BTCUSD 123.00
99              
100             The unit of timestamp is ms.
101              
102             You can custom your output by listen on the event L<output> and modify the data it received.
103              
104             Note the followiing sites doesn't give the timestamp. So the timestamp in the result will be 0:
105              
106             LakeBtc
107              
108             =head1 METHODS
109              
110             This class inherits all methods from L<Mojo::EventEmitter>
111              
112             =head2 new
113              
114             This method have two arguments by which you can costumize the behavior of the feed:
115              
116             =head3 sites
117              
118             which sites you want to connect. It is in fact the array reference of module names of Finance::Bitcoin::Feed::Site::*. Now there are the following sites:
119             Hitbtc
120             BtcChina
121             CoinSetter
122             LakeBtc
123             BitStamp
124              
125             You can also put your own site module under this namespace and added here.
126              
127             =head3 output
128              
129             customize the output format by giving this argument a sub reference. It will be bind to the event 'output'. Please rever to the event <output>.
130              
131             # you can customize the output by giving argument 'output' to the new methold
132             open my $fh, ">out.txt";
133             $fh->autoflush();
134             my $feed = Finance::Bitcoin::Feed->new(output => sub{
135             my ($self, $site, $timestamp, $currency, $price) = @_;
136             print $fh "the price currency $currency on site $site is $price\n";
137             });
138             # let's go!
139             $feed->run();
140              
141              
142             =head1 EVENTS
143              
144             This class inherits all events from L<Mojo::EventEmitter> and add the following new ones:
145              
146             =head2 output
147              
148             This event has a default subscriber:
149              
150             #output to the stdout, the default action:
151             $feed->on('output', sub { shift; say join " ", @_ } );
152              
153             You can customize the output by giving argument 'output' to the new method
154              
155             open my $fh, ">out.txt";
156             $fh->autoflush();
157             my $feed = Finance::Bitcoin::Feed->new(output => sub{
158             my ($self, $site, $timestamp, $currency, $price) = @_;
159             print $fh "the price currency $currency on site $site is $price\n";
160             });
161             # let's go!
162             $feed->run();
163              
164             Or you can bind output directly to the feed to get multi outout or you should unscribe this event first.
165              
166             $feed->on('output', sub {....})
167              
168             The arguments of this event is:
169              
170             $self: the site class object
171             timestamp: the timestamp of the data. If no timestamp is given by the site, then the value of it is 0.
172             sitename: the site class name
173             price: the price
174              
175              
176             =head1 DEBUGGING
177              
178             You can set the FINANCE_BITCOIN_FEED_DEBUG environment variable to get some advanced diagnostics information printed to STDERR.
179             And these modules use L<Mojo::UserAgent>, you can also open the MOJO_USERAGENT_DEBUG environment variable:
180              
181             FINANCE_BITCOIN_FEED_DEBUG=1
182             MOJO_USERAGENT_DEBUG=1
183              
184             =head1 SEE ALSO
185              
186             L<Mojo::EventEmitter>
187              
188             L<Finance::Bitcoin::Feed::Site::BitStamp>
189              
190             L<Finance::Bitcoin::Feed::Site::Hitbtc>
191              
192             L<Finance::Bitcoin::Feed::Site::BtcChina>
193              
194             L<Finance::Bitcoin::Feed::Site::CoinSetter>
195              
196             L<Finance::Bitcoin::Feed::Site::LakeBtc>
197              
198             L<Finance::Bitcoin::Feed::Site::BitStamp>
199              
200             =head1 AUTHOR
201              
202             Chylli C<< <chylli@binary.com> >>
203              
204             =head1 COPYRIGHT
205              
206             Copyright 2014- Binary.com