Gnip/Activity.pm

filter: Statementstmt 85.3% 76 / 89 Branchbran 90.9% 20 / 22 Conditioncond 77.2% 17 / 22 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 73.6% 14 / 19 Podpod 100% 12 / 12 totaltotal 81.9% 145 / 177 CC 23 SCAR 14.4
CC
23
Cov
85%
as_xml
7.0
at
6.0
tags
6.0
CRAP 4.2
« Gnip/Activity/Payload.pm ↑ Summary Gnip/BaseStream.pm »
1 package Net::Gnip::Activity;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use base qw(Net::Gnip::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Net::Gnip::Activity::Payload;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 <action> <actor> [option[s]]
46  
47 Takes a C<action> and a C<actor> as mandatory parameters.
48  
49 Options is a hash and can contain C<at>, C<url>, C<regarding>, C<source> and <tags>.
50  
51 If C<at> isn't passed in then the current time is used.
52  
53 =cut
54  
55 19 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod documented
56 19 my $class = shift;
57 19 ▶ my $action = shift || die "You must pass in an action";
Condition100%2/2
shift() || die('You must pass in an action')
l!l
172
MC/DC100%1/1
shift() || die('You must pass in an action')
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
58 17 ▶ my $actor = shift || die "You must pass in an actor";
Condition50%1/2
shift() || die('You must pass in an actor')
l!l
170
MC/DC0%0/1
shift() || die('You must pass in an actor')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
59 17 my %opts = @_;
60 17 my $no_dt = $opts{_no_dt};
61 17 ▶ my $at = delete $opts{at} || DateTime->now;
Condition66%2/3
delete $opts{'at'} || 'DateTime'->now
l!l&&r!l&&!r
890
MC/DC0%0/2
delete $opts{'at'} || 'DateTime'->now
delete $opts{'at'} 'DateTime'->now
Truth table66%2/3
A || B
A= delete $opts{'at'}B= 'DateTime'->now
ABresult
1X1
011
000
62 17 ▶ my $self = bless { %opts, action => $action, actor => $actor }, ref($class) || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
2150
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
63 17 $self->at($at);
64 17 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 31 ▶sub at {
Subroutine at: called CC=6 SCAR=17.9
Pod documented
78 31 my $self = shift;
79 31 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 21 10 31
80 21 my $dt = shift;
81 # normalise to DateTime
82 21 ▶ if (ref($dt) && $dt->isa('DateTime')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dt and $dt->isa('DateTime')) { } 17 4 21
Condition66%2/3
ref $dt and $dt->isa("DateTime")
!ll&&!rl&&r
4017
MC/DC50%1/2
ref $dt and $dt->isa("DateTime")
ref $dt $dt->isa("DateTime")
Truth table66%2/3
A and B
A= ref $dtB= $dt->isa("DateTime")
ABresult
0X0
100
111
83 # normalise to epoch time if we're forcing no DateTime
84 17 ▶ $dt = $dt->epoch if $self->{_no_dt};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_no_dt"} 4 13 17
85 } else {
86 4 ▶ $dt = $self->_handle_datetime($dt) unless $self->{_no_dt};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_no_dt"} 1 3 4
87 }
88 21 $self->{at} = $dt;
89 }
90  
91 31 return $self->{at};
92 }
93  
94 =head2 actor [actor]
95  
96 Gets or sets the current actor.
97  
98 =cut
99  
100 19 ▶sub actor { my $self = shift; $self->_do('actor', @_) }
Subroutine actor: called CC=1 SCAR=0.0
Pod documented
101  
102 =head2 action [action]
103  
104 Gets or sets the current action.
105  
106 =cut
107  
108 8 ▶sub action { my $self = shift; $self->_do('action', @_) }
Subroutine action: called CC=1 SCAR=0.0
Pod documented
109  
110 =head2 url [url]
111  
112 Gets or sets the current url.
113  
114 =cut
115  
116 0 ▶sub url { my $self = shift; $self->_do('url', @_) }
Subroutine url: not called CC=1 SCAR=6.9
Pod documented
117  
118 =head2 to [to]
119  
120 Gets or sets the current to.
121  
122 =cut
123  
124 0 ▶sub to { my $self = shift; $self->_do('to', @_) }
Subroutine to: not called CC=1 SCAR=6.9
Pod documented
125  
126 =head2 regarding [regarding]
127  
128 Gets or sets the current regarding.
129  
130 =cut
131  
132 0 ▶sub regarding { my $self = shift; $self->_do('regarding', @_) }
Subroutine regarding: not called CC=1 SCAR=6.9
Pod documented
133  
134 =head2 source [source]
135  
136 Gets or sets the current source.
137  
138 =cut
139  
140 0 ▶sub source { my $self = shift; $self->_do('source', @_) }
Subroutine source: not called CC=1 SCAR=6.9
Pod documented
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 0 ▶sub tags {
Subroutine tags: not called CC=2 SCAR=17.9
Pod documented
153 0 my $self = shift;
154 0 my @args;
155 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
156 0 push @args, join(", ", @_);
157 }
158 0 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 6 ▶sub payload { my $self = shift; $self->_do('payload', @_) }
Subroutine payload: called CC=1 SCAR=0.0
Pod documented
168  
169  
170  
171  
172 =head2 parse <xml>
173  
174 Parse some xml into an activity.
175  
176 =cut
177  
178 4 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
179 4 my $class = shift;
180 4 my $xml = shift;
181 4 my %opts = @_;
182 4 my $parser = $class->parser();
183 4 my $doc = $parser->parse_string($xml);
184 4 my $elem = $doc->documentElement();
185 4 return $class->_from_element($elem, %opts);
186 }
187  
188 6 ▶sub _from_element {
Subroutine _from_element: called CC=5 SCAR=16.1
189 6 my $class = shift;
190 6 my $elem = shift;
191 6 my %opts = @_;
192 6 ▶ my $no_dt = (ref($class) && $class->{_no_dt}) || $opts{_no_dt};
Condition100%6/6
ref $class && $class->{'_no_dt'} || $opts{'_no_dt'}
l!l&&r!l&&!r
123
ref $class and $class->{"_no_dt"}
!ll&&!rl&&r
411
MC/DC100%3/3
ref $class && $class->{'_no_dt'} || $opts{'_no_dt'}
ref $class $class->{"_no_dt"} $opts{'_no_dt'}
Truth table80%4/5
A and B || C
A= ref $classB= $class->{"_no_dt"}C= $opts{'_no_dt'}
ABCresult
11X1
0X11
1011
0X00
1000
193 6 foreach my $attr ($elem->attributes()) {
194 18 my $name = $attr->name;
195 18 $opts{$name} = $attr->value;
196 }
197 6 foreach my $payload ($elem->getChildrenByTagName('payload')) {
198 4 $opts{payload} = Net::Gnip::Activity::Payload->_from_element($payload, _no_dt => $no_dt);
199 4 last;
200 }
201  
202 6 $opts{at} = $class->_handle_datetime($opts{at});
203 6 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 4 ▶sub as_xml {
Subroutine as_xml: called CC=7 SCAR=19.5
Pod documented
213 4 my $self = shift;
214 4 my $as_element = shift;
215 4 my $element = XML::LibXML::Element->new('activity');
216 4 my $payload = delete $self->{payload};
217 4 foreach my $key (keys %$self) {
218 14 ▶ next if '_' eq substr($key, 0, 1);
Branch: 1 branch
BranchTrueFalseTotal
if "_" eq substr($key, 0, 1) 2 12 14
219 12 my $value = $self->{$key};
220 12 ▶ if ('at' eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ("at" eq $key) 4 8 12
221 4 ▶ $value = DateTime->from_epoch( epoch => $value ) unless ref($value);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 1 3 4
222 4 $value = $self->_handle_datetime($value);
223 }
224 12 $element->setAttribute($key, $value);
225 }
226 4 ▶ $element->addChild($payload->as_xml(1)) if defined $payload;
Branch: 1 branch
BranchTrueFalseTotal
if defined $payload 2 2 4
227 4 ▶ return ($as_element) ? $element : $element->toString(1);
Branch: 1 branch
BranchTrueFalseTotal
$as_element ? : 2 2 4
228 }
229  
230 13 ▶sub _handle_datetime {
Subroutine _handle_datetime: called CC=3 SCAR=11.0
231 13 my $self = shift;
232 13 my $dt = shift;
233  
234 13 ▶ if (ref $dt && $dt->isa('DateTime')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dt and $dt->isa('DateTime')) { } 4 9 13
Condition66%2/3
ref $dt and $dt->isa("DateTime")
!ll&&!rl&&r
904
MC/DC50%1/2
ref $dt and $dt->isa("DateTime")
ref $dt $dt->isa("DateTime")
Truth table66%2/3
A and B
A= ref $dtB= $dt->isa("DateTime")
ABresult
0X0
100
111
235 4 return $dt->strftime("%FT%H:%M:%SZ")
236 } else {
237 9 return DateTime::Format::ISO8601->parse_datetime($dt)
238 }
239 }
240 10 1;
« Gnip/Activity/Payload.pm ↑ Summary Gnip/BaseStream.pm »