| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package AnyEvent::WebService::ImKayac; |
|
2
|
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
30372
|
use strict; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
103
|
|
|
4
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
174
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
|
7
|
|
|
|
|
|
|
our $URL = "http://im.kayac.com"; |
|
8
|
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
3919
|
use AnyEvent::HTTP; |
|
|
3
|
|
|
|
|
138454
|
|
|
|
3
|
|
|
|
|
358
|
|
|
10
|
3
|
|
|
3
|
|
5531
|
use HTTP::Request::Common; |
|
|
3
|
|
|
|
|
140633
|
|
|
|
3
|
|
|
|
|
356
|
|
|
11
|
3
|
|
|
3
|
|
4240
|
use Digest::SHA qw/sha1_hex/; |
|
|
3
|
|
|
|
|
18299
|
|
|
|
3
|
|
|
|
|
375
|
|
|
12
|
3
|
|
|
3
|
|
3961
|
use JSON; |
|
|
3
|
|
|
|
|
63357
|
|
|
|
3
|
|
|
|
|
22
|
|
|
13
|
3
|
|
|
3
|
|
539
|
use Carp; |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
2123
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
AnyEvent::WebService::ImKayac - connection wrapper for im.kayac.com |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use AnyEvent::WebService::ImKayac; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $im = AnyEvent::WebService::ImKayac->new( |
|
24
|
|
|
|
|
|
|
type => 'password', |
|
25
|
|
|
|
|
|
|
user => '...', |
|
26
|
|
|
|
|
|
|
password => '...' |
|
27
|
|
|
|
|
|
|
); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
$im->send( message => 'Hello! test send!!', cb => sub { |
|
30
|
|
|
|
|
|
|
my ($hdr, $json, $reason) = @_; |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
if ( $json ) { |
|
33
|
|
|
|
|
|
|
if ( $json->{result} eq "posted" ) { |
|
34
|
|
|
|
|
|
|
} |
|
35
|
|
|
|
|
|
|
else { |
|
36
|
|
|
|
|
|
|
warn $json->{error}; |
|
37
|
|
|
|
|
|
|
} |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
else { |
|
40
|
|
|
|
|
|
|
warn $reason; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
}); |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head2 METHODS |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head3 new |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
You must pass C<< type >> and C<< user >> parameter to new method. And type should be |
|
49
|
|
|
|
|
|
|
secret, password or none. |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=over 3 |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item type is secret |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
You should pass secret_key parameter. |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=item type is password |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
You should pass password parameter. |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=item type is none |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
You dond need to pass other parameter. |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=back |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub new { |
|
70
|
6
|
|
|
6
|
1
|
8423
|
my $pkg = shift; |
|
71
|
6
|
100
|
|
|
|
34
|
my %args = ($_[1]) ? @_ : %{$_[1]}; |
|
|
1
|
|
|
|
|
12
|
|
|
72
|
|
|
|
|
|
|
|
|
73
|
5
|
50
|
|
|
|
19
|
croak "missing require parameter 'user'" unless defined $args{user}; |
|
74
|
5
|
50
|
|
|
|
15
|
croak "missing require parameter 'type'" unless defined $args{type}; |
|
75
|
|
|
|
|
|
|
|
|
76
|
5
|
50
|
|
|
|
34
|
$args{type} = 'none' if $args{type} !~ /^(none|password|secret)$/; |
|
77
|
|
|
|
|
|
|
|
|
78
|
5
|
100
|
100
|
|
|
27
|
if ($args{type} eq 'password' && ! defined $args{password}) { |
|
79
|
1
|
|
|
|
|
223
|
croak "require password"; |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
|
|
82
|
4
|
100
|
100
|
|
|
22
|
if ($args{type} eq 'secret' && ! defined $args{secret_key}) { |
|
83
|
1
|
|
|
|
|
136
|
croak "require secret_key"; |
|
84
|
|
|
|
|
|
|
} |
|
85
|
|
|
|
|
|
|
|
|
86
|
3
|
|
|
|
|
14
|
bless \%args, $pkg; |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head3 $imkayac->send( message => '...', cb => sub {} ); |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Send with message and cb parameters. cb is called when message have been sent. |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=cut |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub send { |
|
97
|
0
|
|
|
0
|
1
|
|
my ($self, %args) = @_; |
|
98
|
|
|
|
|
|
|
|
|
99
|
0
|
0
|
|
|
|
|
croak "missing required parameter 'message'" unless defined $args{message}; |
|
100
|
0
|
|
0
|
|
|
|
my $cb = delete $args{cb} || croak "missing required parameter 'cb'"; |
|
101
|
|
|
|
|
|
|
|
|
102
|
0
|
0
|
|
|
|
|
croak "parameter 'cb' should be coderef" unless ref $cb eq 'CODE'; |
|
103
|
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
my $user = $self->{user}; |
|
105
|
0
|
|
|
|
|
|
my $f = sprintf('_param_%s', $self->{type}); |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# from http://github.com/typester/irssi-plugins/blob/master/hilight2im.pl |
|
108
|
0
|
|
|
|
|
|
my $req = POST "$URL/api/post/${user}", [ $self->$f(%args) ]; |
|
109
|
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
my %headers = map { $_ => $req->header($_), } $req->headers->header_field_names; |
|
|
0
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# steal from AnyEvent::Twitter |
|
113
|
|
|
|
|
|
|
http_post $req->uri, $req->content, headers => \%headers, sub { |
|
114
|
0
|
|
|
0
|
|
|
my ($body, $hdr) = @_; |
|
115
|
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
local $@; |
|
117
|
0
|
|
|
|
|
|
my $json = eval { decode_json($body) }; |
|
|
0
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
|
119
|
0
|
0
|
|
|
|
|
if ( $hdr->{Status} =~ /^2/ ) { |
|
120
|
0
|
0
|
|
|
|
|
$cb->( $hdr, $json, $@ ? "parse error: $@" : $hdr->{Reason} ); |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
else { |
|
123
|
0
|
|
|
|
|
|
$cb->( $hdr, undef, $hdr->{Reason}, $json ); |
|
124
|
|
|
|
|
|
|
} |
|
125
|
0
|
|
|
|
|
|
}; |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub _param_none { |
|
130
|
0
|
|
|
0
|
|
|
my ($self, %args) = @_; |
|
131
|
0
|
|
|
|
|
|
%args; |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub _param_password { |
|
135
|
0
|
|
|
0
|
|
|
my ($self, %args) = @_; |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
( |
|
138
|
0
|
|
|
|
|
|
%args, |
|
139
|
|
|
|
|
|
|
password => $self->{password}, |
|
140
|
|
|
|
|
|
|
); |
|
141
|
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _param_secret { |
|
144
|
0
|
|
|
0
|
|
|
my ($self, %args) = @_; |
|
145
|
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
my $skey = $self->{secret_key}; |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
( |
|
149
|
0
|
|
|
|
|
|
%args, |
|
150
|
|
|
|
|
|
|
sig => sha1_hex($args{message} . $skey), |
|
151
|
|
|
|
|
|
|
); |
|
152
|
|
|
|
|
|
|
} |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
1; |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
__END__ |