| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::Gnip::BaseStream; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 5 |  |  | 5 |  | 33 | use strict; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 170 |  | 
| 4 | 5 |  |  | 5 |  | 25 | use base qw(Net::Gnip::Base); | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 2773 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | =head1 NAME | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | Net::Gnip::BaseStream - represent a list of Gnip objects | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | =head1 SYNOPIS | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | # Create a new stream | 
| 14 |  |  |  |  |  |  | my $stream = Net::Gnip::BaseStream->new(); | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # ... or parse from XML | 
| 17 |  |  |  |  |  |  | my $stream = Net::Gnip::BaseStream->parse($xml); | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | # assume that the subclass of BaseStream | 
| 20 |  |  |  |  |  |  | # has children named foo | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | # set the foos | 
| 23 |  |  |  |  |  |  | $stream->foos(@foos); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | # get the filters | 
| 26 |  |  |  |  |  |  | my @foos = $stream->foos; | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | # or use an iterator | 
| 29 |  |  |  |  |  |  | while (my $foo = $stream->next) { | 
| 30 |  |  |  |  |  |  | print $foo->name; | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | $stream->reset; | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | # ... now you can use it again | 
| 36 |  |  |  |  |  |  | while (my $foo = $stream->foo) { | 
| 37 |  |  |  |  |  |  | print $foo->name; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | =head1 METHODS | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | =cut | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | =head2 new | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | Create a new, empty stream | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =cut | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub new { | 
| 52 |  |  |  |  |  |  | my $class = shift; | 
| 53 |  |  |  |  |  |  | my %opts  = @_; | 
| 54 |  |  |  |  |  |  | return bless {%opts}, ref($class) || $class; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | =head2 children [child[ren]] | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | Get or set the children | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | =cut | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | sub children { | 
| 67 |  |  |  |  |  |  | my $self = shift; | 
| 68 |  |  |  |  |  |  | if (@_) { | 
| 69 |  |  |  |  |  |  | $self->{children} = [@_]; | 
| 70 |  |  |  |  |  |  | $self->reset; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  | return @{$self->{children}||[]}; | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =head2 parse | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | Takes a string of XML, parses it and returns a new, | 
| 78 |  |  |  |  |  |  | potentially populated FilterStream | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | =cut | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | sub parse { | 
| 83 |  |  |  |  |  |  | my $class  = shift; | 
| 84 |  |  |  |  |  |  | my $xml    = shift; | 
| 85 |  |  |  |  |  |  | my %opts   = @_; | 
| 86 |  |  |  |  |  |  | my $no_dt  = (ref($class) && $class->{_no_dt}) || $opts{_no_dt}; | 
| 87 |  |  |  |  |  |  | my $parser = $class->parser; | 
| 88 |  |  |  |  |  |  | my $doc    = $parser->parse_string($xml); | 
| 89 |  |  |  |  |  |  | my $elem   = $doc->documentElement(); | 
| 90 |  |  |  |  |  |  | my @children; | 
| 91 |  |  |  |  |  |  | my $name   = $class->_child_name; | 
| 92 |  |  |  |  |  |  | my $cclass = "Net::Gnip::".ucfirst($name); | 
| 93 |  |  |  |  |  |  | my %args; | 
| 94 |  |  |  |  |  |  | foreach my $attr ($elem->attributes()) { | 
| 95 |  |  |  |  |  |  | my $name = $attr->name; | 
| 96 |  |  |  |  |  |  | $args{$name} = $attr->value; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  | foreach my $child ($elem->getChildrenByTagName($name)) { | 
| 99 |  |  |  |  |  |  | push @{$args{children}}, $cclass->_from_element($child, _no_dt => $no_dt); | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | return $class->new( %args ); | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | =head2 next | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | Returns the next Child object | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =cut | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | sub next { | 
| 113 |  |  |  |  |  |  | my $self = shift; | 
| 114 |  |  |  |  |  |  | return $self->{children}->[$self->{_iter}++]; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =head2 reset | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | Resets the iterator | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | =cut | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | sub reset { | 
| 124 |  |  |  |  |  |  | my $self = shift; | 
| 125 |  |  |  |  |  |  | $self->{_iter} = 0; | 
| 126 |  |  |  |  |  |  | return 1; | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =head2 as_xml | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | Return this stream as xml | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | =cut | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | sub as_xml { | 
| 137 |  |  |  |  |  |  | my $self       = shift; | 
| 138 |  |  |  |  |  |  | my $as_element = shift; | 
| 139 |  |  |  |  |  |  | my $name       = $self->_child_name; | 
| 140 |  |  |  |  |  |  | my $elem_name  = $self->_elem_name; | 
| 141 |  |  |  |  |  |  | my $element    = XML::LibXML::Element->new($elem_name); | 
| 142 |  |  |  |  |  |  | foreach my $child (@{delete $self->{children}}) { | 
| 143 |  |  |  |  |  |  | $element->addChild($child->as_xml(1)); | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | foreach my $key (keys %$self) { | 
| 147 |  |  |  |  |  |  | next if '_' eq substr($key, 0, 1); | 
| 148 |  |  |  |  |  |  | my $value = $self->{$key}; | 
| 149 |  |  |  |  |  |  | $element->setAttribute($key, $value); | 
| 150 |  |  |  |  |  |  | } | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | return ($as_element) ? $element : $element->toString(1); | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | 1; |