File Coverage

blib/lib/Mojo/WebService/Twitter/Tweet.pm
Criterion Covered Total %
statement 41 41 100.0
branch 14 16 87.5
condition 3 6 50.0
subroutine 6 6 100.0
pod 1 1 100.0
total 65 70 92.8


line stmt bran cond sub pod time code
1             package Mojo::WebService::Twitter::Tweet;
2 2     2   14 use Mojo::Base -base;
  2         5  
  2         13  
3              
4 2     2   1289 use Mojo::WebService::Twitter::Media;
  2         5  
  2         16  
5 2     2   968 use Mojo::WebService::Twitter::User;
  2         5  
  2         17  
6 2     2   75 use Mojo::WebService::Twitter::Util 'parse_twitter_timestamp';
  2         4  
  2         82  
7 2     2   11 use Scalar::Util 'weaken';
  2         4  
  2         1055  
8              
9             our $VERSION = '1.001';
10              
11             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             has media => sub { [] };
14              
15             sub from_source {
16 21     21 1 172 my ($self, $source) = @_;
17 21 50       75 $self->coordinates($source->{coordinates}{coordinates}) if defined $source->{coordinates};
18 21 50       92 $self->created_at(parse_twitter_timestamp($source->{created_at})) if defined $source->{created_at};
19 21         1433 $self->favorites($source->{favorite_count});
20 21         153 $self->id($source->{id_str});
21 21 100       144 $self->in_reply_to_screen_name($source->{in_reply_to_screen_name}) if defined $source->{in_reply_to_screen_name};
22 21 100       67 $self->in_reply_to_status_id($source->{in_reply_to_status_id_str}) if defined $source->{in_reply_to_status_id_str};
23 21 100       71 $self->in_reply_to_user_id($source->{in_reply_to_user_id_str}) if defined $source->{in_reply_to_user_id_str};
24 21 100 66     75 if (defined $source->{extended_entities} and defined $source->{extended_entities}{media}) {
25 2         4 my @media;
26 2         5 foreach my $media_source (@{$source->{extended_entities}{media}}) {
  2         8  
27 2         15 my $media = Mojo::WebService::Twitter::Media->new->from_source($media_source);
28 2         14 weaken($media->{tweet} = $self);
29 2         7 push @media, $media;
30             }
31 2         8 $self->media(\@media);
32             }
33 21         81 $self->retweets($source->{retweet_count});
34 21   33     133 my $text = $source->{full_text} // $source->{text};
35 21 100       53 if (defined $source->{display_text_range}) {
36 20         31 my ($start, $end) = @{$source->{display_text_range}};
  20         54  
37 20         101 $text = substr $text, $start, $end;
38             }
39 21         70 $self->text($text);
40 21 100       181 $self->user(Mojo::WebService::Twitter::User->new->from_source($source->{user})) if defined $source->{user};
41 21         136 $self->source($source);
42 21         168 return $self;
43             }
44              
45             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 is an object representing a
65             L tweet. See L
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 if tweet does not have coordinates.
82              
83             =head2 created_at
84              
85             my $ts = $tweet->created_at;
86              
87             L 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 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 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 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 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 object.
146              
147             =head1 METHODS
148              
149             L inherits all methods from L,
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
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