File Coverage

blib/lib/Finance/Bitcoin/Feed.pm
Criterion Covered Total %
statement 29 40 72.5
branch 0 2 0.0
condition n/a
subroutine 9 10 90.0
pod 1 2 50.0
total 39 54 72.2


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