File Coverage

blib/lib/Finance/Bitcoin/Feed/Site/BtcChina.pm
Criterion Covered Total %
statement 26 26 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 35 35 100.0


line stmt bran cond sub pod time code
1             package Finance::Bitcoin::Feed::Site::BtcChina;
2 1     1   20174 use strict;
  1         1  
  1         25  
3 1     1   318 use Finance::Bitcoin::Feed::Site::BtcChina::Socket;
  1         2  
  1         10  
4 1     1   25 use Mojo::Base 'Finance::Bitcoin::Feed::Site';
  1         1  
  1         3  
5 1     1   583 use Mojo::UserAgent;
  1         107474  
  1         10  
6              
7             our $VERSION = '0.05';
8              
9             has ws_url => 'wss://websocket.btcchina.com/socket.io/?transport=websocket';
10             has 'ua';
11             has 'site' => 'BTCCHINA';
12              
13             sub go {
14 2     2 1 659 my $self = shift;
15 2         7 $self->SUPER::go;
16              
17 2         6 $self->ua(Mojo::UserAgent->new());
18 2         35 $self->debug('connecting...', $self->ws_url);
19             $self->ua->websocket(
20             $self->ws_url => sub {
21 2     2   109 my ($ua, $tx) = @_;
22 2         4 $self->debug('connected!');
23 2 100       5 unless ($tx->is_websocket) {
24 1         52 $self->error("Site BtcChina WebSocket handshake failed!");
25              
26             # set timeout;
27 1         7 $self->set_timeout;
28 1         1 return;
29             }
30              
31 1         66 bless $tx, 'Finance::Bitcoin::Feed::Site::BtcChina::Socket';
32 1         5 $tx->configure($self);
33 2         5 });
34 2         8 return;
35             }
36              
37             1;
38             __END__
39              
40             =head1 NAME
41              
42             Finance::Bitcoin::Feed::Site::BtcChina -- the class that connect and fetch the bitcoin price data from site btcchina
43              
44             =head1 SYNOPSIS
45              
46             use Finance::Bitcoin::Feed::Site::BtcChina;
47             use AnyEvent;
48              
49             my $obj = Finance::Bitcoin::Feed::Site::BtcChina->new();
50             # listen on the event 'output' to get the adata
51             $obj->on('output', sub { shift; say @_ });
52             $obj->go();
53              
54             # dont forget this
55             AnyEvent->condvar->recv;
56              
57             =head1 DESCRIPTION
58              
59             Connect to site BitStamp by protocol socket.io v2.2.2 and fetch the bitcoin price data.
60              
61             =head1 EVENTS
62              
63             This class inherits all events from L<Finance::Bitcoin::Feed::Site> and add some new ones.
64             The most important event is 'output'.
65              
66             =head2 output
67              
68             It will be emit by its parent class when print out the data. You can listen on this event to get the output.
69              
70             =head2 subscribe
71              
72             It will subscribe channel from the source site. You can subscribe more channels in the method L</configure>
73              
74             =head1 SEE ALSO
75              
76             L<Finance::Bitcoin::Feed::Site>
77              
78             L<btcchina api|http://btcchina.org/websocket-api-market-data-documentation-en>
79              
80             L<socket.io-parse|https://github.com/Automattic/socket.io-parser>
81              
82             L<Mojo::UserAgent>
83              
84             =head1 AUTHOR
85              
86             Chylli C<< <chylli@binary.com> >>
87