File Coverage

blib/lib/Finance/Bitcoin/Feed/Site/CoinSetter.pm
Criterion Covered Total %
statement 36 36 100.0
branch 4 4 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 47 47 100.0


line stmt bran cond sub pod time code
1             package Finance::Bitcoin::Feed::Site::CoinSetter;
2 1     1   26006 use strict;
  1         2  
  1         25  
3 1     1   304 use Finance::Bitcoin::Feed::Site::CoinSetter::Socket;
  1         3  
  1         10  
4 1     1   27 use Mojo::Base 'Finance::Bitcoin::Feed::Site';
  1         1  
  1         3  
5 1     1   620 use Mojo::UserAgent;
  1         80422  
  1         8  
6              
7             our $VERSION = '0.04';
8              
9             # Module implementation here
10             has ws_url => 'https://plug.coinsetter.com:3000/socket.io/1';
11             has 'ua';
12             has 'site' => 'COINSETTER';
13              
14             # this site need 2 handshakes
15             # 1. get session id by http GET method
16             # 2. generate a new url by adding session id to the old url
17             # 3. connect by socket id
18             sub go {
19 3     3 1 1067 my $self = shift;
20 3         13 $self->SUPER::go;
21 3         8 $self->ua(Mojo::UserAgent->new());
22 3         17 $self->debug('get handshake information');
23 3         6 my $tx = $self->ua->get($self->ws_url);
24 3 100       145 unless ($tx->success) {
25 1         33 my $err = $tx->error;
26 1         33 $self->error("Connection error of Site CoinSetter: $err->{message}");
27 1         6 $self->set_timeout;
28 1         4 return;
29             }
30              
31             # f_P7lQkhkg4JD5Xq0LCl:60:60:websocket,htmlfile,xhr-polling,jsonp-polling
32 2         62 my ($sid, $hb_timeout, $con_timeout, $transports) = split /:/, $tx->res->text;
33              
34 2         112 my $url = $self->ws_url . "/websocket/$sid";
35 2         13 $url =~ s/https/wss/;
36              
37 2         4 $self->debug('connecting...', $url);
38              
39             my $socket = $self->ua->websocket(
40             $url => sub {
41 2     2   81 my ($ua, $tx) = @_;
42 2         4 $self->debug('connected!');
43 2 100       5 unless ($tx->is_websocket) {
44 1         51 $self->error("Site BtcChina WebSocket handshake failed!");
45              
46             # set timeout;
47 1         4 $self->set_timeout;
48 1         2 return;
49             }
50 1         46 bless $tx, 'Finance::Bitcoin::Feed::Site::CoinSetter::Socket';
51 1         4 $tx->configure($self);
52 2         3 });
53 2         7 return;
54             }
55              
56             1;
57              
58             __END__
59              
60             =head1 NAME
61              
62             Finance::Bitcoin::Feed::Site::CoinSetter -- the class that connect and fetch the bitcoin price data from site Coinsetter
63              
64              
65             =head1 SYNOPSIS
66              
67             use Finance::Bitcoin::Feed::Site::CoinSetter;
68             use AnyEvent;
69              
70             my $obj = Finance::Bitcoin::Feed::Site::BitStamp->new();
71             # listen on the event 'output' to get the adata
72             $obj->on('output', sub { shift; say @_ });
73             $obj->go();
74              
75             # dont forget this
76             AnyEvent->condvar->recv;
77            
78             =head1 DESCRIPTION
79              
80             Connect to site BitStamp by protocol socket.io v 0.9.6 and fetch the bitcoin price data.
81              
82             =head1 EVENTS
83              
84             This class inherits all events from L<Finance::Bitcoin::Feed::Site> and add some new ones.
85             The most important event is 'output'.
86              
87             =head2 output
88              
89             It will be emit by its parent class when print out the data. You can listen on this event to get the output.
90              
91             =head2 subscribe
92              
93             It will subscribe channel from the source site. You can subscribe more channels in the method L</configure>
94              
95             =head1 SEE ALSO
96              
97             L<Finance::Bitcoin::Feed::Site>
98              
99             L<https://www.coinsetter.com/api>
100              
101             L<Mojo::UserAgent>
102              
103             L<socket.io-parser|https://github.com/Automattic/socket.io-parser>
104              
105             =head1 AUTHOR
106              
107             Chylli C<< <chylli@binary.com> >>
108