line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
TweetHook::API - |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use TweetHook::API; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my $th = TweetHook::API->new; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
foreach my $hook ( @{$th->list->{searches}} ) { |
12
|
|
|
|
|
|
|
print "$hook{id} $hook{search} $hook{webhook} $hook{active}\n"; |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
$th->start ( $id ); |
16
|
|
|
|
|
|
|
$th->stop ( $id ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$th->create ( 'search string', 'webhook url' ); |
19
|
|
|
|
|
|
|
$th->destroy ( $id ); |
20
|
|
|
|
|
|
|
$th->modify ( $id, { search => 'new search' } ); |
21
|
|
|
|
|
|
|
$th->modify ( $id, { webhook => 'new webhook' } ); |
22
|
|
|
|
|
|
|
$th->modify ( $id, { search => 'new search', webhook => 'new webhook' } ); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 DESCRIPTION |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=cut |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
package TweetHook::API; |
29
|
1
|
|
|
1
|
|
871719
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
34
|
|
30
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
31
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
73
|
|
32
|
1
|
|
|
1
|
|
1486340
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
2349250
|
|
|
1
|
|
|
|
|
37
|
|
33
|
1
|
|
|
1
|
|
770
|
use MIME::Base64; |
|
1
|
|
|
|
|
741
|
|
|
1
|
|
|
|
|
68
|
|
34
|
1
|
|
|
1
|
|
1484
|
use JSON; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
use fields ( 'format', 'username', 'password', 'basicauth' ); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
use Data::Dumper; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
our $apiroot = 'https://api.tweethook.com'; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
1; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub new { |
44
|
|
|
|
|
|
|
my ( $class, $un, $pw ) = @_; |
45
|
|
|
|
|
|
|
croak ( "Usage: $class->new ( username, password );" ) unless $un && $pw; |
46
|
|
|
|
|
|
|
my $self = {}; |
47
|
|
|
|
|
|
|
$self->{format} = 'json'; |
48
|
|
|
|
|
|
|
$self->{username} = $un; |
49
|
|
|
|
|
|
|
$self->{password} = $pw; |
50
|
|
|
|
|
|
|
$self->{basicauth} = 'Basic ' . MIME::Base64::encode ( "$un:$pw" ); |
51
|
|
|
|
|
|
|
$self->{ua} = LWP::UserAgent->new; |
52
|
|
|
|
|
|
|
bless $self, ref $class || $class; |
53
|
|
|
|
|
|
|
return $self; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub list { |
57
|
|
|
|
|
|
|
my $self = shift; |
58
|
|
|
|
|
|
|
my $resp = $self->{ua}->get ( $apiroot . "/list." . $self->{format}, |
59
|
|
|
|
|
|
|
Authorization => $self->{basicauth} ); |
60
|
|
|
|
|
|
|
return from_json ( $resp->content ) if $resp->is_success; |
61
|
|
|
|
|
|
|
return undef; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub info { |
65
|
|
|
|
|
|
|
my ( $self, @ids ) = @_; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
if ( scalar @ids == 0 ) { |
68
|
|
|
|
|
|
|
carp "info needs at least 1 id"; |
69
|
|
|
|
|
|
|
return; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
my @res; |
72
|
|
|
|
|
|
|
foreach my $id ( @ids ) { |
73
|
|
|
|
|
|
|
my $url = URI->new ( $apiroot . "/info.json" ); |
74
|
|
|
|
|
|
|
$url->query_form ( { id => $id } ); |
75
|
|
|
|
|
|
|
my $resp = $self->{ua}->get ( $url, Authorization => $self->{basicauth} ); |
76
|
|
|
|
|
|
|
if ( $resp->is_success ) { |
77
|
|
|
|
|
|
|
push @res, from_json ( $resp->content ); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
return undef if scalar @res == 0; |
81
|
|
|
|
|
|
|
return wantarray ? @res : $res[0]; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub do_post { |
85
|
|
|
|
|
|
|
my ( $self, $thmethod, $args ) = @_; |
86
|
|
|
|
|
|
|
my $uri = URI->new ( "http:" ); # just want to do url encoding |
87
|
|
|
|
|
|
|
$uri->query_form ( $args ); |
88
|
|
|
|
|
|
|
my $content = $uri->query; |
89
|
|
|
|
|
|
|
my $resp = $self->{ua}->request ( HTTP::Request->new ( "POST", |
90
|
|
|
|
|
|
|
"$apiroot$thmethod", |
91
|
|
|
|
|
|
|
[ Authorization => $self->{basicauth}, |
92
|
|
|
|
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded' ], |
93
|
|
|
|
|
|
|
$content ) ); |
94
|
|
|
|
|
|
|
return from_json ( $resp->content ) if $resp->is_success; |
95
|
|
|
|
|
|
|
return undef; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub start { |
99
|
|
|
|
|
|
|
my ( $self, $id ) = @_; |
100
|
|
|
|
|
|
|
if ( !$id ) { |
101
|
|
|
|
|
|
|
carp "start needs an id"; |
102
|
|
|
|
|
|
|
return undef; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
return $self->do_post ( '/start.json', { id => $id } ); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub stop { |
108
|
|
|
|
|
|
|
my ( $self, $id ) = @_; |
109
|
|
|
|
|
|
|
if ( !$id ) { |
110
|
|
|
|
|
|
|
carp "stop needs an id"; |
111
|
|
|
|
|
|
|
return undef; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
return $self->do_post ( '/stop.json', { id => $id } ); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub create { |
117
|
|
|
|
|
|
|
my ( $self, $search, $hook ) = @_; |
118
|
|
|
|
|
|
|
if ( !$search || !$hook ) { |
119
|
|
|
|
|
|
|
carp "create needs search and webhook"; |
120
|
|
|
|
|
|
|
return undef; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
return $self->do_post ( '/create.json', { search => $search, webhook => $hook } ); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub destroy { |
126
|
|
|
|
|
|
|
my ( $self, $id ) = @_; |
127
|
|
|
|
|
|
|
if ( !$id ) { |
128
|
|
|
|
|
|
|
carp "destroy needs an id"; |
129
|
|
|
|
|
|
|
return undef; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
return $self->do_post ( '/destroy.json', { id => $id } ); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub modify { |
135
|
|
|
|
|
|
|
my ( $self, $id, $args ) = @_; |
136
|
|
|
|
|
|
|
if ( !$id ) { |
137
|
|
|
|
|
|
|
carp "modify needs an id"; |
138
|
|
|
|
|
|
|
return undef; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
$args->{id} = $id; |
141
|
|
|
|
|
|
|
return $self->do_post ( '/modify.json', $args ); |
142
|
|
|
|
|
|
|
} |