Twitter/Tweet.pm

filter: Statementstmt 100% 46 / 46 Branchbran 87.5% 14 / 16 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 90.0% 72 / 80 CC 12 SCAR 21.3
CC
12
Cov
93%
from_source
12.2
BEGIN
1.0
BEGIN
1.0
CRAP 8.4
« Twitter.pm ↑ Summary Twitter/User.pm »
1 package Mojo::WebService::Twitter::Tweet;
2 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Mojo::WebService::Twitter::Media;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mojo::WebService::Twitter::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::WebService::Twitter::Util 'parse_twitter_timestamp';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '1.003';
10  
11 2 has [qw(source coordinates created_at favorites id in_reply_to_screen_name
12 in_reply_to_status_id in_reply_to_user_id retweets text user)];
13 1 ▶has media => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 41 ▶sub from_source {
Subroutine from_source: called CC=12 SCAR=25.0
Pod documented
16 41 my ($self, $source) = @_;
17 41 ▶ $self->coordinates($source->{coordinates}{coordinates}) if defined $source->{coordinates};
Branch: 1 branch
BranchTrueFalseTotal
if defined $source->{"coordinates"} 0 41 41
18 41 ▶ $self->created_at(parse_twitter_timestamp($source->{created_at})) if defined $source->{created_at};
Branch: 1 branch
BranchTrueFalseTotal
if defined $source->{"created_at"} 41 0 41
19 41 $self->favorites($source->{favorite_count});
20 41 $self->id($source->{id_str});
21 41 ▶ $self->in_reply_to_screen_name($source->{in_reply_to_screen_name}) if defined $source->{in_reply_to_screen_name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $source->{"in_reply_to_screen_name"} 10 31 41
22 41 ▶ $self->in_reply_to_status_id($source->{in_reply_to_status_id_str}) if defined $source->{in_reply_to_status_id_str};
Branch: 1 branch
BranchTrueFalseTotal
if defined $source->{"in_reply_to_status_id_str"} 10 31 41
23 41 ▶ $self->in_reply_to_user_id($source->{in_reply_to_user_id_str}) if defined $source->{in_reply_to_user_id_str};
Branch: 1 branch
BranchTrueFalseTotal
if defined $source->{"in_reply_to_user_id_str"} 10 31 41
24 41 ▶ if (defined $source->{extended_entities} and defined $source->{extended_entities}{media}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $source->{"extended_entities"} and defined $source->{"extended_entities"}{"media"}) 13 28 41
Condition66%2/3
defined $source->{"extended_entities"} and defined $source->{"extended_entities"}{"media"}
!ll&&!rl&&r
28013
MC/DC50%1/2
defined $source->{"extended_entities"} and defined $source->{"extended_entities"}{"media"}
defined $source->{"extended_entities"} defined $source->{"extended_entities"}{"media"}
Truth table66%2/3
A and B
A= defined $source->{"extended_entities"}B= defined $source->{"extended_entities"}{"media"}
ABresult
0X0
100
111
25 13 my @media;
26 13 foreach my $media_source (@{$source->{extended_entities}{media}}) {
27 13 my $media = Mojo::WebService::Twitter::Media->new->from_source($media_source);
28 13 weaken($media->{tweet} = $self);
29 13 push @media, $media;
30 }
31 13 $self->media(\@media);
32 }
33 41 $self->retweets($source->{retweet_count});
34 41 ▶ my $text = $source->{full_text} // $source->{text};
Condition33%1/3
$source->{"full_text"} // $source->{"text"}
l!l&&r!l&&!r
4100
MC/DC0%0/2
$source->{"full_text"} // $source->{"text"}
$source->{"full_text"} $source->{"text"}
Truth table33%1/3
A // B
A= $source->{"full_text"}B= $source->{"text"}
ABresult
1X1
011
000
35 41 ▶ if (defined $source->{display_text_range}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $source->{"display_text_range"}) 40 1 41
36 40 my ($start, $end) = @{$source->{display_text_range}};
37 40 $text = substr $text, $start, $end;
38 }
39 41 $self->text($text);
40 41 ▶ $self->user(Mojo::WebService::Twitter::User->new->from_source($source->{user})) if defined $source->{user};
Branch: 1 branch
BranchTrueFalseTotal
if defined $source->{"user"} 39 2 41
41 41 $self->source($source);
42 41 return $self;
43 }
44  
45 2 1;
46  
47 =head1 NAME
48  
49 Mojo::WebService::Twitter::Tweet - A tweet
50  
51 =head1 SYNOPSIS
52  
53 use Mojo::WebService::Twitter;
54 my $twitter = Mojo::WebService::Twitter->new(api_key => $api_key, api_secret => $api_secret);
55 my $tweet = $twitter->get_tweet($tweet_id);
56
57 my $username = $tweet->user->screen_name;
58 my $created_at = $tweet->created_at;
59 my $text = $tweet->text;
60 say "[$created_at] \@$username: $text";
61  
62 =head1 DESCRIPTION
63  
64 L<Mojo::WebService::Twitter::Tweet> is an object representing a
65 L<Twitter|https://twitter.com> tweet. See L<https://dev.twitter.com/overview/api/tweets>
66 for more information.
67  
68 =head1 ATTRIBUTES
69  
70 =head2 source
71  
72 my $href = $tweet->source;
73  
74 Source data hashref from Twitter API.
75  
76 =head2 coordinates
77  
78 my $coords = $tweet->coordinates;
79  
80 Array reference of geographic coordinates (longitude then latitude), or
81 C<undef> if tweet does not have coordinates.
82  
83 =head2 created_at
84  
85 my $ts = $tweet->created_at;
86  
87 L<Time::Piece> object representing the creation time of the tweet in UTC.
88  
89 =head2 favorites
90  
91 my $count = $tweet->favorites;
92  
93 Number of times the tweet has been favorited.
94  
95 =head2 id
96  
97 my $tweet_id = $tweet->id;
98  
99 Tweet identifier. Note that tweet IDs are usually too large to be represented
100 as a number, so should always be treated as a string.
101  
102 =head2 in_reply_to_screen_name
103  
104 my $screen_name = $tweet->in_reply_to_screen_name;
105  
106 Screen name of user whom tweet was in reply to, or C<undef> if tweet was not a
107 reply.
108  
109 =head2 in_reply_to_status_id
110  
111 my $tweet_id = $tweet->in_reply_to_status_id;
112  
113 Tweet ID which tweet was in reply to, or C<undef> if tweet was not a reply.
114  
115 =head2 in_reply_to_user_id
116  
117 my $user_id = $tweet->in_reply_to_user_id;
118  
119 User ID of user whom tweet was in reply to, or C<undef> if tweet was not a
120 reply.
121  
122 =head2 media
123  
124 my $media = $tweet->media;
125  
126 Array reference of media entities associated to this tweet as
127 L<Mojo::WebService::Twitter::Media> objects.
128  
129 =head2 retweets
130  
131 my $count = $tweet->retweets;
132  
133 Number of times the tweet has been retweeted.
134  
135 =head2 text
136  
137 my $text = $tweet->text;
138  
139 Text contents of tweet.
140  
141 =head2 user
142  
143 my $user = $tweet->user;
144  
145 User who sent the tweet, as a L<Mojo::WebService::Twitter::User> object.
146  
147 =head1 METHODS
148  
149 L<Mojo::WebService::Twitter::Tweet> inherits all methods from L<Mojo::Base>,
150 and implements the following new ones.
151  
152 =head2 from_source
153  
154 $tweet = $tweet->from_source($hr);
155  
156 Populate attributes from hashref of Twitter API source data.
157  
158 =head1 BUGS
159  
160 Report any issues on the public bugtracker.
161  
162 =head1 AUTHOR
163  
164 Dan Book <dbook@cpan.org>
165  
166 =head1 COPYRIGHT AND LICENSE
167  
168 This software is Copyright (c) 2015 by Dan Book.
169  
170 This is free software, licensed under:
171  
172 The Artistic License 2.0 (GPL Compatible)
173  
174 =head1 SEE ALSO
175  
176 L<Mojo::WebService::Twitter>
« Twitter.pm ↑ Summary Twitter/User.pm »