Twitter.pm

filter: Statementstmt 60.8% 227 / 373 Branchbran 34.1% 58 / 170 Conditioncond 26.0% 19 / 73 MC/DCmcdc 2.6% 1 / 38 Subroutinesub 52.2% 46 / 88 Podpod 100% 21 / 21 totaltotal 48.7% 372 / 763 CC 123 SCAR 21.5
CC
123
Cov
49%
_build_retweet
30.0
_build_search_tweets
27.1
authentication
22.5
CRAP 8.5
« Twitter/Error.pm ↑ Summary Twitter/Tweet.pm »
1 package Mojo::WebService::Twitter;
2 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Mojo::Util qw(b64_encode encode url_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Mojo::WebService::Twitter::Error 'twitter_tx_error';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Mojo::WebService::Twitter::Tweet;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Mojo::WebService::Twitter::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Mojo::WebService::Twitter::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use WWW::OAuth;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our $VERSION = '1.003';
16  
17 2 has ['api_key','api_secret'];
18 2 ▶has 'ua' => sub { Mojo::UserAgent->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
19  
20 12 ▶sub authentication {
Subroutine authentication: called CC=10 SCAR=31.1
Pod documented
21 12 my $self = shift;
22 12 ▶ return $self->{authentication} // croak 'No authentication set' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 10 2 12
Condition100%2/2
$self->{"authentication"} // croak("No authentication set")
l!l
91
MC/DC100%1/1
$self->{"authentication"} // croak("No authentication set")
$self->{"authentication"}
Truth table100%2/2
A
A= $self->{"authentication"}
Aresult
11
00
23 2 my $auth = shift;
24 2 ▶ if (ref $auth eq 'CODE') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $auth eq 'CODE') { } 0 2 2
elsif (ref $auth eq 'HASH') { } 0 2 2
elsif ($auth eq 'oauth') { } 1 1 2
elsif ($auth eq 'oauth2') { } 1 0 1
25 0 $self->{authentication} = $auth;
26 } elsif (ref $auth eq 'HASH') {
27 0 ▶ if (defined $auth->{access_token}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $auth->{'access_token'}) { } 0 0 0
elsif (defined $auth->{'oauth_token'} and defined $auth->{'oauth_token_secret'}) { } 0 0 0
28 0 $self->{authentication} = $self->_oauth2($auth->{access_token});
29 ▶ } elsif (defined $auth->{oauth_token} and defined $auth->{oauth_token_secret}) {
Condition0%0/3
defined $auth->{"oauth_token"} and defined $auth->{"oauth_token_secret"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $auth->{"oauth_token"} and defined $auth->{"oauth_token_secret"}
defined $auth->{"oauth_token"} defined $auth->{"oauth_token_secret"}
Truth table0%0/3
A and B
A= defined $auth->{"oauth_token"}B= defined $auth->{"oauth_token_secret"}
ABresult
0X0
100
111
30 0 $self->{authentication} = $self->_oauth($auth->{oauth_token}, $auth->{oauth_token_secret});
31 } else {
32 0 croak 'Unrecognized authentication hashref (no oauth_token or access_token)';
33 }
34 } elsif ($auth eq 'oauth') {
35 1 $self->{authentication} = $self->_oauth(@_);
36 } elsif ($auth eq 'oauth2') {
37 1 $self->{authentication} = $self->_oauth2(@_);
38 } else {
39 0 croak "Unknown authentication $auth";
40 }
41 2 return $self;
42 }
43  
44 1 ▶sub request_oauth {
Subroutine request_oauth: called CC=5 SCAR=21.9
Pod documented
45 1 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 1 1
46 1 my $self = shift;
47 1 my $tx = $self->_build_request_oauth(@_);
48 1 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 0 1 1
49 0 ▶ $self->ua->start($tx, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
50 0 my ($ua, $tx) = @_;
51 0 ▶ return $self->$cb(twitter_tx_error($tx)) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
52 0 ▶ my $res = $self->_from_request_oauth($tx) // return $self->$cb('OAuth callback was not confirmed');
Condition0%0/2
$self->_from_request_oauth($tx) // (return $self->$cb("OAuth callback was not confirmed"))
l!l
00
MC/DC0%0/1
$self->_from_request_oauth($tx) // (return $self->$cb("OAuth callback was not confirmed"))
$self->_from_request_oauth($tx)
Truth table0%0/2
A
A= $self->_from_request_oauth($tx)
Aresult
11
00
53 0 $self->$cb(undef, $res);
54 0 });
55 } else {
56 1 $tx = $self->ua->start($tx);
57 1 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 1 1
58 1 ▶ return $self->_from_request_oauth($tx) // die "OAuth callback was not confirmed\n";
Condition50%1/2
$self->_from_request_oauth($tx) // die("OAuth callback was not confirmed\n")
l!l
10
MC/DC0%0/1
$self->_from_request_oauth($tx) // die("OAuth callback was not confirmed\n")
$self->_from_request_oauth($tx)
Truth table50%1/2
A
A= $self->_from_request_oauth($tx)
Aresult
11
00
59 }
60 }
61  
62 0 ▶sub request_oauth_p {
Subroutine request_oauth_p: not called CC=1 SCAR=6.9
Pod documented
63 0 my $self = shift;
64 0 my $tx = $self->_build_request_oauth(@_);
65 0 ▶ return $self->ua->start_p($tx)->then(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
66 0 my ($tx) = @_;
67 0 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
68 0 ▶ return $self->_from_request_oauth($tx) // die "OAuth callback was not confirmed\n";
Condition0%0/2
$self->_from_request_oauth($tx) // die("OAuth callback was not confirmed\n")
l!l
00
MC/DC0%0/1
$self->_from_request_oauth($tx) // die("OAuth callback was not confirmed\n")
$self->_from_request_oauth($tx)
Truth table0%0/2
A
A= $self->_from_request_oauth($tx)
Aresult
11
00
69 0 ▶ }, sub { die Mojo::WebService::Twitter::Error->new(connection_error => $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 }
71  
72 1 ▶sub _build_request_oauth {
Subroutine _build_request_oauth: called CC=2 SCAR=7.0
73 1 my ($self, $url) = @_;
74 1 ▶ $url //= 'oob';
Condition50%1/2
$url //= "oob"
l!l
01
MC/DC0%0/1
$url //= "oob"
$url
Truth table50%1/2
A
A= $url
Aresult
11
00
75 1 my $tx = $self->ua->build_tx(POST => _oauth_url('request_token'));
76 1 $self->_oauth->($tx->req, { oauth_callback => $url });
77 1 return $tx;
78 }
79  
80 1 ▶sub _from_request_oauth {
Subroutine _from_request_oauth: called CC=4 SCAR=15.9
81 1 my ($self, $tx) = @_;
82 1 my $params = Mojo::Parameters->new($tx->res->text)->to_hash;
83 return undef unless $params->{oauth_callback_confirmed} eq 'true'
84 1 ▶ and defined $params->{oauth_token} and defined $params->{oauth_token_secret};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"oauth_callback_confirmed"} eq "true" and defined $params->{"oauth_token"} and defined $params->{"oauth_token_secret"} 0 1 1
Condition33%2/6
$params->{"oauth_callback_confirmed"} eq "true" and defined $params->{"oauth_token"} and defined $params->{"oauth_token_secret"}
!ll&&!rl&&r
001
$params->{"oauth_callback_confirmed"} eq "true" and defined $params->{"oauth_token"}
!ll&&!rl&&r
001
MC/DC0%0/3
$params->{"oauth_callback_confirmed"} eq "true" and defined $params->{"oauth_token"} and defined $params->{"oauth_token_secret"}
$params->{"oauth_callback_confirmed"} eq "true" defined $params->{"oauth_token"} defined $params->{"oauth_token_secret"}
Truth table25%1/4
A and B and C
A= $params->{"oauth_callback_confirmed"} eq "true"B= defined $params->{"oauth_token"}C= defined $params->{"oauth_token_secret"}
ABCresult
0XX0
10X0
1100
1111
85 1 $self->{request_token_secrets}{$params->{oauth_token}} = $params->{oauth_token_secret};
86 1 return $params;
87 }
88  
89 1 ▶sub verify_oauth {
Subroutine verify_oauth: called CC=5 SCAR=21.9
Pod documented
90 1 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 1 1
91 1 my $self = shift;
92 1 my $tx = $self->_build_verify_oauth(@_);
93 1 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 0 1 1
94 0 ▶ $self->ua->start($tx, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
95 0 my ($ua, $tx) = @_;
96 0 ▶ return $self->$cb(twitter_tx_error($tx)) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
97 0 ▶ my $res = $self->_from_verify_oauth($tx) // return $self->$cb('No OAuth token returned');
Condition0%0/2
$self->_from_verify_oauth($tx) // (return $self->$cb("No OAuth token returned"))
l!l
00
MC/DC0%0/1
$self->_from_verify_oauth($tx) // (return $self->$cb("No OAuth token returned"))
$self->_from_verify_oauth($tx)
Truth table0%0/2
A
A= $self->_from_verify_oauth($tx)
Aresult
11
00
98 0 $self->$cb(undef, $res);
99 0 });
100 } else {
101 1 $tx = $self->ua->start($tx);
102 1 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 1 1
103 1 ▶ return $self->_from_verify_oauth($tx) // die "No OAuth token returned\n";
Condition50%1/2
$self->_from_verify_oauth($tx) // die("No OAuth token returned\n")
l!l
10
MC/DC0%0/1
$self->_from_verify_oauth($tx) // die("No OAuth token returned\n")
$self->_from_verify_oauth($tx)
Truth table50%1/2
A
A= $self->_from_verify_oauth($tx)
Aresult
11
00
104 }
105 }
106  
107 0 ▶sub verify_oauth_p {
Subroutine verify_oauth_p: not called CC=1 SCAR=6.9
Pod documented
108 0 my $self = shift;
109 0 my $tx = $self->_build_verify_oauth(@_);
110 0 ▶ return $self->ua->start_p($tx)->then(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
111 0 my ($tx) = @_;
112 0 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
113 0 ▶ return $self->_from_verify_oauth($tx) // die "No OAuth token returned\n";
Condition0%0/2
$self->_from_verify_oauth($tx) // die("No OAuth token returned\n")
l!l
00
MC/DC0%0/1
$self->_from_verify_oauth($tx) // die("No OAuth token returned\n")
$self->_from_verify_oauth($tx)
Truth table0%0/2
A
A= $self->_from_verify_oauth($tx)
Aresult
11
00
114 0 ▶ }, sub { die Mojo::WebService::Twitter::Error->new(connection_error => $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 }
116  
117 1 ▶sub _build_verify_oauth {
Subroutine _build_verify_oauth: called CC=3 SCAR=11.3
118 1 my ($self, $verifier, $request_token, $request_token_secret) = @_;
119 1 ▶ $request_token_secret //= delete $self->{request_token_secrets}{$request_token} // croak "Unknown request token";
Condition50%2/4
$request_token_secret //= delete $self->{"request_token_secrets"}{$request_token} // croak("Unknown request token")
l!l
01
delete $self->{"request_token_secrets"}{$request_token} // croak("Unknown request token")
l!l
10
MC/DC0%0/1
$request_token_secret //= delete $self->{"request_token_secrets"}{$request_token} // croak("Unknown request token")
$request_token_secret
Truth table50%1/2
A
A= $request_token_secret
Aresult
11
00
120 1 my $tx = $self->ua->build_tx(POST => _oauth_url('access_token'));
121 1 $self->_oauth($request_token, $request_token_secret)->($tx->req, { oauth_verifier => $verifier });
122 1 return $tx;
123 }
124  
125 1 ▶sub _from_verify_oauth {
Subroutine _from_verify_oauth: called CC=3 SCAR=12.0
126 1 my ($self, $tx) = @_;
127 1 my $params = Mojo::Parameters->new($tx->res->text)->to_hash;
128 1 ▶ return undef unless defined $params->{'oauth_token'} and defined $params->{'oauth_token_secret'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{"oauth_token"} and defined $params->{"oauth_token_secret"} 0 1 1
Condition33%1/3
defined $params->{"oauth_token"} and defined $params->{"oauth_token_secret"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $params->{"oauth_token"} and defined $params->{"oauth_token_secret"}
defined $params->{"oauth_token"} defined $params->{"oauth_token_secret"}
Truth table33%1/3
A and B
A= defined $params->{"oauth_token"}B= defined $params->{"oauth_token_secret"}
ABresult
0X0
100
111
129 1 return $params;
130 }
131  
132 1 ▶sub request_oauth2 {
Subroutine request_oauth2: called CC=5 SCAR=21.9
Pod documented
133 1 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 1 1
134 1 my $self = shift;
135 1 my $tx = $self->_build_request_oauth2(@_);
136 1 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 0 1 1
137 0 ▶ $self->ua->start($tx, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
138 0 my ($ua, $tx) = @_;
139 0 ▶ return $self->$cb(twitter_tx_error($tx)) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
140 0 ▶ my $res = $self->_from_request_oauth2($tx) // return $self->$cb('No bearer token returned');
Condition0%0/2
$self->_from_request_oauth2($tx) // (return $self->$cb("No bearer token returned"))
l!l
00
MC/DC0%0/1
$self->_from_request_oauth2($tx) // (return $self->$cb("No bearer token returned"))
$self->_from_request_oauth2($tx)
Truth table0%0/2
A
A= $self->_from_request_oauth2($tx)
Aresult
11
00
141 0 $self->$cb(undef, $res);
142 0 });
143 } else {
144 1 $tx = $self->ua->start($tx);
145 1 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 1 1
146 1 ▶ return $self->_from_request_oauth2($tx) // die "No bearer token returned\n";
Condition50%1/2
$self->_from_request_oauth2($tx) // die("No bearer token returned\n")
l!l
10
MC/DC0%0/1
$self->_from_request_oauth2($tx) // die("No bearer token returned\n")
$self->_from_request_oauth2($tx)
Truth table50%1/2
A
A= $self->_from_request_oauth2($tx)
Aresult
11
00
147 }
148 }
149  
150 0 ▶sub request_oauth2_p {
Subroutine request_oauth2_p: not called CC=1 SCAR=6.9
Pod documented
151 0 my $self = shift;
152 0 my $tx = $self->_build_request_oauth2(@_);
153 0 ▶ return $self->ua->start_p($tx)->then(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
154 0 my ($tx) = @_;
155 0 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
156 0 ▶ return $self->_from_request_oauth2($tx) // die "No bearer token returned\n";
Condition0%0/2
$self->_from_request_oauth2($tx) // die("No bearer token returned\n")
l!l
00
MC/DC0%0/1
$self->_from_request_oauth2($tx) // die("No bearer token returned\n")
$self->_from_request_oauth2($tx)
Truth table0%0/2
A
A= $self->_from_request_oauth2($tx)
Aresult
11
00
157 0 ▶ }, sub { die Mojo::WebService::Twitter::Error->new(connection_error => $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 }
159  
160 1 ▶sub _build_request_oauth2 {
Subroutine _build_request_oauth2: called CC=1 SCAR=0.0
161 1 my ($self) = @_;
162 1 my $tx = $self->ua->build_tx(POST => _oauth2_url('token'), form => { grant_type => 'client_credentials' });
163 1 $self->_oauth2_request->($tx->req);
164 1 return $tx;
165 }
166  
167 1 ▶sub _from_request_oauth2 {
Subroutine _from_request_oauth2: called CC=3 SCAR=11.4
168 1 my ($self, $tx) = @_;
169 1 ▶ my $params = $tx->res->json // {};
Condition50%1/2
$tx->res->json // {}
l!l
10
MC/DC0%0/1
$tx->res->json // {}
$tx->res->json
Truth table50%1/2
A
A= $tx->res->json
Aresult
11
00
170 1 ▶ return undef unless defined $params->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{"access_token"} 0 1 1
171 1 return $params;
172 }
173  
174 4 ▶sub get_tweet {
Subroutine get_tweet: called CC=4 SCAR=17.4
Pod documented
175 4 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 4 4
176 4 my $self = shift;
177 4 my $tx = $self->_build_get_tweet(@_);
178 3 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 0 3 3
179 0 ▶ $self->ua->start($tx, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
180 0 my ($ua, $tx) = @_;
181 0 ▶ return $self->$cb(twitter_tx_error($tx)) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
182 0 $self->$cb(undef, _tweet_object($tx->res->json));
183 0 });
184 } else {
185 3 $tx = $self->ua->start($tx);
186 3 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 3 3
187 3 return _tweet_object($tx->res->json);
188 }
189 }
190  
191 0 ▶sub get_tweet_p {
Subroutine get_tweet_p: not called CC=1 SCAR=6.9
Pod documented
192 0 my $self = shift;
193 0 my $tx = $self->_build_get_tweet(@_);
194 0 ▶ return $self->ua->start_p($tx)->then(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
195 0 my ($tx) = @_;
196 0 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
197 0 return _tweet_object($tx->res->json);
198 0 ▶ }, sub { die Mojo::WebService::Twitter::Error->new(connection_error => $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
199 }
200  
201 4 ▶sub _build_get_tweet {
Subroutine _build_get_tweet: called CC=3 SCAR=11.2
202 4 my ($self, $id) = @_;
203 4 ▶ croak 'Tweet ID is required for get_tweet' unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 4 4
204 4 ▶ croak 'Invalid tweet ID to retrieve' unless $id =~ m/\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A[0-9]+\z/u 0 4 4
205 4 my $tx = $self->ua->build_tx(GET => _api_url('statuses/show.json')
206 ->query(_www_form_urlencode(id => $id, tweet_mode => 'extended')));
207 4 $self->authentication->($tx->req);
208 3 return $tx;
209 }
210  
211 2 ▶sub get_user {
Subroutine get_user: called CC=4 SCAR=17.4
Pod documented
212 2 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 2 2
213 2 my $self = shift;
214 2 my $tx = $self->_build_get_user(@_);
215 2 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 0 2 2
216 0 ▶ $self->ua->start($tx, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
217 0 my ($ua, $tx) = @_;
218 0 ▶ return $self->$cb(twitter_tx_error($tx)) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
219 0 $self->$cb(undef, _user_object($tx->res->json));
220 0 });
221 } else {
222 2 $tx = $self->ua->start($tx);
223 2 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 2 2
224 2 return _user_object($tx->res->json);
225 }
226 }
227  
228 0 ▶sub get_user_p {
Subroutine get_user_p: not called CC=1 SCAR=6.9
Pod documented
229 0 my $self = shift;
230 0 my $tx = $self->_build_get_user(@_);
231 0 ▶ return $self->ua->start_p($tx)->then(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
232 0 my ($tx) = @_;
233 0 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
234 0 return _user_object($tx->res->json);
235 0 ▶ }, sub { die Mojo::WebService::Twitter::Error->new(connection_error => $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
236 }
237  
238 2 ▶sub _build_get_user {
Subroutine _build_get_user: called CC=4 SCAR=13.9
239 2 my ($self, %params) = @_;
240 2 my %query;
241 2 ▶ $query{user_id} = $params{user_id} if defined $params{user_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"user_id"} 1 1 2
242 2 ▶ $query{screen_name} = $params{screen_name} if defined $params{screen_name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"screen_name"} 1 1 2
243 2 ▶ croak 'user_id or screen_name is required for get_user' unless %query;
Branch: 1 branch
BranchTrueFalseTotal
unless %query 0 2 2
244 2 $query{tweet_mode} = 'extended';
245 2 my $tx = $self->ua->build_tx(GET => _api_url('users/show.json')->query(_www_form_urlencode(%query)));
246 2 $self->authentication->($tx->req);
247 2 return $tx;
248 }
249  
250 1 ▶sub get_user_timeline {
Subroutine get_user_timeline: called CC=5 SCAR=21.4
Pod documented
251 1 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 1 1
252 1 my $self = shift;
253 1 my $tx = $self->_build_get_user_timeline(@_);
254 1 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 0 1 1
255 0 ▶ $self->ua->start($tx, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
256 0 my ($ua, $tx) = @_;
257 0 ▶ return $self->$cb(twitter_tx_error($tx)) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
258 0 ▶ $self->$cb(undef, Mojo::Collection->new(map { _tweet_object($_) } @{$tx->res->json // []}));
Condition0%0/2
$tx->res->json // []
l!l
00
MC/DC0%0/1
$tx->res->json // []
$tx->res->json
Truth table0%0/2
A
A= $tx->res->json
Aresult
11
00
259 0 });
260 } else {
261 1 $tx = $self->ua->start($tx);
262 1 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 1 1
263 1 ▶ return Mojo::Collection->new(map { _tweet_object($_) } @{$tx->res->json // []});
Condition50%1/2
$tx->res->json // []
l!l
10
MC/DC0%0/1
$tx->res->json // []
$tx->res->json
Truth table50%1/2
A
A= $tx->res->json
Aresult
11
00
264 }
265 }
266  
267 0 ▶sub get_user_timeline_p {
Subroutine get_user_timeline_p: not called CC=1 SCAR=6.9
Pod documented
268 0 my $self = shift;
269 0 my $tx = $self->_build_get_user_timeline(@_);
270 0 ▶ return $self->ua->start_p($tx)->then(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
271 0 my ($tx) = @_;
272 0 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
273 0 ▶ return Mojo::Collection->new(map { _tweet_object($_) } @{$tx->res->json // []});
Condition0%0/2
$tx->res->json // []
l!l
00
MC/DC0%0/1
$tx->res->json // []
$tx->res->json
Truth table0%0/2
A
A= $tx->res->json
Aresult
11
00
274 0 ▶ }, sub { die Mojo::WebService::Twitter::Error->new(connection_error => $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
275 }
276  
277 1 ▶sub _build_get_user_timeline {
Subroutine _build_get_user_timeline: called CC=7 SCAR=20.1
278 1 my ($self, %params) = @_;
279 1 my %query;
280 1 ▶ $query{user_id} = $params{user_id} if defined $params{user_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"user_id"} 1 0 1
281 1 ▶ $query{screen_name} = $params{screen_name} if defined $params{screen_name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"screen_name"} 0 1 1
282 1 ▶ croak 'user_id or screen_name is required for get_user_timeline' unless %query;
Branch: 1 branch
BranchTrueFalseTotal
unless %query 0 1 1
283 1 $query{$_} = $params{$_} for grep { defined $params{$_} } qw(count since_id max_id);
284 1 ▶ $query{exclude_replies} = $params{exclude_replies} ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$params{'exclude_replies'} ? : 0 1 1
285 1 ▶ $query{include_rts} = $params{exclude_rts} ? 'false' : 'true';
Branch: 1 branch
BranchTrueFalseTotal
$params{'exclude_rts'} ? : 0 1 1
286 1 $query{trim_user} = 'true';
287 1 $query{tweet_mode} = 'extended';
288 1 my $tx = $self->ua->build_tx(GET => _api_url('statuses/user_timeline.json')->query(_www_form_urlencode(%query)));
289 1 $self->authentication->($tx->req);
290 1 return $tx;
291 }
292  
293 1 ▶sub post_tweet {
Subroutine post_tweet: called CC=4 SCAR=17.4
Pod documented
294 1 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 1 1
295 1 my $self = shift;
296 1 my $tx = $self->_build_post_tweet(@_);
297 1 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 0 1 1
298 0 ▶ $self->ua->start($tx, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
299 0 my ($ua, $tx) = @_;
300 0 ▶ return $self->$cb(twitter_tx_error($tx)) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
301 0 $self->$cb(undef, _tweet_object($tx->res->json));
302 0 });
303 } else {
304 1 $tx = $self->ua->start($tx);
305 1 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 1 1
306 1 return _tweet_object($tx->res->json);
307 }
308 }
309  
310 0 ▶sub post_tweet_p {
Subroutine post_tweet_p: not called CC=1 SCAR=6.9
Pod documented
311 0 my $self = shift;
312 0 my $tx = $self->_build_post_tweet(@_);
313 0 ▶ return $self->ua->start_p($tx)->then(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
314 0 my ($tx) = @_;
315 0 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
316 0 return _tweet_object($tx->res->json);
317 0 ▶ }, sub { die Mojo::WebService::Twitter::Error->new(connection_error => $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
318 }
319  
320 1 ▶sub _build_post_tweet {
Subroutine _build_post_tweet: called CC=5 SCAR=16.4
321 1 my ($self, $status, %params) = @_;
322 1 ▶ croak 'Status text is required to post a tweet' unless defined $status;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status 0 1 1
323 1 my %form;
324 1 $form{status} = $status;
325 1 $form{$_} = $params{$_} for grep { defined $params{$_} }
326 qw(in_reply_to_status_id lat long place_id);
327 1 ▶ $form{$_} = $params{$_} ? 'true' : 'false' for grep { defined $params{$_} }
Branch: 1 branch
BranchTrueFalseTotal
$params{$_} ? : 0 0 0
328 qw(display_coordinates);
329 1 $form{tweet_mode} = 'extended';
330 1 my $tx = $self->ua->build_tx(POST => _api_url('statuses/update.json'),
331 {'Content-Type' => 'application/x-www-form-urlencoded'}, _www_form_urlencode(%form));
332 1 $self->authentication->($tx->req);
333 1 return $tx;
334 }
335  
336 0 ▶sub retweet {
Subroutine retweet: not called CC=4 SCAR=30.0
Pod documented
337 0 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 0 0
338 0 my $self = shift;
339 0 my $tx = $self->_build_retweet(@_);
340 0 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 0 0 0
341 0 ▶ $self->ua->start($tx, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
342 0 my ($ua, $tx) = @_;
343 0 ▶ return $self->$cb(twitter_tx_error($tx)) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
344 0 $self->$cb(undef, _tweet_object($tx->res->json));
345 0 });
346 } else {
347 0 $tx = $self->ua->start($tx);
348 0 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
349 0 return _tweet_object($tx->res->json);
350 }
351 }
352  
353 0 ▶sub retweet_p {
Subroutine retweet_p: not called CC=1 SCAR=6.9
Pod documented
354 0 my $self = shift;
355 0 my $tx = $self->_build_retweet(@_);
356 0 ▶ return $self->ua->start_p($tx)->then(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
357 0 my ($tx) = @_;
358 0 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
359 0 return _tweet_object($tx->res->json);
360 0 ▶ }, sub { die Mojo::WebService::Twitter::Error->new(connection_error => $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
361 }
362  
363 0 ▶sub _build_retweet {
Subroutine _build_retweet: not called CC=5 SCAR=34.0
364 0 my ($self, $id) = @_;
365 0 ▶ croak 'Tweet ID is required for retweet' unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
366 0 ▶ $id = $id->id if blessed $id and $id->isa('Mojo::WebService::Twitter::Tweet');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($id) and $id->isa("Mojo::WebService::Twitter::Tweet") 0 0 0
Condition0%0/3
builtin::blessed($id) and $id->isa("Mojo::WebService::Twitter::Tweet")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($id) and $id->isa("Mojo::WebService::Twitter::Tweet")
builtin::blessed($id) $id->isa("Mojo::WebService::Twitter::Tweet")
Truth table0%0/3
A and B
A= builtin::blessed($id)B= $id->isa("Mojo::WebService::Twitter::Tweet")
ABresult
0X0
100
111
367 0 ▶ croak 'Invalid tweet ID to retweet' unless $id =~ m/\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /\A[0-9]+\z/u 0 0 0
368 0 my $tx = $self->ua->build_tx(POST => _api_url("statuses/retweet/$id.json")->query(tweet_mode => 'extended'));
369 0 $self->authentication->($tx->req);
370 0 return $tx;
371 }
372  
373 1 ▶sub search_tweets {
Subroutine search_tweets: called CC=5 SCAR=21.4
Pod documented
374 1 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 1 1
375 1 my $self = shift;
376 1 my $tx = $self->_build_search_tweets(@_);
377 1 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 0 1 1
378 0 ▶ $self->ua->start($tx, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
379 0 my ($ua, $tx) = @_;
380 0 ▶ return $self->$cb(twitter_tx_error($tx)) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
381 0 ▶ $self->$cb(undef, Mojo::Collection->new(map { _tweet_object($_) } @{$tx->res->json->{statuses} // []}));
Condition0%0/2
$tx->res->json->{"statuses"} // []
l!l
00
MC/DC0%0/1
$tx->res->json->{"statuses"} // []
$tx->res->json->{"statuses"}
Truth table0%0/2
A
A= $tx->res->json->{"statuses"}
Aresult
11
00
382 0 });
383 } else {
384 1 $tx = $self->ua->start($tx);
385 1 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 1 1
386 1 ▶ return Mojo::Collection->new(map { _tweet_object($_) } @{$tx->res->json->{statuses} // []});
Condition50%1/2
$tx->res->json->{"statuses"} // []
l!l
10
MC/DC0%0/1
$tx->res->json->{"statuses"} // []
$tx->res->json->{"statuses"}
Truth table50%1/2
A
A= $tx->res->json->{"statuses"}
Aresult
11
00
387 }
388 }
389  
390 0 ▶sub search_tweets_p {
Subroutine search_tweets_p: not called CC=1 SCAR=6.9
Pod documented
391 0 my $self = shift;
392 0 my $tx = $self->_build_search_tweets(@_);
393 0 ▶ return $self->ua->start_p($tx)->then(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
394 0 my ($tx) = @_;
395 0 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
396 0 ▶ return Mojo::Collection->new(map { _tweet_object($_) } @{$tx->res->json->{statuses} // []});
Condition0%0/2
$tx->res->json->{"statuses"} // []
l!l
00
MC/DC0%0/1
$tx->res->json->{"statuses"} // []
$tx->res->json->{"statuses"}
Truth table0%0/2
A
A= $tx->res->json->{"statuses"}
Aresult
11
00
397 0 ▶ }, sub { die Mojo::WebService::Twitter::Error->new(connection_error => $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
398 }
399  
400 1 ▶sub _build_search_tweets {
Subroutine _build_search_tweets: called CC=10 SCAR=33.0
401 1 my ($self, $q, %params) = @_;
402 1 ▶ croak 'Search query is required for search_tweets' unless defined $q;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $q 0 1 1
403 1 my %query;
404 1 $query{q} = $q;
405 1 my $geocode = $params{geocode};
406 1 ▶ if (ref $geocode) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $geocode) 0 1 1
407 0 my ($lat, $long, $rad);
408 0 ▶ ($lat, $long, $rad) = @$geocode if ref $geocode eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $geocode eq "ARRAY" 0 0 0
409 0 ▶ ($lat, $long, $rad) = @{$geocode}{'latitude','longitude','radius'} if ref $geocode eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $geocode eq "HASH" 0 0 0
410 0 ▶ $geocode = "$lat,$long,$rad" if defined $lat and defined $long and defined $rad;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lat and defined $long and defined $rad 0 0 0
Condition0%0/6
defined $lat and defined $long and defined $rad
!ll&&!rl&&r
000
defined $lat and defined $long
!ll&&!rl&&r
000
MC/DC0%0/3
defined $lat and defined $long and defined $rad
defined $lat defined $long defined $rad
Truth table0%0/4
A and B and C
A= defined $latB= defined $longC= defined $rad
ABCresult
0XX0
10X0
1100
1111
411 }
412 1 ▶ $query{geocode} = $geocode if defined $geocode;
Branch: 1 branch
BranchTrueFalseTotal
if defined $geocode 0 1 1
413 1 $query{$_} = $params{$_} for grep { defined $params{$_} } qw(lang result_type count until since_id max_id);
414 1 $query{tweet_mode} = 'extended';
415 1 my $tx = $self->ua->build_tx(GET => _api_url('search/tweets.json')->query(_www_form_urlencode(%query)));
416 1 $self->authentication->($tx->req);
417 1 return $tx;
418 }
419  
420 1 ▶sub verify_credentials {
Subroutine verify_credentials: called CC=4 SCAR=17.4
Pod documented
421 1 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 1 1
422 1 my $self = shift;
423 1 my $tx = $self->_build_verify_credentials(@_);
424 1 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 0 1 1
425 0 ▶ $self->ua->start($tx, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
426 0 my ($ua, $tx) = @_;
427 0 ▶ return $self->$cb(twitter_tx_error($tx)) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
428 0 $self->$cb(undef, Mojo::WebService::Twitter::User->new(twitter => $self)->from_source($tx->res->json));
429 0 });
430 } else {
431 1 $tx = $self->ua->start($tx);
432 1 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 1 1
433 1 return Mojo::WebService::Twitter::User->new(twitter => $self)->from_source($tx->res->json);
434 }
435 }
436  
437 0 ▶sub verify_credentials_p {
Subroutine verify_credentials_p: not called CC=1 SCAR=6.9
Pod documented
438 0 my $self = shift;
439 0 my $tx = $self->_build_verify_credentials(@_);
440 0 ▶ return $self->ua->start_p($tx)->then(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
441 0 my ($tx) = @_;
442 0 ▶ die twitter_tx_error($tx) if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
443 0 return Mojo::WebService::Twitter::User->new(twitter => $self)->from_source($tx->res->json);
444 0 ▶ }, sub { die Mojo::WebService::Twitter::Error->new(connection_error => $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
445 }
446  
447 1 ▶sub _build_verify_credentials {
Subroutine _build_verify_credentials: called CC=1 SCAR=0.0
448 1 my ($self) = @_;
449 1 my $tx = $self->ua->build_tx(GET => _api_url('account/verify_credentials.json')->query(tweet_mode => 'extended'));
450 1 $self->authentication->($tx->req);
451 1 return $tx;
452 }
453  
454 10 ▶sub _api_url { Mojo::WebService::Twitter::Util::_api_url(@_) }
Subroutine _api_url: called CC=1 SCAR=0.0
455 2 ▶sub _oauth_url { Mojo::WebService::Twitter::Util::_oauth_url(@_) }
Subroutine _oauth_url: called CC=1 SCAR=0.0
456 1 ▶sub _oauth2_url { Mojo::WebService::Twitter::Util::_oauth2_url(@_) }
Subroutine _oauth2_url: called CC=1 SCAR=0.0
457  
458 3 ▶sub _oauth {
Subroutine _oauth: called CC=3 SCAR=11.4
459 3 my $self = shift;
460 3 my ($api_key, $api_secret) = ($self->api_key, $self->api_secret);
461 3 ▶ croak 'Twitter API key and secret are required' unless defined $api_key and defined $api_secret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $api_key and defined $api_secret 0 3 3
Condition33%1/3
defined $api_key and defined $api_secret
!ll&&!rl&&r
003
MC/DC0%0/2
defined $api_key and defined $api_secret
defined $api_key defined $api_secret
Truth table33%1/3
A and B
A= defined $api_keyB= defined $api_secret
ABresult
0X0
100
111
462 3 my ($token, $token_secret) = @_;
463 3 my $oauth = WWW::OAuth->new(
464 client_id => $api_key,
465 client_secret => $api_secret,
466 token => $token,
467 token_secret => $token_secret,
468 );
469 3 ▶ return sub { $oauth->authenticate(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
470 }
471  
472 1 ▶sub _oauth2_request {
Subroutine _oauth2_request: called CC=3 SCAR=11.6
473 1 my $self = shift;
474 1 my ($api_key, $api_secret) = ($self->api_key, $self->api_secret);
475 1 ▶ croak 'Twitter API key and secret are required' unless defined $api_key and defined $api_secret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $api_key and defined $api_secret 0 1 1
Condition33%1/3
defined $api_key and defined $api_secret
!ll&&!rl&&r
001
MC/DC0%0/2
defined $api_key and defined $api_secret
defined $api_key defined $api_secret
Truth table33%1/3
A and B
A= defined $api_keyB= defined $api_secret
ABresult
0X0
100
111
476 1 my $token = b64_encode(url_escape($api_key) . ':' . url_escape($api_secret), '');
477 1 ▶ return sub { shift->headers->authorization("Basic $token") };
Subroutine __ANON__: called CC=1 SCAR=0.0
478 }
479  
480 1 ▶sub _oauth2 {
Subroutine _oauth2: called CC=2 SCAR=7.0
481 1 my $self = shift;
482 1 ▶ my $token = shift // croak 'Access token is required for OAuth2 authentication';
Condition50%1/2
shift() // croak("Access token is required for OAuth2 authentication")
l!l
10
MC/DC0%0/1
shift() // croak("Access token is required for OAuth2 authentication")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
483 1 ▶ return sub { shift->headers->authorization("Bearer $token") };
Subroutine __ANON__: called CC=1 SCAR=0.0
484 }
485  
486 39 ▶sub _tweet_object { Mojo::WebService::Twitter::Tweet->new->from_source(shift) }
Subroutine _tweet_object: called CC=1 SCAR=0.0
487  
488 2 ▶sub _user_object { Mojo::WebService::Twitter::User->new->from_source(shift) }
Subroutine _user_object: called CC=1 SCAR=0.0
489  
490 9 ▶sub _www_form_urlencode {
Subroutine _www_form_urlencode: called CC=6 SCAR=18.3
491 9 my @params = @_;
492 9 my @terms;
493 9 ▶ while (@params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 21 9 30
494 21 my ($key, $values) = splice @params, 0, 2;
495 21 ▶ foreach my $value (ref $values eq 'ARRAY' ? @$values : $values) {
Branch: 1 branch
BranchTrueFalseTotal
ref $values eq 'ARRAY' ? : 0 21 21
496 21 ▶ push @terms, join '=', map { url_escape encode 'UTF-8', $_ } ($key // ''), ($value // '');
Condition50%2/4
$key // ""
l!l
210
$value // ""
l!l
210
MC/DC0%0/1
$key // ""
$key
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $key
Aresult
11
00
Truth table50%1/2
A
A= $value
Aresult
11
00
497 }
498 }
499 9 return join '&', @terms;
500 }
501  
502 2 1;
503  
504 =head1 NAME
505  
506 Mojo::WebService::Twitter - Simple Twitter API client
507  
508 =head1 SYNOPSIS
509  
510 my $twitter = Mojo::WebService::Twitter->new(api_key => $api_key, api_secret => $api_secret);
511
512 # Request and store access token
513 $twitter->authentication($twitter->request_oauth2);
514
515 # Blocking API request
516 my $user = $twitter->get_user(screen_name => $name);
517 say $user->screen_name . ' was created on ' . $user->created_at->ymd;
518
519 # Non-blocking API request
520 $twitter->get_tweet($tweet_id, sub {
521 my ($twitter, $err, $tweet) = @_;
522 print $err ? "Error: $err" : 'Tweet: ' . $tweet->text . "\n";
523 });
524 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
525
526 # Non-blocking API request using promises
527 $twitter->get_tweet_p($tweet_id)->then(sub {
528 my ($tweet) = @_;
529 print 'Tweet: ' . $tweet->text . "\n";
530 })->catch(sub {
531 my ($err) = @_;
532 print "Error: $err";
533 })->wait;
534
535 # Some requests require authentication on behalf of a user
536 $twitter->authentication(oauth => $token, $secret);
537 my $authorizing_user = $twitter->verify_credentials;
538
539 my $new_tweet = $twitter->post_tweet('Something new and exciting!');
540  
541 =head1 DESCRIPTION
542  
543 L<Mojo::WebService::Twitter> is a L<Mojo::UserAgent> based
544 L<Twitter|https://twitter.com> API client that can perform requests
545 synchronously or asynchronously. An API key and secret for a
546 L<Twitter Application|https://apps.twitter.com> are required.
547  
548 API requests are authenticated by the L</"authentication"> coderef, which can
549 either use an OAuth 2.0 access token to authenticate requests on behalf of the
550 application itself, or OAuth 1.0 credentials (access token and secret) to
551 authenticate requests on behalf of a specific user. The L<twitter_oauth_creds>
552 script can be used to obtain Twitter OAuth credentials for a user from the
553 command-line. A web application may wish to implement its own OAuth
554 authorization flow, passing a callback URL back to the application in
555 L</"request_oauth">, then calling L</"verify_oauth"> with the passed verifier
556 code to retrieve the credentials. See the
557 L<Twitter documentation|https://dev.twitter.com/web/sign-in/implementing> for
558 more details.
559  
560 All methods which query the Twitter API can be called with an optional trailing
561 callback argument to run a non-blocking API query. Alternatively, the C<_p>
562 variant will run a non-blocking API query and return a L<Mojo::Promise>, which
563 can simplify complex sequences of non-blocking queries. On connection, HTTP, or
564 API error, blocking API queries will throw a L<Mojo::WebService::Twitter::Error>
565 exception. Non-blocking API queries will pass this exception object to the
566 callback or reject the promise, and otherwise pass the results to the callback
567 or resolve the promise.
568  
569 Note that this distribution implements only a subset of the Twitter API.
570 Additional features may be added as requested. See L<Twitter::API> for a more
571 fully-featured lightweight and modern twitter client library.
572  
573 =head1 ATTRIBUTES
574  
575 L<Mojo::WebService::Twitter> implements the following attributes.
576  
577 =head2 api_key
578  
579 my $api_key = $twitter->api_key;
580 $twitter = $twitter->api_key($api_key);
581  
582 API key for your L<Twitter Application|https://apps.twitter.com>.
583  
584 =head2 api_secret
585  
586 my $api_secret = $twitter->api_secret;
587 $twitter = $twitter->api_secret($api_secret);
588  
589 API secret for your L<Twitter Application|https://apps.twitter.com>.
590  
591 =head2 ua
592  
593 my $ua = $webservice->ua;
594 $webservice = $webservice->ua(Mojo::UserAgent->new);
595  
596 HTTP user agent object to use for synchronous and asynchronous requests,
597 defaults to a L<Mojo::UserAgent> object.
598  
599 =head1 METHODS
600  
601 L<Mojo::WebService::Twitter> inherits all methods from L<Mojo::Base>, and
602 implements the following new ones.
603  
604 =head2 authentication
605  
606 my $code = $twitter->authentication;
607 $twitter = $twitter->authentication($code);
608 $twitter = $twitter->authentication({oauth_token => $access_token, oauth_token_secret => $access_token_secret});
609 $twitter = $twitter->authentication(oauth => $access_token, $access_token_secret);
610 $twitter = $twitter->authentication({access_token => $access_token});
611 $twitter = $twitter->authentication(oauth2 => $access_token);
612  
613 Get or set coderef used to authenticate API requests. Passing C<oauth> with
614 optional token and secret, or a hashref containing C<oauth_token> and
615 C<oauth_token_secret>, will set a coderef which uses a L<WWW::OAuth> to
616 authenticate requests. Passing C<oauth2> with required token or a hashref
617 containing C<access_token> will set a coderef which authenticates using the
618 passed access token. The coderef will receive the L<Mojo::Message::Request>
619 object as the first parameter, and an optional hashref of C<oauth_> parameters.
620  
621 =head2 request_oauth
622  
623 =head2 request_oauth_p
624  
625 my $res = $twitter->request_oauth;
626 my $res = $twitter->request_oauth($callback_url);
627 $twitter->request_oauth(sub {
628 my ($twitter, $error, $res) = @_;
629 });
630 my $p = $twitter->request_oauth_p;
631  
632 Send an OAuth 1.0 authorization request and return a hashref containing
633 C<oauth_token> and C<oauth_token_secret> (request token and secret). An
634 optional OAuth callback URL may be passed; by default, C<oob> is passed to use
635 PIN-based authorization. The user should be directed to the authorization URL
636 which can be retrieved by passing the request token to
637 L<Mojo::WebService::Twitter::Util/"twitter_authorize_url">. After
638 authorization, the user will either be redirected to the callback URL with the
639 query parameter C<oauth_verifier>, or receive a PIN to return to the
640 application. Either the verifier string or PIN should be passed to
641 L</"verify_oauth"> to retrieve an access token and secret.
642  
643 =head2 verify_oauth
644  
645 =head2 verify_oauth_p
646  
647 my $res = $twitter->verify_oauth($verifier, $request_token, $request_token_secret);
648 $twitter->verify_oauth($verifier, $request_token, $request_token_secret, sub {
649 my ($twitter, $error, $res) = @_;
650 });
651 my $p = $twitter->verify_oauth_p($verifier, $request_token, $request_token_secret);
652  
653 Verify an OAuth 1.0 authorization request with the verifier string or PIN from
654 the authorizing user, and the previously obtained request token and secret. The
655 secret is cached by L</"request_oauth"> and may be omitted. Returns a hashref
656 containing C<oauth_token> and C<oauth_token_secret> (access token and secret)
657 which may be passed directly to L</"authentication"> to authenticate requests
658 on behalf of the user.
659  
660 =head2 request_oauth2
661  
662 =head2 request_oauth2_p
663  
664 my $res = $twitter->request_oauth2;
665 $twitter->request_oauth2(sub {
666 my ($twitter, $error, $res) = @_;
667 });
668 my $p = $twitter->request_oauth2_p;
669  
670 Request OAuth 2 credentials and return a hashref containing an C<access_token>
671 that can be passed directly to L</"authentication"> to authenticate requests on
672 behalf of the application itself.
673  
674 =head2 get_tweet
675  
676 =head2 get_tweet_p
677  
678 my $tweet = $twitter->get_tweet($tweet_id);
679 $twitter->get_tweet($tweet_id, sub {
680 my ($twitter, $err, $tweet) = @_;
681 });
682 $twitter->get_tweet_p($tweet_id);
683  
684 Retrieve a L<Mojo::WebService::Twitter::Tweet> by tweet ID.
685  
686 =head2 get_user
687  
688 =head2 get_user_p
689  
690 my $user = $twitter->get_user(user_id => $user_id);
691 my $user = $twitter->get_user(screen_name => $screen_name);
692 $twitter->get_user(screen_name => $screen_name, sub {
693 my ($twitter, $err, $user) = @_;
694 });
695 my $p = $twitter->get_user_p(user_id => $user_id);
696  
697 Retrieve a L<Mojo::WebService::Twitter::User> by user ID or screen name.
698  
699 =head2 get_user_timeline
700  
701 =head2 get_user_timeline_p
702  
703 I<Available from version 1.002.>
704  
705 my $tweets = $twitter->get_user_timeline(user_id => $user_id);
706 my $tweets = $twitter->get_user_timeline(screen_name => $screen_name);
707 my $tweets = $twitter->get_user_timeline(user_id => $user_id, %options);
708 $twitter->get_user_timeline(screen_name => $screen_name, %options, sub {
709 my ($twitter, $err, $tweets) = @_;
710 });
711 my $p = $twitter->get_user_timeline_p(screen_name => $screen_name, %options);
712  
713 Retrieve a L<Mojo::Collection> of L<Mojo::WebService::Twitter::Tweet> objects
714 for a user's timeline by user ID or screen name. Note that the embedded user
715 objects will only contain an C<id> to avoid excess duplication of the same
716 user's information; use L</"get_user"> to retrieve the user's information.
717  
718 Accepts the following options:
719  
720 =over
721  
722 =item count
723  
724 count => 5
725  
726 Limit of tweets to try and retrieve per page. Actual returned count may be
727 smaller due to filtering of content that is no longer available, RTs if the
728 L</"exclude_rts"> option is enabled, or replies if the L</"exclude_replies">
729 option is enabled. Maximum C<200>, default C<20>.
730  
731 =item since_id
732  
733 since_id => '12345'
734  
735 Restricts results to those more recent than the given tweet ID. IDs should be
736 specified as a string to avoid issues with large integers. See
737 L<here|https://developer.twitter.com/en/docs/twitter-api/v1/tweets/timelines/guides/working-with-timelines>
738 for more information on filtering results with C<since_id> and C<max_id>.
739  
740 =item max_id
741  
742 max_id => '54321'
743  
744 Restricts results to those older than (or equal to) the given tweet ID. IDs
745 should be specified as a string to avoid issues with large integers. See
746 L<here|https://developer.twitter.com/en/docs/twitter-api/v1/tweets/timelines/guides/working-with-timelines>
747 for more information on filtering results with C<since_id> and C<max_id>.
748  
749 =item exclude_replies
750  
751 exclude_replies => 1
752  
753 If true, replies will be filtered from the results.
754  
755 =item exclude_rts
756  
757 exclude_rts => 1
758  
759 If true, RTs will be filtered from the results.
760  
761 =back
762  
763 =head2 post_tweet
764  
765 =head2 post_tweet_p
766  
767 my $tweet = $twitter->post_tweet($text, %options);
768 $twitter->post_tweet($text, %options, sub {
769 my ($twitter, $err, $tweet) = @_;
770 });
771 my $p = $twitter->post_tweet_p($text, %options);
772  
773 Post a status update (tweet) and retrieve the resulting
774 L<Mojo::WebService::Twitter::Tweet>. Requires OAuth 1.0 authentication. Accepts
775 the following options:
776  
777 =over
778  
779 =item in_reply_to_status_id
780  
781 in_reply_to_status_id => '12345'
782  
783 Indicates the tweet is in reply to an existing tweet ID. IDs should be
784 specified as a string to avoid issues with large integers. This parameter will
785 be ignored by the Twitter API unless the author of the referenced tweet is
786 mentioned within the status text as C<@username>.
787  
788 =item lat
789  
790 lat => '37.781157'
791  
792 The latitude of the location to attach to the tweet. This parameter will be
793 ignored by the Twitter API unless it is within the range C<-90.0> to C<90.0>,
794 and a corresponding C<long> is specified. It is recommended to specify values
795 as strings to avoid issues with floating-point representations.
796  
797 =item long
798  
799 long => '-122.398720'
800  
801 The longitude of the location to attach to the tweet. This parameter will be
802 ignored by the Twitter API unless it is within the range C<-180.0> to C<180.0>,
803 and a corresponding C<lat> is specified. It is recommended to specify values as
804 strings to avoid issues with floating-point representations.
805  
806 =item place_id
807  
808 place_id => 'df51dec6f4ee2b2c'
809  
810 A Twitter L<place ID|https://dev.twitter.com/overview/api/places> to attach to
811 the tweet.
812  
813 =item display_coordinates
814  
815 display_coordinates => 1
816  
817 If true, tweet will display the exact coordinates the tweet was sent from.
818  
819 =back
820  
821 =head2 retweet
822  
823 =head2 retweet_p
824  
825 my $tweet = $twitter->retweet($tweet_id);
826 $twitter->retweet($tweet_id, sub {
827 my ($twitter, $err, $tweet) = @_;
828 });
829 my $p = $twitter->retweet_p($tweet_id);
830  
831 Retweet the tweet ID or L<Mojo::WebService::Twitter::Tweet> object. Returns a
832 L<Mojo::WebService::Twitter::Tweet> representing the original tweet. Requires
833 OAuth 1.0 authentication.
834  
835 =head2 search_tweets
836  
837 =head2 search_tweets_p
838  
839 my $tweets = $twitter->search_tweets($query);
840 my $tweets = $twitter->search_tweets($query, %options);
841 $twitter->search_tweets($query, %options, sub {
842 my ($twitter, $err, $tweets) = @_;
843 });
844 my $p = $twitter->search_tweets_p($query, %options);
845  
846 Search Twitter and return a L<Mojo::Collection> of L<Mojo::WebService::Twitter::Tweet>
847 objects.
848  
849 Accepts the following options:
850  
851 =over
852  
853 =item geocode
854  
855 geocode => '37.781157,-122.398720,1mi'
856 geocode => ['37.781157','-122.398720','1mi']
857 geocode => {latitude => '37.781157', longitude => '-122.398720', radius => '1mi'}
858  
859 Restricts tweets to the given radius of the given latitude/longitude. Radius
860 must be specified as C<mi> (miles) or C<km> (kilometers). It is recommended to
861 specify values as strings to avoid issues with floating-point representations.
862  
863 =item lang
864  
865 lang => 'eu'
866  
867 Restricts tweets to the given L<ISO 639-1|http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes>
868 language code.
869  
870 =item result_type
871  
872 result_type => 'recent'
873  
874 Specifies what type of search results to receive. Valid values are C<recent>,
875 C<popular>, and C<mixed> (default).
876  
877 =item count
878  
879 count => 5
880  
881 Limits the search results per page. Maximum C<100>, default C<15>.
882  
883 =item until
884  
885 until => '2015-07-19'
886  
887 Restricts tweets to those created before the given date, in the format
888 C<YYYY-MM-DD>.
889  
890 =item since_id
891  
892 since_id => '12345'
893  
894 Restricts results to those more recent than the given tweet ID. IDs should be
895 specified as a string to avoid issues with large integers. See
896 L<here|https://developer.twitter.com/en/docs/twitter-api/v1/tweets/timelines/guides/working-with-timelines>
897 for more information on filtering results with C<since_id> and C<max_id>.
898  
899 =item max_id
900  
901 max_id => '54321'
902  
903 Restricts results to those older than (or equal to) the given tweet ID. IDs
904 should be specified as a string to avoid issues with large integers. See
905 L<here|https://developer.twitter.com/en/docs/twitter-api/v1/tweets/timelines/guides/working-with-timelines>
906 for more information on filtering results with C<since_id> and C<max_id>.
907  
908 =back
909  
910 =head2 verify_credentials
911  
912 =head2 verify_credentials_p
913  
914 my $user = $twitter->verify_credentials;
915 $twitter->verify_credentials(sub {
916 my ($twitter, $error, $user) = @_;
917 });
918 my $p = $twitter->verify_credentials_p;
919  
920 Verify the authorizing user's credentials and return a representative
921 L<Mojo::WebService::Twitter::User> object. Requires OAuth 1.0 authentication.
922  
923 =head1 BUGS
924  
925 Report any issues on the public bugtracker.
926  
927 =head1 AUTHOR
928  
929 Dan Book <dbook@cpan.org>
930  
931 =head1 COPYRIGHT AND LICENSE
932  
933 This software is Copyright (c) 2015 by Dan Book.
934  
935 This is free software, licensed under:
936  
937 The Artistic License 2.0 (GPL Compatible)
938  
939 =head1 SEE ALSO
940  
941 L<Twitter::API>, L<WWW::OAuth>
« Twitter/Error.pm ↑ Summary Twitter/Tweet.pm »