WebService::TagTheNet - Retrieve tags using the tagthe.net REST API


WebService-TagTheNet documentation Contained in the WebService-TagTheNet distribution.

Index


Code Index:

NAME

Top

WebService::TagTheNet - Retrieve tags using the tagthe.net REST API

SYNOPSIS

Top

  use WebService::TagTheNet;

  my $ttn = WebService::TagTheNet->new(count => 5);
  my $result = $ttn->url('http://tagthe.net/');

  print "Tags: ", join ", ", @{$result->{'topic'}};

DESCRIPTION

Top

WebService::TagTheNet - Retrieve tags using the tagthe.net REST API

METHODS

The following methods can be used

new

new creates a new WebService::TagTheNet object.

options

uri

If for some obscure reason you do not want to use the default http://tagthe.net/api/ URI, you can specify your own one here.

agent

The agent string used for LWP::UserAgent. Defaults to 'WebService::TagTheNet v.'.$VERSION

count

Define the amount of topics/tags you'd like to receive. Defaults to 10.

text

Accepts a scalar and will pass that on to tagthe.net for analysis. It returns a hashref with the results (or croaks if something goes wrong).

url

Accepts an URL and will pass that on to tagthe.net for analysis. It returns a hashref with the results (or croaks if something goes wrong).

RETURN VALUES

The hash with API results may contain any of these fields:

text

language
topic
location
person

url

title
size
content-type
author

Please see http://www.tagthe.net/fordevelopers for more information.

SEE ALSO

Top

http://tagthe.net/, http://tagthe.net/fordevelopers

BUGS

Top

Please report any bugs to http://rt.cpan.org/Ticket/Create.html?Queue=WebService::TagTheNet.

AUTHOR

Top

M. Blom, <blom@cpan.org>, http://menno.b10m.net/perl/

COPYRIGHT

Top


WebService-TagTheNet documentation Contained in the WebService-TagTheNet distribution.

package WebService::TagTheNet;

use strict;
use Carp qw/croak/;
use LWP::UserAgent;
use XML::XPath;

our $VERSION = '0.01';

sub new {
   my ($class, %args) = @_;

   my $self = bless ({}, ref ($class) || $class);

   $self->{'_opts'} = {
      uri       => 'http://tagthe.net/api/',
      agent     => 'WebService::TagTheNet v.'.$VERSION,
      count     => 10,
      %args
   };
   $self->{'_lwp'} = LWP::UserAgent->new(agent => $self->{'_opts'}->{'agent'});

   return $self;
}

sub text {
   my ($self, $text) = @_;
   croak "No text found" unless($text);

   return $self->_post('text', $text);
}

sub url {
   my ($self, $url) = @_;
   croak "No URL found" unless($url);

   return $self->_post('url', $url);
}

sub _post {
   my ($self, $k, $v) = @_;
   my $status = $self->{'_lwp'}->post(
      $self->{'_opts'}->{'uri'},
      { $k => $v, count => $self->{'_opts'}->{'count'} }
   );
   croak ("Can't connect to ", $self->{'_opts'}->{'uri'},": ", 
           $status->status_line) unless($status->is_success);
   return $self->_xml2hash($status->content);
}

sub _xml2hash {
   my ($self, $xml) = @_;

   my %result = ();
   my $xp = XML::XPath->new(xml => $xml);
   my $nodeset = $xp->find('/memes/meme/dim');
   foreach my $node ($nodeset->get_nodelist) {
      my $type = $node->getAttribute('type');
      foreach my $child ($node->getChildNodes) {
         next unless(ref $child eq 'XML::XPath::Node::Element');
         push @{$result{$type}}, $child->string_value;
      }
    }
   return \%result;
}
#################### main pod documentation begin ###################

#################### main pod documentation end ###################

1;