File Coverage

blib/lib/Finance/Bitcoin/Feed/Site.pm
Criterion Covered Total %
statement 38 49 77.5
branch 1 6 16.6
condition n/a
subroutine 10 13 76.9
pod 9 9 100.0
total 58 77 75.3


line stmt bran cond sub pod time code
1             package Finance::Bitcoin::Feed::Site;
2 6     6   17528 use strict;
  6         9  
  6         178  
3              
4 6     6   387 use Mojo::Base 'Mojo::EventEmitter';
  6         6360  
  6         31  
5 6     6   8275 use AnyEvent;
  6         16787  
  6         2724  
6              
7             our $VERSION = '0.05';
8              
9             has last_activity_at => 0;
10             has last_activity_period => 300;
11             has 'timer';
12             has started => 0;
13              
14             #override this attribute by real site name
15             has site => '';
16              
17             sub new {
18 6     6 1 87 my $class = shift;
19 6         73 my $self = $class->SUPER::new(@_);
20 6         95 $self->on('timeout', \&on_timeout);
21 6         92 $self->on('data_out', \&on_data_out);
22              
23             my $timer = AnyEvent->timer(
24             after => 0, # first invoke ASAP
25             interval => 1, # then invoke every second
26             cb => sub { # the callback to invoke
27 0     0   0 $self->timer_call_back;
28             },
29 6         74 );
30 6         10321 $self->timer($timer);
31              
32 6         44 return $self;
33             }
34              
35             sub on_data_out {
36 8     8 1 435 my ($self, @content) = @_;
37 8         32 $self->last_activity_at(time());
38 8         44 $self->emit('output', $self->site, @content);
39 8         121 return;
40             }
41              
42             sub timer_call_back {
43 0     0 1 0 my $self = shift;
44 0 0       0 return unless $self->started;
45 0 0       0 if ($self->is_timeout) {
46 0         0 $self->emit('timeout');
47             }
48 0         0 return;
49             }
50              
51             sub set_timeout {
52 7     7 1 1130 my $self = shift;
53 7         18 $self->debug('set timeout...');
54 7         38 $self->last_activity_at(time - $self->last_activity_period - 100);
55 7         57 return;
56             }
57              
58             sub is_timeout {
59 9     9 1 5638 my $self = shift;
60 9         32 return time() - $self->last_activity_at > $self->last_activity_period;
61             }
62              
63             sub on_timeout {
64 0     0 1 0 my $self = shift;
65              
66 0         0 $self->debug('reconnecting...');
67 0         0 return $self->go;
68             }
69              
70             sub go {
71 11     11 1 1946 my $self = shift;
72 11         42 $self->debug("starting ", $self->site);
73 11         45 $self->started(1);
74 11         93 $self->last_activity_at(time());
75 11         41 return;
76             }
77              
78             sub debug {
79 37     37 1 135 my $self = shift;
80 37 50       93 if ($ENV{FINANCE_BITCOIN_FEED_DEBUG}) {
81 0         0 say STDERR $self->site, "-------------------------";
82 0         0 say STDERR @_;
83             }
84 37         50 return;
85             }
86              
87             sub error {
88 5     5 1 16 my ($self, @content) = @_;
89 5         15 say STDERR $self->site, "-------------------------";
90 5         887 say STDERR @content;
91 5         20 return;
92             }
93              
94             1;
95              
96             __END__
97              
98             =head1 NAME
99              
100             Finance::Bitcoin::Feed::Site - Base class of Finance::Bitcoin::Feed::Site::* modules
101              
102              
103             =head1 SYNOPSIS
104              
105             use Mojo::Base 'Finance::Bitcoin::Feed::Site';
106             has site => 'SITENAME';
107              
108              
109             sub go{
110             my $self = shift;
111             #Dont' forget this line:
112             $self->SUPER::go();
113             # connect the site
114             # parse the data
115             # and emit the data by call
116             $self->emit('data_out', $currency, $price);
117             }
118              
119             =head1 DESCRIPTION
120              
121             It is a base class. It set some helper attributes and methods, and have an timer event that can restart the connection.
122             You just need to override the method 'go' to connect to the site.
123              
124             =head1 ATTRIBUTES
125              
126             This class inherits all attributes from L<Mojo::EventEmitter> and add the following new ones:
127              
128             =head2 last_activity_at
129              
130             The time that the object receive the data from the server.
131             It is mainly updated by the method 'on_data_out'
132              
133             =head2 last_activity_period
134              
135             if time() - last_activity_at > last_activity_period, then we think the site is disconnected.
136              
137             =head2 timer
138              
139             The timer event to restart the connection
140              
141             =head2 started
142              
143             The tag that shows the site is running.
144              
145             =head2 site
146              
147             The site name which will be print in the debug information.
148              
149             =head1 METHODS
150              
151             =head2 new
152              
153             Create object and set some events and timer
154              
155             =head2 on_data_out
156              
157             the callback which will be called when receive the event 'data_out'.
158             It will Then emit the event 'output'
159              
160             The args of event data_out is:
161              
162             my ($self, $timestamp, $site, $currency, $price) = @_;
163              
164             The unit of timestamp is ms.
165              
166             =head2 timer_call_back
167              
168             The callback called by timer. It will emit event 'timeout' when timeout.
169              
170             =head2 set_timeout
171              
172             =head2 is_timeout
173              
174             =head2 on_timeout
175              
176             The callback of event 'timeout'. It will call method 'go' to restart the connection
177              
178             =head2 go
179              
180             Establish the connection.
181              
182             =head2 debug
183              
184             Print debug information if the envrionment variable 'FINANCE_BITCOIN_FEED_DEBUG' is set to true.
185              
186             =head2 error
187              
188             Print error information if there is error.
189              
190             =head1 EVENTS
191              
192             This class inherits all events from L<Mojo::EventEmitter> and add the following new ones:
193              
194             =head2 data_out
195              
196             It will be emitted by the site module when the site module want to output the data.
197              
198             =head2 output
199              
200             It will be emit by this module when print out the data. You can listen on this event to get the output.
201              
202             =head2 timeout
203              
204             It will be emit when the timer watch that the connection is timeout
205              
206             =head1 SEE ALSO
207              
208             L<Mojo::EventEmitter>
209              
210             L<Finance::Bitcoin::Feed>
211              
212             =head1 AUTHOR
213              
214             Chylli C<< <chylli@binary.com> >>
215