Net::Douban::Entry - Net::Douban::Entry documentation


Net-Douban documentation Contained in the Net-Douban distribution.

Index


Code Index:

NAME

Top

Net::Douban::Entry

VERSION

Top

version 1.07

SYNOPSIS

Top

	use Net::Douban::Atom;
	my $feed = Net::Douban::Atom->new(\$xml);
	my @entries = $feed->entries;
	$entry->get('title');
	$entry->content;
	$entry->attributes;
	$entry->tags;

DESCRIPTION

Top

This is the parser of douban.com xml entry based on XML::Atom::Entry and Moose

Many functions not listed here are documented in XML::Atom::Entry

METHODS

Top

get

see Net::Douban::Atom

content
	$entry->content();

返回content内容

attributes
	$entry->attributes;

返回包含所有attribute的hash引用

tags
	$entry->tags;

返回包括所有tag的数组引用

rating
	$entry->rating;

返回一个rating的hash引用

SEE ALSO

Top

Net::Douban Net::Douban::Atom Moose XML::Atom http://douban.com/service/apidoc

AUTHOR

Top

woosley.xu<redicaps@gmail.com>

COPYRIGHT

Top


Net-Douban documentation Contained in the Net-Douban distribution.

package Net::Douban::Entry;

BEGIN {
    $Net::Douban::Entry::VERSION = '1.07';
}

use Moose;
use MooseX::StrictConstructor;
use Net::Douban::DBSubject;
use Carp qw/carp croak/;

#use Smart::Comments;
extends qw/XML::Atom::Entry/;

has 'namespace' => (
    is       => 'ro',
    isa      => 'HashRef',
    required => 1,
);

sub new {
    my $class = shift;
    my %args  = @_;
    my $ns    = delete $args{namespace};
    my $self  = $class->SUPER::new(%args);
    return $class->meta->new_object(__INSTANCE__ => $self, namespace => $ns);
}

sub get {
    my $self = shift;
    my ($ns, $field);
    if (@_ == 1) {
        $field = shift;
        if ($field =~ /^(.*?):(.*)$/) {
            $ns    = $self->namespace->{$1};
            $field = $2;
        } else {
            $ns = $self->{ns};
        }
    } else {
        ($ns, $field) = @_;
        $ns = $self->namespace->{$ns} unless $ns =~ m{^http://};
    }
    $ns or croak "No Namespace found!";
    $self->SUPER::get($ns, $field);
}

sub content {
    return $_[0]->SUPER::content->body;
}

sub attributes {
    my $self = shift;
    my $ns   = $self->namespace->{db};
    my %attr;
    foreach my $node ($self->elem->getChildrenByTagNameNS($ns, 'attribute')) {
        $attr{$node->getAttribute('name')} = $node->textContent;
    }
    \%attr;
}

sub tags {
    my $self = shift;
    my $ns   = $self->namespace->{db};
    my @tags;
    foreach my $node ($self->elem->getChildrenByTagNameNS($ns, 'tag')) {
        push @tags, $node->getAttribute('name');
    }
    \@tags;
}

sub rating {
    my $self = shift;
    my %rating;
    my $rate =
      ($self->elem->getChildrenByTagNameNS($self->namespace->{gd}, 'rating'))
      [0];
    foreach my $attr ($rate->attributes) {
        $rating{$attr->nodeName} = $attr->value;
    }
    \%rating;
}

sub subject {
    my $self    = shift;
    my $subject = (
        $self->elem->getChildrenByTagNameNS(
            $self->namespace->{db}, 'subject'
        )
    )[0];
    ### subject: $subject
    return unless ref $subject eq 'XML::LibXML::Element';
    my $k = Net::Douban::DBSubject->new(
        Elem      => $subject,
        namespace => $self->namespace,
    );
    $k->{ns} = $self->namespace->{main};
    return $k;
}

#sub DESTROY { }
#our $AUTOLOAD;
#
#sub AUTOLOAD {
#
#    #	my $self = shift;
#    #	my $class = ref $self ? ref $self : $self;
#    (my $name = $AUTOLOAD) =~ s/.*:://g;
#    return if $name eq 'DESTROY';
#    my $sub = <<SUB;
#	sub $name {
#		my \$self =  shift;
#		return \$self->get($name);
#	}
#SUB
#    eval($sub);    ## the same as *$name = $sub;
#    goto &$name;
#}
1;

__END__