line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::ImKayac::Simple; |
2
|
2
|
|
|
2
|
|
6384
|
use 5.008005; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
68
|
|
3
|
2
|
|
|
2
|
|
20
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
53
|
|
4
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
68
|
|
5
|
2
|
|
|
2
|
|
9
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
132
|
|
6
|
2
|
|
|
2
|
|
1580
|
use Digest::SHA1 qw/sha1_hex/; |
|
2
|
|
|
|
|
1603
|
|
|
2
|
|
|
|
|
131
|
|
7
|
2
|
|
|
2
|
|
1930
|
use Encode qw/encode_utf8 decode_utf8/; |
|
2
|
|
|
|
|
36052
|
|
|
2
|
|
|
|
|
186
|
|
8
|
2
|
|
|
2
|
|
2187
|
use Furl; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use JSON (); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use constant IM_KAYAC_BASE_URL => 'http://im.kayac.com/api/post/'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
|
|
|
|
|
|
my ($class, @arg) = @_; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $user; |
19
|
|
|
|
|
|
|
my $password; |
20
|
|
|
|
|
|
|
my $type; |
21
|
|
|
|
|
|
|
if (scalar @arg == 1) { |
22
|
|
|
|
|
|
|
require YAML::Tiny; |
23
|
|
|
|
|
|
|
my $yaml_file = shift @arg; |
24
|
|
|
|
|
|
|
my $conf = YAML::Tiny->read($yaml_file)->[0]; |
25
|
|
|
|
|
|
|
$user = $conf->{user}; |
26
|
|
|
|
|
|
|
$type = $conf->{type} || ''; |
27
|
|
|
|
|
|
|
$password = $conf->{password} || ''; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
else { |
30
|
|
|
|
|
|
|
my %arg = @arg; |
31
|
|
|
|
|
|
|
$user = $arg{user}; |
32
|
|
|
|
|
|
|
$type = $arg{type} || ''; |
33
|
|
|
|
|
|
|
$password = $arg{password} || ''; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
croak '[ERROR] User name is required' unless $user; |
37
|
|
|
|
|
|
|
if ($type) { |
38
|
|
|
|
|
|
|
if ($type !~ /^(?:password|secret)$/) { |
39
|
|
|
|
|
|
|
croak "[ERROR] Invalid type: $type (type must be 'password' or 'secret')"; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
croak '[ERROR] Password is required' unless $password; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
bless { |
45
|
|
|
|
|
|
|
user => $user, |
46
|
|
|
|
|
|
|
password => $password, |
47
|
|
|
|
|
|
|
type => $type, |
48
|
|
|
|
|
|
|
furl => Furl->new( |
49
|
|
|
|
|
|
|
agent => 'WebService::ImKayac::Simple (Perl)', |
50
|
|
|
|
|
|
|
timeout => 10, |
51
|
|
|
|
|
|
|
), |
52
|
|
|
|
|
|
|
}, $class; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub send { |
56
|
|
|
|
|
|
|
my ($self, $message, $handler) = @_; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
croak '[ERROR] Message is required' unless $message; |
59
|
|
|
|
|
|
|
eval { $message = decode_utf8($message) }; |
60
|
|
|
|
|
|
|
$message = encode_utf8($message); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
my $param = {message => $message}; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
if (my $type = $self->{type}) { |
65
|
|
|
|
|
|
|
if ($type eq 'password') { |
66
|
|
|
|
|
|
|
$param->{password} = $self->{password}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
elsif ($type eq 'secret') { |
69
|
|
|
|
|
|
|
$param->{sig} = sha1_hex($message . $self->{password}); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
if ($handler) { |
74
|
|
|
|
|
|
|
$param->{handler} = $handler; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
my $res = $self->{furl}->post( |
78
|
|
|
|
|
|
|
IM_KAYAC_BASE_URL . $self->{user}, |
79
|
|
|
|
|
|
|
['Content-Type' => 'application/x-www-form-urlencoded'], |
80
|
|
|
|
|
|
|
$param, |
81
|
|
|
|
|
|
|
); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
unless ($res->is_success) { |
84
|
|
|
|
|
|
|
croak "[ERROR] " . $res->status_line; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
my $json = JSON::decode_json($res->{content}); |
88
|
|
|
|
|
|
|
if (my $error = $json->{error}) { |
89
|
|
|
|
|
|
|
croak "[ERROR] $error"; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
__END__ |