| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::Gnip::Filter; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 43823 | use strict; | 
|  | 4 |  |  |  |  | 12 |  | 
|  | 4 |  |  |  |  | 177 |  | 
| 4 | 4 |  |  | 4 |  | 23 | use base qw(Net::Gnip::Base); | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 1580 |  | 
| 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    [opt[s]] | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | Create a new filter. | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =cut | 
| 33 |  |  |  |  |  |  | sub new { | 
| 34 |  |  |  |  |  |  | my $class = shift; | 
| 35 |  |  |  |  |  |  | my $name  = shift || die "You must pass in a name"; | 
| 36 |  |  |  |  |  |  | my $full  = shift || die "You must pass in whether you want full data"; | 
| 37 |  |  |  |  |  |  | my $rules = shift || die "You must pass in at least one rule"; | 
| 38 |  |  |  |  |  |  | my %what  = @_; | 
| 39 |  |  |  |  |  |  | if (defined $what{postUrl} && defined $what{jid}) { | 
| 40 |  |  |  |  |  |  | die "You can only pass in a url or a jid option"; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  | my %opts  = ( | 
| 43 |  |  |  |  |  |  | name     => $name, | 
| 44 |  |  |  |  |  |  | fullData => $full, | 
| 45 |  |  |  |  |  |  | rules    => $rules, | 
| 46 |  |  |  |  |  |  | what     => \%what, | 
| 47 |  |  |  |  |  |  | ); | 
| 48 |  |  |  |  |  |  | return bless \%opts, ref($class) || $class; | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | =head2 name [name] | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | Get or sets the name. | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | =cut | 
| 56 |  |  |  |  |  |  | sub name { shift->_do('name', @_) } | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | =head2 full_data [full] | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | Get or set whether we want full data | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | =cut | 
| 63 |  |  |  |  |  |  | sub full_data { shift->_do('fullData', @_) } | 
| 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 |  |  |  |  |  |  | sub rules { | 
| 81 |  |  |  |  |  |  | my $self = shift; | 
| 82 |  |  |  |  |  |  | my @args; | 
| 83 |  |  |  |  |  |  | if (@_) { | 
| 84 |  |  |  |  |  |  | @args = [@_]; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  | return @{$self->_do('rules', @args) || []}; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =head2 what | 
| 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 |  |  |  |  |  |  | sub what { | 
| 100 |  |  |  |  |  |  | my $self = shift; | 
| 101 |  |  |  |  |  |  | my @args; | 
| 102 |  |  |  |  |  |  | if (@args) { | 
| 103 |  |  |  |  |  |  | my $type  = shift; | 
| 104 |  |  |  |  |  |  | my $value = shift || ""; | 
| 105 |  |  |  |  |  |  | @args = { $type => $value }; | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  | return %{$self->_do('what', @args) || {}}; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =head2 parse | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | Parse some xml into an activity. | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | =cut | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | sub parse { | 
| 117 |  |  |  |  |  |  | my $class  = shift; | 
| 118 |  |  |  |  |  |  | my $xml    = shift; | 
| 119 |  |  |  |  |  |  | my %opts   = @_; | 
| 120 |  |  |  |  |  |  | my $parser = $class->parser(); | 
| 121 |  |  |  |  |  |  | my $doc    = $parser->parse_string($xml); | 
| 122 |  |  |  |  |  |  | my $elem   = $doc->documentElement(); | 
| 123 |  |  |  |  |  |  | return $class->_from_element($elem, %opts); | 
| 124 |  |  |  |  |  |  | } | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | sub _from_element { | 
| 127 |  |  |  |  |  |  | my $class = shift; | 
| 128 |  |  |  |  |  |  | my $elem  = shift; | 
| 129 |  |  |  |  |  |  | my %opts  = @_; | 
| 130 |  |  |  |  |  |  | foreach my $attr ($elem->attributes()) { | 
| 131 |  |  |  |  |  |  | my $name = $attr->name; | 
| 132 |  |  |  |  |  |  | $opts{$name} = $attr->value; | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  | use Data::Dumper; | 
| 135 |  |  |  |  |  |  | my @rules; | 
| 136 |  |  |  |  |  |  | my %what; | 
| 137 |  |  |  |  |  |  | foreach my $child ($elem->childNodes) { | 
| 138 |  |  |  |  |  |  | my $name = $child->nodeName; | 
| 139 |  |  |  |  |  |  | if ('postUrl' eq $name || 'jid' eq $name) { | 
| 140 |  |  |  |  |  |  | $what{$name} = $child->firstChild->textContent; | 
| 141 |  |  |  |  |  |  | } elsif ('rule' eq $name) { | 
| 142 |  |  |  |  |  |  | my $rule; | 
| 143 |  |  |  |  |  |  | push @rules, $class->_parse_rule($child); | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  | return $class->new(delete $opts{name}, delete $opts{fullData}, [@rules], %what); | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | sub _parse_rule { | 
| 150 |  |  |  |  |  |  | my $self = shift; | 
| 151 |  |  |  |  |  |  | my $elem = shift; | 
| 152 |  |  |  |  |  |  | my $rule; | 
| 153 |  |  |  |  |  |  | $rule->{$_} = $elem->getAttribute($_) for qw(type value); | 
| 154 |  |  |  |  |  |  | return $rule; | 
| 155 |  |  |  |  |  |  | } | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | =head2 as_xml | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | Return the activity as xml | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =cut | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | sub as_xml { | 
| 164 |  |  |  |  |  |  | my $self       = shift; | 
| 165 |  |  |  |  |  |  | my $as_element = shift; | 
| 166 |  |  |  |  |  |  | my $element    = XML::LibXML::Element->new('filter'); | 
| 167 |  |  |  |  |  |  | my $what       = delete $self->{what}; | 
| 168 |  |  |  |  |  |  | my $rules      = delete $self->{rules}; | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | foreach my $key (keys %$self) { | 
| 171 |  |  |  |  |  |  | next if '_' eq substr($key, 0, 1); | 
| 172 |  |  |  |  |  |  | my $value = $self->{$key}; | 
| 173 |  |  |  |  |  |  | $element->setAttribute($key, $value); | 
| 174 |  |  |  |  |  |  | } | 
| 175 |  |  |  |  |  |  | if (defined $what && defined [%$what]->[0]) { | 
| 176 |  |  |  |  |  |  | my $tmp = XML::LibXML::Element->new([%$what]->[0]); | 
| 177 |  |  |  |  |  |  | $tmp->appendTextNode([%$what]->[1]); | 
| 178 |  |  |  |  |  |  | $element->addChild($tmp); | 
| 179 |  |  |  |  |  |  | } | 
| 180 |  |  |  |  |  |  | foreach my $rule (@$rules) { | 
| 181 |  |  |  |  |  |  | my $tmp = $self->_create_rule($rule); | 
| 182 |  |  |  |  |  |  | $element->addChild($tmp); | 
| 183 |  |  |  |  |  |  | } | 
| 184 |  |  |  |  |  |  | return ($as_element) ? $element : $element->toString(1); | 
| 185 |  |  |  |  |  |  | } | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | sub _create_rule { | 
| 188 |  |  |  |  |  |  | my $self = shift; | 
| 189 |  |  |  |  |  |  | my $rule = shift; | 
| 190 |  |  |  |  |  |  | my $tmp = XML::LibXML::Element->new('rule'); | 
| 191 |  |  |  |  |  |  | foreach my $key (keys %$rule) { | 
| 192 |  |  |  |  |  |  | $tmp->setAttribute($key, $rule->{$key}); | 
| 193 |  |  |  |  |  |  | } | 
| 194 |  |  |  |  |  |  | return $tmp; | 
| 195 |  |  |  |  |  |  | } | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | 1; | 
| 198 |  |  |  |  |  |  |  |