Gnip/Filter.pm

filter: Statementstmt 94.0% 79 / 84 Branchbran 75.0% 12 / 16 Conditioncond 50.0% 12 / 24 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 79.1% 125 / 158 CC 25 SCAR 16.7
CC
25
Cov
83%
new
9.1
as_xml
7.0
_from_element
6.0
CRAP 5.3
« Gnip.pm ↑ Summary Gnip/Activity/Payload.pm »
1 package Net::Gnip::Filter;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use base qw(Net::Gnip::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 =head1 NAME
8  
9 Net::Gnip::Filter - a GNIP filter
10  
11 =head1 SYNOPSIS
12  
13 my @rules = ( { type => 'actor', value => 'joe' } );
14  
15 my $filter = Net::Gnip::Filter->new($name, $full_data, [@rules], %opts);
16 my $name = $filter->name;
17 my $full = $filter->full_data;
18 my @rules = $filter->rules;
19 my %what = $filter->what;
20  
21 $filter->what( url => $url );
22 $filter->what( jid => $jid );
23  
24 =head1 METHODS
25  
26 =cut
27  
28 =head2 new <name> <full data> <rule[s]> [opt[s]]
29  
30 Create a new filter.
31  
32 =cut
33 5 ▶sub new {
Subroutine new: called CC=7 SCAR=22.0
Pod documented
34 5 my $class = shift;
35 5 ▶ my $name = shift || die "You must pass in a name";
Condition50%1/2
shift() || die('You must pass in a name')
l!l
50
MC/DC0%0/1
shift() || die('You must pass in a name')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
36 5 ▶ my $full = shift || die "You must pass in whether you want full data";
Condition50%1/2
shift() || die('You must pass in whether you want full data')
l!l
50
MC/DC0%0/1
shift() || die('You must pass in whether you want full data')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
37 5 ▶ my $rules = shift || die "You must pass in at least one rule";
Condition50%1/2
shift() || die('You must pass in at least one rule')
l!l
50
MC/DC0%0/1
shift() || die('You must pass in at least one rule')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
38 5 my %what = @_;
39 5 ▶ if (defined $what{postUrl} && defined $what{jid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $what{"postUrl"} and defined $what{"jid"}) 0 5 5
Condition33%1/3
defined $what{"postUrl"} and defined $what{"jid"}
!ll&&!rl&&r
500
MC/DC0%0/2
defined $what{"postUrl"} and defined $what{"jid"}
defined $what{"postUrl"} defined $what{"jid"}
Truth table33%1/3
A and B
A= defined $what{"postUrl"}B= defined $what{"jid"}
ABresult
0X0
100
111
40 0 die "You can only pass in a url or a jid option";
41 }
42 5 my %opts = (
43 name => $name,
44 fullData => $full,
45 rules => $rules,
46 what => \%what,
47 );
48 5 ▶ return bless \%opts, ref($class) || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
140
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
49 }
50  
51 =head2 name [name]
52  
53 Get or sets the name.
54  
55 =cut
56 4 ▶sub name { shift->_do('name', @_) }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
57  
58 =head2 full_data [full]
59  
60 Get or set whether we want full data
61  
62 =cut
63 4 ▶sub full_data { shift->_do('fullData', @_) }
Subroutine full_data: called CC=1 SCAR=0.0
Pod documented
64  
65 =head2 rules [rule[s]]
66  
67 Get or set the rules.
68  
69 Rules should be hashrefs with a type key and value key.
70  
71 The type key should be one of
72  
73 actor
74 tag
75 to
76 regarding
77 source
78  
79 =cut
80 4 ▶sub rules {
Subroutine rules: called CC=3 SCAR=11.8
Pod documented
81 4 my $self = shift;
82 4 my @args;
83 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 4 4
84 0 @args = [@_];
85 }
86 4 ▶ return @{$self->_do('rules', @args) || []};
Condition50%1/2
$self->_do('rules', @args) || []
l!l
40
MC/DC0%0/1
$self->_do('rules', @args) || []
$self->_do('rules', @args)
Truth table50%1/2
A
A= $self->_do('rules', @args)
Aresult
11
00
87 }
88  
89 =head2 what <type> <value>
90  
91 Get or set what type is needed.
92  
93 Type should be one of
94  
95 postUrl
96 jid
97  
98 =cut
99 2 ▶sub what {
Subroutine what: called CC=4 SCAR=17.9
Pod documented
100 2 my $self = shift;
101 2 my @args;
102 2 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 2 2
103 0 my $type = shift;
104 0 ▶ my $value = shift || "";
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
105 0 @args = { $type => $value };
106 }
107 2 ▶ return %{$self->_do('what', @args) || {}};
Condition50%1/2
$self->_do('what', @args) || {}
l!l
20
MC/DC0%0/1
$self->_do('what', @args) || {}
$self->_do('what', @args)
Truth table50%1/2
A
A= $self->_do('what', @args)
Aresult
11
00
108 }
109  
110 =head2 parse <xml>
111  
112 Parse some xml into an activity.
113  
114 =cut
115  
116 2 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
117 2 my $class = shift;
118 2 my $xml = shift;
119 2 my %opts = @_;
120 2 my $parser = $class->parser();
121 2 my $doc = $parser->parse_string($xml);
122 2 my $elem = $doc->documentElement();
123 2 return $class->_from_element($elem, %opts);
124 }
125  
126 3 ▶sub _from_element {
Subroutine _from_element: called CC=6 SCAR=17.9
127 3 my $class = shift;
128 3 my $elem = shift;
129 3 my %opts = @_;
130 3 foreach my $attr ($elem->attributes()) {
131 6 my $name = $attr->name;
132 6 $opts{$name} = $attr->value;
133 }
134 7 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
135 3 my @rules;
136 my %what;
137 3 foreach my $child ($elem->childNodes) {
138 12 my $name = $child->nodeName;
139 12 ▶ if ('postUrl' eq $name || 'jid' eq $name) {
Branch: 2 branches
BranchTrueFalseTotal
if ('postUrl' eq $name or 'jid' eq $name) { } 2 10 12
elsif ('rule' eq $name) { } 3 7 10
Condition66%2/3
"postUrl" eq $name or "jid" eq $name
l!l&&r!l&&!r
0210
MC/DC50%1/2
"postUrl" eq $name or "jid" eq $name
"postUrl" eq $name "jid" eq $name
Truth table66%2/3
A or B
A= "postUrl" eq $nameB= "jid" eq $name
ABresult
1X1
011
000
140 2 $what{$name} = $child->firstChild->textContent;
141 } elsif ('rule' eq $name) {
142 3 my $rule;
143 3 push @rules, $class->_parse_rule($child);
144 }
145 }
146 3 return $class->new(delete $opts{name}, delete $opts{fullData}, [@rules], %what);
147 }
148  
149 3 ▶sub _parse_rule {
Subroutine _parse_rule: called CC=2 SCAR=6.9
150 3 my $self = shift;
151 3 my $elem = shift;
152 3 my $rule;
153 3 $rule->{$_} = $elem->getAttribute($_) for qw(type value);
154 3 return $rule;
155 }
156  
157 =head2 as_xml
158  
159 Return the activity as xml
160  
161 =cut
162  
163 2 ▶sub as_xml {
Subroutine as_xml: called CC=7 SCAR=19.5
Pod documented
164 2 my $self = shift;
165 2 my $as_element = shift;
166 2 my $element = XML::LibXML::Element->new('filter');
167 2 my $what = delete $self->{what};
168 2 my $rules = delete $self->{rules};
169  
170 2 foreach my $key (keys %$self) {
171 4 ▶ next if '_' eq substr($key, 0, 1);
Branch: 1 branch
BranchTrueFalseTotal
if "_" eq substr($key, 0, 1) 0 4 4
172 4 my $value = $self->{$key};
173 4 $element->setAttribute($key, $value);
174 }
175 2 ▶ if (defined $what && defined [%$what]->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $what and defined [%$what]->[0]) 1 1 2
Condition66%2/3
defined $what and defined [%$what]->[0]
!ll&&!rl&&r
011
MC/DC50%1/2
defined $what and defined [%$what]->[0]
defined $what defined [%$what]->[0]
Truth table66%2/3
A and B
A= defined $whatB= defined [%$what]->[0]
ABresult
0X0
100
111
176 1 my $tmp = XML::LibXML::Element->new([%$what]->[0]);
177 1 $tmp->appendTextNode([%$what]->[1]);
178 1 $element->addChild($tmp);
179 }
180 2 foreach my $rule (@$rules) {
181 2 my $tmp = $self->_create_rule($rule);
182 2 $element->addChild($tmp);
183 }
184 2 ▶ return ($as_element) ? $element : $element->toString(1);
Branch: 1 branch
BranchTrueFalseTotal
$as_element ? : 1 1 2
185 }
186  
187 2 ▶sub _create_rule {
Subroutine _create_rule: called CC=2 SCAR=6.9
188 2 my $self = shift;
189 2 my $rule = shift;
190 2 my $tmp = XML::LibXML::Element->new('rule');
191 2 foreach my $key (keys %$rule) {
192 4 $tmp->setAttribute($key, $rule->{$key});
193 }
194 2 return $tmp;
195 }
196  
197 7 1;
198  
« Gnip.pm ↑ Summary Gnip/Activity/Payload.pm »