line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package AnyEvent::Campfire::Client; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$AnyEvent::Campfire::Client::VERSION = '0.0.2'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# Abstract: Campfire API in an event loop |
7
|
1
|
|
|
1
|
|
30121
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use namespace::autoclean; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use AnyEvent; |
11
|
|
|
|
|
|
|
use AnyEvent::HTTP::ScopedClient; |
12
|
|
|
|
|
|
|
use AnyEvent::Campfire::Stream; |
13
|
|
|
|
|
|
|
use URI; |
14
|
|
|
|
|
|
|
use MIME::Base64; |
15
|
|
|
|
|
|
|
use JSON::XS; |
16
|
|
|
|
|
|
|
use Try::Tiny; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
extends 'AnyEvent::Campfire'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has 'account' => ( |
21
|
|
|
|
|
|
|
is => 'ro', |
22
|
|
|
|
|
|
|
isa => 'Str' |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has 'uri' => ( |
26
|
|
|
|
|
|
|
is => 'ro', |
27
|
|
|
|
|
|
|
isa => 'URI', |
28
|
|
|
|
|
|
|
lazy_build => 1, |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub _build_uri { |
32
|
|
|
|
|
|
|
my $account = shift->account; |
33
|
|
|
|
|
|
|
return URI->new("https://$account.campfirenow.com/"); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub BUILD { |
37
|
|
|
|
|
|
|
my $self = shift; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
if ( !$self->authorization || !scalar @{ $self->rooms } || !$self->account ) |
40
|
|
|
|
|
|
|
{ |
41
|
|
|
|
|
|
|
print STDERR |
42
|
|
|
|
|
|
|
"Not enough parameters provided. I Need a token, rooms and account\n"; |
43
|
|
|
|
|
|
|
exit(1); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
for my $room ( @{ $self->rooms } ) { |
47
|
|
|
|
|
|
|
$self->post( |
48
|
|
|
|
|
|
|
"/room/$room/join", |
49
|
|
|
|
|
|
|
sub { |
50
|
|
|
|
|
|
|
my ( $body, $hdr ) = @_; |
51
|
|
|
|
|
|
|
if ( $hdr->{Status} !~ m/^2/ ) { |
52
|
|
|
|
|
|
|
$self->emit( 'error', "$hdr->{Status}: $hdr->{Reason}" ); |
53
|
|
|
|
|
|
|
return; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$self->emit( 'join', $room ); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
my $stream = AnyEvent::Campfire::Stream->new( |
59
|
|
|
|
|
|
|
token => $self->token, |
60
|
|
|
|
|
|
|
rooms => join( ',', @{ $self->rooms } ), |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
$stream->on( 'stream', $self->_events->{message}[0] ); |
64
|
|
|
|
|
|
|
$stream->on( 'error', $self->_events->{error}[0] ); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub speak { |
71
|
|
|
|
|
|
|
my ( $self, $room, $text ) = @_; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
$self->post( |
74
|
|
|
|
|
|
|
"/room/$room/speak", |
75
|
|
|
|
|
|
|
encode_json( { message => { body => $text } } ), |
76
|
|
|
|
|
|
|
sub { |
77
|
|
|
|
|
|
|
my ( $body, $hdr ) = @_; |
78
|
|
|
|
|
|
|
if ( !$body || $hdr->{Status} !~ m/^2/ ) { |
79
|
|
|
|
|
|
|
$self->emit( 'error', $hdr->{Reason} ); |
80
|
|
|
|
|
|
|
return; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub leave { |
87
|
|
|
|
|
|
|
my ( $self, $room ) = @_; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
$self->post( |
90
|
|
|
|
|
|
|
"/room/$room/leave", |
91
|
|
|
|
|
|
|
sub { |
92
|
|
|
|
|
|
|
my ( $body, $hdr ) = @_; |
93
|
|
|
|
|
|
|
if ( !$body || $hdr->{Status} !~ m/^2/ ) { |
94
|
|
|
|
|
|
|
$self->emit( 'error', "$hdr->{Status}: $hdr->{Reason}" ); |
95
|
|
|
|
|
|
|
return; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
$self->emit( 'leave', $room ); |
99
|
|
|
|
|
|
|
$self->emit('exit') if ( $room eq @{ $self->rooms }[-1] ); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub exit { |
105
|
|
|
|
|
|
|
my $self = shift; |
106
|
|
|
|
|
|
|
for my $room ( @{ $self->rooms } ) { |
107
|
|
|
|
|
|
|
$self->leave($room); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub get_account { |
112
|
|
|
|
|
|
|
my ( $self, $callback ) = @_; |
113
|
|
|
|
|
|
|
$self->get( '/account', $callback ); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub recent { |
117
|
|
|
|
|
|
|
my ( $self, $room, $opt, $callback ) = @_; |
118
|
|
|
|
|
|
|
return unless $room; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
if ( 'CODE' eq ref $opt ) { |
121
|
|
|
|
|
|
|
$callback = $opt; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
else { |
124
|
|
|
|
|
|
|
# limit, since_message_id |
125
|
|
|
|
|
|
|
$self->uri->query_form($opt); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
$self->get( "/room/$room/recent", $callback ); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub get_rooms { |
132
|
|
|
|
|
|
|
my ( $self, $callback ) = @_; |
133
|
|
|
|
|
|
|
$self->get( '/rooms', $callback ); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub put_room { |
137
|
|
|
|
|
|
|
my ( $self, $room, $room_info, $callback ) = @_; |
138
|
|
|
|
|
|
|
$room_info = encode_json($room_info) if ref($room_info) eq 'HASH'; |
139
|
|
|
|
|
|
|
$self->put( "/room/$room", $room_info, $callback ); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub lock { |
143
|
|
|
|
|
|
|
my ( $self, $room, $callback ) = @_; |
144
|
|
|
|
|
|
|
$self->post( "/room/$room/lock", $callback ); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub unlock { |
148
|
|
|
|
|
|
|
my ( $self, $room, $callback ) = @_; |
149
|
|
|
|
|
|
|
$self->post( "/room/$room/unlock", $callback ); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub request { |
153
|
|
|
|
|
|
|
my ( $self, $method, $path, $reqBody, $callback ) = @_; |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
$self->uri->path($path); |
156
|
|
|
|
|
|
|
my $scope = AnyEvent::HTTP::ScopedClient->new( $self->uri ); |
157
|
|
|
|
|
|
|
$scope->header( |
158
|
|
|
|
|
|
|
{ |
159
|
|
|
|
|
|
|
Authorization => $self->authorization, |
160
|
|
|
|
|
|
|
Accept => 'application/json', |
161
|
|
|
|
|
|
|
'Content-Type' => 'application/json', |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
)->request( $method, $reqBody, $callback ); |
164
|
|
|
|
|
|
|
$self->uri->query_form(''); # clear query |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub get { shift->request( 'GET', @_ ) } |
168
|
|
|
|
|
|
|
sub post { shift->request( 'POST', @_ ) } |
169
|
|
|
|
|
|
|
sub put { shift->request( 'PUT', @_ ) } |
170
|
|
|
|
|
|
|
sub delete { shift->request( 'DELETE', @_ ) } |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
1; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
1; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
__END__ |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=pod |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=encoding utf-8 |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head1 NAME |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
AnyEvent::Campfire::Client |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head1 VERSION |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
version 0.0.2 |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head1 SYNOPSIS |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
use AnyEvent::Campfire::Client; |
196
|
|
|
|
|
|
|
my $client = AnyEvent::Campfire::Client->new( |
197
|
|
|
|
|
|
|
token => 'xxxx', |
198
|
|
|
|
|
|
|
rooms => '1234', |
199
|
|
|
|
|
|
|
account => 'p5-hubot', |
200
|
|
|
|
|
|
|
); |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
$client->on( |
203
|
|
|
|
|
|
|
'join', |
204
|
|
|
|
|
|
|
sub { |
205
|
|
|
|
|
|
|
my ($e, $room_id) = @_; # $e is event emitter. please ignore it. |
206
|
|
|
|
|
|
|
$client->speak($room_id, "hi"); |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
); |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
$client->on( |
211
|
|
|
|
|
|
|
'message', |
212
|
|
|
|
|
|
|
sub { |
213
|
|
|
|
|
|
|
my ($e, $data) = @_; |
214
|
|
|
|
|
|
|
# ... |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
); |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
## want to exit? |
219
|
|
|
|
|
|
|
$client->exit; |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head1 AUTHOR |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
Hyungsuk Hong <hshong@perl.kr> |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
This software is copyright (c) 2012 by Hyungsuk Hong. |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
230
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=cut |