line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Gnip::Activity; |
2
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
78005
|
use strict; |
|
7
|
|
|
|
|
17
|
|
|
7
|
|
|
|
|
286
|
|
4
|
7
|
|
|
7
|
|
5160
|
use DateTime; |
|
7
|
|
|
|
|
777927
|
|
|
7
|
|
|
|
|
217
|
|
5
|
7
|
|
|
7
|
|
7931
|
use DateTime::Format::ISO8601; |
|
7
|
|
|
|
|
377308
|
|
|
7
|
|
|
|
|
551
|
|
6
|
7
|
|
|
7
|
|
93
|
use base qw(Net::Gnip::Base); |
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
4738
|
|
7
|
|
|
|
|
|
|
use Net::Gnip::Activity::Payload; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Net::Gnip::Activity - a single item of activity in Gnip |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# Create a new activity |
16
|
|
|
|
|
|
|
# 'at' defaults to now |
17
|
|
|
|
|
|
|
my $activity = Net::Gnip::Activity->new($action, $actor, %opts); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# ... or parse from xml |
20
|
|
|
|
|
|
|
my $activity = Net::Gnip::Activity->parse($xml); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# at, uid and type are always present |
23
|
|
|
|
|
|
|
print "At: ".$activity->at; # returns a DateTime objec |
24
|
|
|
|
|
|
|
$activity->at(DateTime->now); # Can take a string or a DateTime |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
print "Actor: ".$activity->actor; |
27
|
|
|
|
|
|
|
print "Action: ".$activity->action; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# These are optional |
30
|
|
|
|
|
|
|
print "Url: ".$activity->url; |
31
|
|
|
|
|
|
|
print "To: ".$activity->to."\n"; |
32
|
|
|
|
|
|
|
print "Regarding: ".$activity->regarding."\n"; |
33
|
|
|
|
|
|
|
print "Source: ".$activity->source."\n"; |
34
|
|
|
|
|
|
|
print "Tags: ".$activity->tags."\n"; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
my $payload = Net::Gnip::Activity::Payload->new($body); |
37
|
|
|
|
|
|
|
$activity->payload($payload); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
print $activity->as_xml; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 METHODS |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head2 new [option[s]] |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Takes a C and a C as mandatory parameters. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Options is a hash and can contain C, C, C, C |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
If C isn't passed in then the current time is used. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=cut |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub new { |
56
|
|
|
|
|
|
|
my $class = shift; |
57
|
|
|
|
|
|
|
my $action = shift || die "You must pass in an action"; |
58
|
|
|
|
|
|
|
my $actor = shift || die "You must pass in an actor"; |
59
|
|
|
|
|
|
|
my %opts = @_; |
60
|
|
|
|
|
|
|
my $no_dt = $opts{_no_dt}; |
61
|
|
|
|
|
|
|
my $at = delete $opts{at} || DateTime->now; |
62
|
|
|
|
|
|
|
my $self = bless { %opts, action => $action, actor => $actor }, ref($class) || $class; |
63
|
|
|
|
|
|
|
$self->at($at); |
64
|
|
|
|
|
|
|
return $self; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head2 at [value] |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Returns the current at value. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Optionally takes either a string or a DateTime object to set |
73
|
|
|
|
|
|
|
the at time. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=cut |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub at { |
78
|
|
|
|
|
|
|
my $self = shift; |
79
|
|
|
|
|
|
|
if (@_) { |
80
|
|
|
|
|
|
|
my $dt = shift; |
81
|
|
|
|
|
|
|
# normalise to DateTime |
82
|
|
|
|
|
|
|
if (ref($dt) && $dt->isa('DateTime')) { |
83
|
|
|
|
|
|
|
# normalise to epoch time if we're forcing no DateTime |
84
|
|
|
|
|
|
|
$dt = $dt->epoch if $self->{_no_dt}; |
85
|
|
|
|
|
|
|
} else { |
86
|
|
|
|
|
|
|
$dt = $self->_handle_datetime($dt) unless $self->{_no_dt}; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
$self->{at} = $dt; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
return $self->{at}; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 actor [actor] |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Gets or sets the current actor. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub actor { my $self = shift; $self->_do('actor', @_) } |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head2 action [action] |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Gets or sets the current action. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=cut |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub action { my $self = shift; $self->_do('action', @_) } |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head2 url [url] |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Gets or sets the current url. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=cut |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub url { my $self = shift; $self->_do('url', @_) } |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 to [to] |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Gets or sets the current to. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=cut |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub to { my $self = shift; $self->_do('to', @_) } |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head2 regarding [regarding] |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Gets or sets the current regarding. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub regarding { my $self = shift; $self->_do('regarding', @_) } |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 source [source] |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Gets or sets the current source. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=cut |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub source { my $self = shift; $self->_do('source', @_) } |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head2 tags [tags] |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Gets or sets the current tags. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
The param either a list or comma separated string. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Returns a list. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub tags { |
153
|
|
|
|
|
|
|
my $self = shift; |
154
|
|
|
|
|
|
|
my @args; |
155
|
|
|
|
|
|
|
if (@_) { |
156
|
|
|
|
|
|
|
push @args, join(", ", @_); |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
split /\s*,\s*/, $self->_do('to', @args); |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head2 payload [payload] |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Get or sets the current payload. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=cut |
167
|
|
|
|
|
|
|
sub payload { my $self = shift; $self->_do('payload', @_) } |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 parse |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Parse some xml into an activity. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub parse { |
179
|
|
|
|
|
|
|
my $class = shift; |
180
|
|
|
|
|
|
|
my $xml = shift; |
181
|
|
|
|
|
|
|
my %opts = @_; |
182
|
|
|
|
|
|
|
my $parser = $class->parser(); |
183
|
|
|
|
|
|
|
my $doc = $parser->parse_string($xml); |
184
|
|
|
|
|
|
|
my $elem = $doc->documentElement(); |
185
|
|
|
|
|
|
|
return $class->_from_element($elem, %opts); |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub _from_element { |
189
|
|
|
|
|
|
|
my $class = shift; |
190
|
|
|
|
|
|
|
my $elem = shift; |
191
|
|
|
|
|
|
|
my %opts = @_; |
192
|
|
|
|
|
|
|
my $no_dt = (ref($class) && $class->{_no_dt}) || $opts{_no_dt}; |
193
|
|
|
|
|
|
|
foreach my $attr ($elem->attributes()) { |
194
|
|
|
|
|
|
|
my $name = $attr->name; |
195
|
|
|
|
|
|
|
$opts{$name} = $attr->value; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
foreach my $payload ($elem->getChildrenByTagName('payload')) { |
198
|
|
|
|
|
|
|
$opts{payload} = Net::Gnip::Activity::Payload->_from_element($payload, _no_dt => $no_dt); |
199
|
|
|
|
|
|
|
last; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
$opts{at} = $class->_handle_datetime($opts{at}); |
203
|
|
|
|
|
|
|
return $class->new(delete $opts{action}, delete $opts{actor}, %opts, _no_dt => $no_dt); |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head2 as_xml |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Return the activity as xml |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=cut |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
sub as_xml { |
213
|
|
|
|
|
|
|
my $self = shift; |
214
|
|
|
|
|
|
|
my $as_element = shift; |
215
|
|
|
|
|
|
|
my $element = XML::LibXML::Element->new('activity'); |
216
|
|
|
|
|
|
|
my $payload = delete $self->{payload}; |
217
|
|
|
|
|
|
|
foreach my $key (keys %$self) { |
218
|
|
|
|
|
|
|
next if '_' eq substr($key, 0, 1); |
219
|
|
|
|
|
|
|
my $value = $self->{$key}; |
220
|
|
|
|
|
|
|
if ('at' eq $key) { |
221
|
|
|
|
|
|
|
$value = DateTime->from_epoch( epoch => $value ) unless ref($value); |
222
|
|
|
|
|
|
|
$value = $self->_handle_datetime($value); |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
$element->setAttribute($key, $value); |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
$element->addChild($payload->as_xml(1)) if defined $payload; |
227
|
|
|
|
|
|
|
return ($as_element) ? $element : $element->toString(1); |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
sub _handle_datetime { |
231
|
|
|
|
|
|
|
my $self = shift; |
232
|
|
|
|
|
|
|
my $dt = shift; |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
if (ref $dt && $dt->isa('DateTime')) { |
235
|
|
|
|
|
|
|
return $dt->strftime("%FT%H:%M:%SZ") |
236
|
|
|
|
|
|
|
} else { |
237
|
|
|
|
|
|
|
return DateTime::Format::ISO8601->parse_datetime($dt) |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
1; |