blib/lib/Finance/BitStamp/Socket.pm

filter: Statementstmt 38.1% 76 / 199 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 43.1% 25 / 58 Podpod 0.0% 0 / 28 totaltotal 31.1% 101 / 324 CC 21 SCAR 24.6
CC
21
Cov
32%
__ANON__
72.0
setup
30.0
init
12.0
CRAP 11.8
↑ Summary
1 package Finance::BitStamp::Socket;
2  
3 1 ▶use 5.014002;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our $VERSION = '0.01';
7  
8 1 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Protocol::WebSocket::Handshake::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Protocol::WebSocket::Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use constant VERBOSE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use constant CHANNELS => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
21 order_book
22 live_trades
23 1 );
24  
25 1 ▶use constant ATTRIBUTES => qw(protocol app_key channels ssl);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use constant PROTOCOL => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use constant APP_KEY => 'de504dc5763aeef9ff52';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # TODO: test SSL. This didnt seem to work for me last time I set it...
30 1 ▶use constant SSL => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # cleartext...
33 1 ▶use constant PORT => 80;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use constant SCHEME => 'ws';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use constant TLS => undef;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use constant HOST => 'ws.pusherapp.com';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 # ssl...
39 1 ▶use constant SSL_PORT => 443;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use constant SSL_SCHEME => 'wws';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use constant SSL_TLS => 'connect';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use constant SSL_HOST => 'wws.pusherapp.com';
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 # THESE two methods: trade() and order_book() are the main methods you will want to move and rewrite into your own module.
45 # within these subroutines you will have access to the json response in a hash format.
46 0 ▶sub trade {
Subroutine trade: not called CC=1 SCAR=6.9
Pod undocumented
47 0 my $self = shift;
48 0 my $data = shift;
49 0 warn Data::Dumper->Dump([$data]);
50 0 warn "*** I am the default trade()... you should overwride this method in your own package\n";
51 }
52  
53 0 ▶sub order_book {
Subroutine order_book: not called CC=1 SCAR=6.9
Pod undocumented
54 0 my $self = shift;
55 0 my $data = shift;
56 0 warn Data::Dumper->Dump([$data]);
57 0 warn "** I am the default order_book()... you should overwride this method in your own package\n";
58 }
59 # end the methods you should definately override.
60  
61  
62 # This module is meant to be used as a base for your own module.
63 # Your own module will decide what to do with the incoming message through the
64 # trade() and order_book() routines.
65 #
66 # You should look at "test.pl" to see a basic example.
67  
68 0 ▶sub new { (bless {} => shift)->init(@_) }
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
69  
70 0 ▶sub init {
Subroutine init: not called CC=3 SCAR=24.8
Pod undocumented
71 0 my $self = shift;
72 0 my %args = @_;
73 0 foreach my $attribute ($self->attributes) {
74 0 ▶ $self->$attribute($args{$attribute}) if exists $args{$attribute};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$attribute} 0 0 0
75 }
76 0 return $self;
77 }
78  
79 0 ▶sub setup {
Subroutine setup: not called CC=5 SCAR=34.0
Pod undocumented
80 0 my $self = shift;
81 0 ▶ $self->channels([CHANNELS]) unless $self->channels;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->channels 0 0 0
82 0 ▶ $self->protocol( PROTOCOL ) unless $self->protocol;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->protocol 0 0 0
83 0 ▶ $self->app_key ( APP_KEY ) unless $self->app_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->app_key 0 0 0
84 0 ▶ $self->ssl ( SSL ) unless $self->ssl;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ssl 0 0 0
85 }
86  
87 0 ▶sub go {
Subroutine go: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my $self = shift;
89 0 $self->setup;
90 0 $self->handle;
91 0 $self->wait;
92 }
93  
94 0 ▶sub handle {
Subroutine handle: not called CC=1 SCAR=6.9
Pod undocumented
95 0 my $self = shift;
96 0 $self->client(Protocol::WebSocket::Handshake::Client->new(url => $self->uri->as_string));
97 0 $self->frame(Protocol::WebSocket::Frame->new);
98 0 $self->{handle} = AnyEvent::Handle->new(
99 connect => [$self->host, $self->port],
100 tls => $self->tls,
101 tls_ctx => {verify => 0},
102 keepalive => 1,
103 wtimeout => 50,
104 on_connect => $self->on_connect,
105 on_read => $self->on_read,
106 on_wtimeout => $self->on_wtimeout,
107 on_error => $self->on_error,
108 on_eof => $self->on_eof,
109 );
110 }
111  
112 0 ▶sub on_read {
Subroutine on_read: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my $self = shift;
114 0 ▶ return sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
115 0 my $handle = shift;
116 0 my $chunk = $handle->{rbuf};
117 0 $handle->{rbuf} = undef;
118 0 ▶ if (!$self->client->is_done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->client->is_done) 0 0 0
119 0 $self->client->parse($chunk);
120 }
121  
122 0 $self->frame->append($chunk);
123 0 ▶ if ($self->frame->is_ping()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->frame->is_ping) 0 0 0
124 0 $handle->push_write(
125 $self->frame->new(buffer => '', type => 'pong')->to_bytes
126 );
127 }
128 0 ▶ while (my $msg = $self->frame->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $self->frame->next) 0 0 0
129 0 my $d;
130 eval {
131 0 $d = $self->json->decode($msg);
132 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $d = $self->json->decode($msg) } } or do { my $e = $@; warn $self->now . ' - error: ' . $e; next } 0 0 0
133 0 my $e = $@;
134 0 warn $self->now . ' - error: ' . $e;
135 0 next;
136 };
137 0 given ($d->{event}) {
138  
139 0 when ('pusher:connection_established') {
140 0 say $self->now . ' - subscribing to events' if VERBOSE;
141 0 foreach my $channel (@{$self->channels}) {
142 0 say $self->now . ' - requesting channel: ' . $channel if VERBOSE;
143 0 $handle->push_write(
144 $self->frame->new($self->json->encode({
145 event => 'pusher:subscribe',
146 data => {
147 channel => $channel,
148 },
149 }))->to_bytes
150 );
151 }
152 }
153 0 when ('pusher_internal:subscription_succeeded') {
154 0 printf("%s - subscribed to channel: %s\n", $self->now, $d->{channel}) if VERBOSE;
155 }
156  
157  
158 0 when ('trade') {
159 0 printf("%s - got %s request on channel: %s\n", $self->now, @{$d}{qw(event channel)}) if VERBOSE;
160 0 ▶ if ($d->{channel} eq 'live_trades') {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->{'channel'} eq 'live_trades') { } 0 0 0
161 0 my $data = $self->json->decode($d->{data});
162 0 $self->trade($data);
163 }
164 else {
165 0 printf "%s - got event: %s", $self->now, Dumper $d if VERBOSE;
166 }
167 }
168 0 when ('data') {
169 0 printf("%s - got %s request on channel: %s\n", $self->now, @{$d}{qw(event channel)}) if VERBOSE;
170 0 ▶ if ($d->{channel} eq 'order_book') {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->{'channel'} eq 'order_book') { } 0 0 0
171 0 my $data = $self->json->decode($d->{data});
172 0 $self->order_book($data);
173 }
174 else {
175 0 printf '%s - got event: %s', $self->now, Dumper $d if VERBOSE;
176 }
177 }
178  
179 0 default {
180 0 printf '%s - got event: %s', $self->now, Dumper $d if VERBOSE;
181 }
182 }
183 }
184 }
185 0 }
186  
187 0 ▶sub on_connect {
Subroutine on_connect: not called CC=1 SCAR=6.9
Pod undocumented
188 0 my $self = shift;
189 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
190 0 my $handle = shift;
191 0 say $self->now . ' - connected to pusher' if VERBOSE;
192 0 $handle->push_write($self->client->to_string);
193 }
194 0 }
195  
196 0 ▶sub on_wtimeout {
Subroutine on_wtimeout: not called CC=1 SCAR=6.9
Pod undocumented
197 0 my $self = shift;
198 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
199 0 my $handle = shift;
200 0 $handle->push_write(
201 $self->frame->new(buffer => '', type => 'ping')->to_bytes
202 );
203 }
204 0 }
205  
206 0 ▶sub on_error {
Subroutine on_error: not called CC=1 SCAR=6.9
Pod undocumented
207 0 my $self = shift;
208 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
209 0 my ($handle, $fatal, $msg) = @_;
210 0 warn $self->now . " - fatal($fatal): $msg" if VERBOSE or DEBUG;
211 0 $handle->destroy;
212 0 $self->setup;
213 }
214 0 }
215  
216 0 ▶sub on_eof {
Subroutine on_eof: not called CC=1 SCAR=6.9
Pod undocumented
217 0 my $self = shift;
218 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 0 my $handle = shift;
220 0 warn $self->now . " - lost connection, reconnecting" if VERBOSE or DEBUG;
221 0 $self->setup;
222 }
223 0 }
224  
225 0 ▶sub attributes { ATTRIBUTES }
Subroutine attributes: not called CC=1 SCAR=6.9
Pod undocumented
226 0 ▶sub wait { AnyEvent->condvar->wait }
Subroutine wait: not called CC=1 SCAR=6.9
Pod undocumented
227 0 ▶sub json { shift->{json} ||= JSON->new }
Subroutine json: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
+(shift())->{"json"} ||= "JSON"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
+(shift())->{"json"} ||= "JSON"->new
+(shift())->{"json"} "JSON"->new
Truth table0%0/3
A ||= B
A= +(shift())->{"json"}B= "JSON"->new
ABresult
1X1
011
000
228 0 ▶sub host { shift->ssl ? SSL_HOST : HOST }
Subroutine host: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
(shift())->ssl ? : 0 0 0
229 0 ▶sub port { shift->ssl ? SSL_PORT : PORT }
Subroutine port: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
(shift())->ssl ? : 0 0 0
230 0 ▶sub tls { shift->ssl ? SSL_TLS : TLS }
Subroutine tls: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
(shift())->ssl ? : 0 0 0
231 0 ▶sub scheme { shift->ssl ? SSL_SCHEME : SCHEME }
Subroutine scheme: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
(shift())->ssl ? : 0 0 0
232 0 ▶sub client { my $self = shift; $self->get_set(@_) }
Subroutine client: not called CC=1 SCAR=6.9
Pod undocumented
233 0 ▶sub frame { my $self = shift; $self->get_set(@_) }
Subroutine frame: not called CC=1 SCAR=6.9
Pod undocumented
234 0 ▶sub channels { my $self = shift; $self->get_set(@_) }
Subroutine channels: not called CC=1 SCAR=6.9
Pod undocumented
235 0 ▶sub protocol { my $self = shift; $self->get_set(@_) }
Subroutine protocol: not called CC=1 SCAR=6.9
Pod undocumented
236 0 ▶sub app_key { my $self = shift; $self->get_set(@_) }
Subroutine app_key: not called CC=1 SCAR=6.9
Pod undocumented
237 0 ▶sub ssl { my $self = shift; $self->get_set(@_) }
Subroutine ssl: not called CC=1 SCAR=6.9
Pod undocumented
238 0 ▶sub now { sprintf '%4d-%02d-%02d %02d:%02d:%02d', (localtime(time))[5] + 1900, (localtime(time))[4,3,2,1,0] }
Subroutine now: not called CC=1 SCAR=6.9
Pod undocumented
239  
240 0 ▶sub get_set {
Subroutine get_set: not called CC=2 SCAR=17.9
Pod undocumented
241 0 my $self = shift;
242 0 my $attribute = ((caller(1))[3] =~ /::(\w+)$/)[0];
243 0 ▶ $self->{$attribute} = shift if scalar @_;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 0 0 0
244 0 return $self->{$attribute};
245 }
246  
247 0 ▶sub uri {
Subroutine uri: not called CC=2 SCAR=17.9
Pod undocumented
248 0 my $self = shift;
249 0 ▶ unless ($self->{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"uri"}) 0 0 0
250 0 my $uri = URI->new;
251 0 $uri->scheme('http');
252 0 $uri->host($self->host);
253 0 $uri->path(sprintf '/app/%s' => $self->app_key);
254 0 $uri->query_form(protocol => $self->protocol);
255 0 $uri->scheme($self->scheme);
256 0 $self->{uri} = $uri;
257 }
258 0 return $self->{uri};
259 }
260  
261  
262  
263 1 1;
264  
265 __END__
↑ Summary