|
1 | 1 | package WebService::Bitstamp;
|
2 | 2 |
|
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; |
27 | 5 |
|
28 | 6 | our $VERSION = '0.01';
|
29 | 7 |
|
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 | +} |
32 | 68 |
|
33 | 69 | 1;
|
34 | 70 | __END__
|
|
0 commit comments