Skip to content

Commit cac0dd9

Browse files
author
Sven Eppler
committedJun 17, 2014
Implemented basic api, not working
1 parent d6eb706 commit cac0dd9

File tree

2 files changed

+72
-26
lines changed

2 files changed

+72
-26
lines changed
 

‎lib/WebService/Bitstamp.pm

+62-26
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,70 @@
11
package WebService::Bitstamp;
22

3-
use 5.014002;
4-
use strict;
5-
use warnings;
6-
7-
require Exporter;
8-
9-
our @ISA = qw(Exporter);
10-
11-
# Items to export into callers namespace by default. Note: do not export
12-
# names by default without a very good reason. Use EXPORT_OK instead.
13-
# Do not simply export all your public functions/methods/constants.
14-
15-
# This allows declaration use WebService::Bitstamp ':all';
16-
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
17-
# will save memory.
18-
our %EXPORT_TAGS = ( 'all' => [ qw(
19-
20-
) ] );
21-
22-
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
23-
24-
our @EXPORT = qw(
25-
26-
);
3+
use Mojo::Base "Mojo::EventEmitter";
4+
use Mojo::UserAgent;
275

286
our $VERSION = '0.01';
297

30-
31-
# Preloaded methods go here.
8+
has 'ua' => sub { Mojo::UserAgent->new(); };
9+
10+
sub connect {
11+
my $self = shift;
12+
$self->ua->inactivity_timeout(0);
13+
14+
$self->ua->on(error => sub {
15+
my ($tx, $error) = @_;
16+
die " * FATAL: $error";
17+
});
18+
19+
$self->ua->websocket("ws://ws.pusherapp.com/app/de504dc5763aeef9ff52?protocol=5" => sub {
20+
my ($ua, $tx) = @_;
21+
22+
say 'WebSocket handshake failed!' and return unless $tx->is_websocket;
23+
24+
$tx->on(error => sub {
25+
my ($tx, $error) = @_;
26+
say "*** ERROR: $error";
27+
});
28+
29+
$tx->on(finish => sub {
30+
my ($tx, $code, $reason) = @_;
31+
$reason //= "Unknown reason!";
32+
say "WebSocket closed with status $code and reason: $reason.";
33+
});
34+
35+
$tx->on(message => $self->on_message);
36+
$tx->on(trade => $self->on_trade);
37+
38+
$tx->send({
39+
json => {
40+
event => "pusher:subscribe",
41+
data => { channel => "live_trades" },
42+
}
43+
});
44+
});
45+
}
46+
47+
sub on_message {
48+
my ($tx, $msg) = @_;
49+
my $data = j($msg);
50+
given ($data->{event}) {
51+
when ("pusher:connection_established") { say " * Connected to API" }
52+
when ("pusher_internal:subscription_succeeded") { say " * Subscribed to channel " . $data->{channel} }
53+
when ("trade") { $tx->emit("trade", $data); }
54+
default { say " * UNHANDLED MSG: " . Dumper $data }
55+
}
56+
}
57+
58+
sub on_trade {
59+
my ($tx, $msg) = @_;
60+
my $trade = j($msg->{data});
61+
62+
my ($price, $amount) = ($trade->{price}, $trade->{amount});
63+
64+
my $trend = "Unknown";
65+
66+
printf(' * Trade: %.3fBTC @ $%.2f - '."$trend\n", $amount, $price);
67+
}
3268

3369
1;
3470
__END__

‎test.pl

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env perl
2+
3+
use Mojo::Base -strict;
4+
use lib './lib/';
5+
use WebService::Bitstamp;
6+
7+
my $bs = WebService::Bitstamp->new();
8+
$bs->connect();
9+
10+
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

0 commit comments

Comments
 (0)
Please sign in to comment.