File Coverage

blib/lib/AnyEvent/MyPeopleBot/Client.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package AnyEvent::MyPeopleBot::Client;
2             {
3             $AnyEvent::MyPeopleBot::Client::VERSION = '0.0.2';
4             }
5             # Abstract: MyPeopleBot API in an event loop
6              
7 1     1   28276 use Moose;
  0            
  0            
8             use namespace::autoclean;
9              
10             use AnyEvent;
11             use AnyEvent::HTTP::ScopedClient;
12              
13             has apikey => (
14             is => 'ro',
15             isa => 'Str',
16             required => 1,
17             );
18              
19             sub profile {
20             my ($self, $buddyId, $cb) = @_;
21              
22             my $client = AnyEvent::HTTP::ScopedClient->new("https://apis.daum.net/mypeople/profile/buddy.json?apikey=" . $self->apikey);
23             $client->header('Accept', 'application/json')
24             ->post(
25             { buddyId => $buddyId },
26             sub {
27             my ($body, $hdr) = @_;
28              
29             return if ( !$body || $hdr->{Status} !~ /^2/ );
30             print "$body\n" if $ENV{DEBUG};
31             $cb->($body) if $cb;
32             }
33             );
34             }
35              
36             sub buddys {
37             my ($self, $groupId, $cb) = @_;
38              
39             my $client = AnyEvent::HTTP::ScopedClient->new("https://apis.daum.net/mypeople/group/members.json?apikey=" . $self->apikey);
40             $client->header('Accept', 'application/json')
41             ->post(
42             { groupId => $groupId },
43             sub {
44             my ($body, $hdr) = @_;
45              
46             return if ( !$body || $hdr->{Status} !~ /^2/ );
47             print "$body\n" if $ENV{DEBUG};
48             $cb->($body) if $cb;
49             }
50             );
51             }
52              
53             sub send {
54             my ($self, $id, $msg, $cb) = @_;
55              
56             my $which = $id =~ /^B/ ? 'buddy' : 'group';
57             my %params = (
58             $which . 'Id' => $id,
59             content => $msg,
60             );
61              
62             my $client = AnyEvent::HTTP::ScopedClient->new("https://apis.daum.net/mypeople/$which/send.json?apikey=" . $self->apikey);
63             $client->header('Accept', 'application/json')
64             ->post(
65             \%params,
66             sub {
67             my ($body, $hdr) = @_;
68              
69             return if ( !$body || $hdr->{Status} !~ /^2/ );
70             print "$body\n" if $ENV{DEBUG};
71             $cb->($body) if $cb;
72             }
73             );
74             }
75              
76             sub exit {
77             my ($self, $groupId, $cb) = @_;
78              
79             my $client = AnyEvent::HTTP::ScopedClient->new("https://apis.daum.net/mypeople/group/exit.json?apikey=" . $self->apikey);
80             $client->header('Accept', 'application/json')
81             ->post(
82             { groupId => $groupId },
83             sub {
84             my ($body, $hdr) = @_;
85              
86             return if ( !$body || $hdr->{Status} !~ /^2/ );
87             print "$body\n" if $ENV{DEBUG};
88             $cb->($body) if $cb;
89             }
90             );
91             }
92              
93             __PACKAGE__->meta->make_immutable;
94              
95             1;
96              
97             __END__
98              
99             =pod
100              
101             =encoding utf-8
102              
103             =head1 NAME
104              
105             AnyEvent::MyPeopleBot::Client
106              
107             =head1 VERSION
108              
109             version 0.0.2
110              
111             =head1 SYNOPSIS
112              
113             use AnyEvent::HTTPD;
114             use AnyEvent::Mepeople::Client;
115             my $client = AnyEvent::MyPeopleBot::Client->new(
116             apikey => 'xxxx',
117             );
118              
119             my $httpd = AnyEvent::HTTPD->new(port => 8080);
120             $httpd->reg_cb(
121             '/' => sub {
122             my $action = $req->parm('action');
123             my $buddyId = $req->parm('buddyId');
124             my $groupId = $req->parm('groupId');
125             my $content = $req->parm('content');
126              
127             $req->respond({ content => [ 'text/plain', "AnyEvent::MyPeopleBot::Client" ]});
128             if ($action =~ /^sendFrom/) {
129             $client->send($buddyId || $groupId, 'hi', sub {
130             my $json = shift;
131             print "$json\n";
132             });
133             }
134             }
135             );
136              
137             $httpd->run;
138              
139             =head1 AUTHOR
140              
141             Hyungsuk Hong <hshong@perl.kr>
142              
143             =head1 COPYRIGHT AND LICENSE
144              
145             This software is copyright (c) 2013 by Hyungsuk Hong.
146              
147             This is free software; you can redistribute it and/or modify it under
148             the same terms as the Perl 5 programming language system itself.
149              
150             =cut