WebService::Reflexa - Perl wrapper for Japanese assoc word search engine. (http://labs.preferred.jp/reflexa/)


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

Index


Code Index:

NAME

Top

WebService::Reflexa - Perl wrapper for Japanese assoc word search engine. (http://labs.preferred.jp/reflexa/)

VERSION

Top

version 0.03

SYNOPSIS

Top

    my $service = WebService::Reflexa->new;
    my $words = $service->search(['Perl', 'CPAN']);
    print join("\n", @$words);

METHODS

Top

new($args)

Create instance, $args is hash reference. Key detail is below,

use_xml (optional)

Default 1. Create XML::LibXML and enable xml response.

use_json (optional)

Default 0. Create JSON::Any and enable json response.

search($word[, $format]);

search($words_array_ref[, $format])

Search assoc words by keywords. Return as array reference. Keywords is permitted Array reference or scalar.

If you want to specify format type which is "xml" or "json", then you must specify last argument called $format.

xml_dispatcher()

Return code reference for WWW::REST.

json_dispatcher()

Return code reference for WWW::REST.

rest()

WWW::REST instance.

json()

JSON::Any instance.

xml()

XML::LibXML instance.

result()

Last response string.

SEE ALSO

Top

http://labs.preferred.jp/reflexa/

Reflexa search engine top.

http://labs.preferred.jp/reflexa/about_api.html

About reflexa API

WWW::REST

WWW::REST is great module. But I want to fix 'Subroutine WWW::REST::new redefined' warnings.

XML::LibXML
JSON::Any

AUTHOR

Top

Toru Yamaguchi, <zigorou@cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-webservice-reflexa@rt.cpan.org, or through the web interface at http://rt.cpan.org. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

COPYRIGHT & LICENSE

Top


WebService-Reflexa documentation Contained in the WebService-Reflexa distribution.
package WebService::Reflexa;

use strict;
use warnings;

use base qw(Class::Accessor::Fast);

__PACKAGE__->mk_accessors(qw/rest json xml result/);

use Carp qw(croak);
use Encode;
use JSON::Any qw/XS DWIW Syck JSON/;
use WWW::REST;
use XML::LibXML;

our $API_URI = "http://labs.preferred.jp/reflexa/api.php";

our $VERSION = '0.03';

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

   $args = {
       use_xml => 1,
       use_json => 0,
       (ref $args) ? %$args : ()
   };

    my $self = $class->SUPER::new({
        xml => ($args->{use_xml}) ? XML::LibXML->new : undef,
        json => ($args->{use_json}) ? JSON::Any->new : undef,
        rest => WWW::REST->new($API_URI),
        result => ''
    });

    return $self;
}

sub search {
    my ($self, $words, $format) = @_;

    $format ||= ($self->xml) ? "xml" : ($self->json) ? "json" : "";
    $format = lc($format);

    if ($format eq 'xml') {
        $self->rest->dispatch($self->xml_dispatcher);
    }
    elsif ($format eq 'json') {
        $self->rest->dispatch($self->json_dispatcher);
    }
    else {
        croak("Unknown format $format or not instanciate $format. Please specify use_$format flag to on in new() arguments");
    }

    return $self->rest->get( q => join(" ", (ref $words eq 'ARRAY') ? @$words : $words), format => $format);
}

sub xml_dispatcher {
    my $self = shift;

    return sub {
        my $rest = shift;

        croak($rest->status_line) if $rest->is_error;

        $self->result($rest->content);

        my $doc = $self->xml->parse_string($self->result);
        my $xc = XML::LibXML::XPathContext->new($doc);
        my @nodes = map { encode_utf8($_->data) } $xc->findnodes("//word/text()");

        return \@nodes;
    };
}

sub json_dispatcher {
    my $self = shift;

    return sub {
        my $rest = shift;

        croak($rest->status_line) if $rest->is_error;

        $self->result($rest->content);
        my $result = [map { encode_utf8($_) } @{$self->json->jsonToObj($self->result)}];

        return $result;
    };
}

1; # End of WebService::Reflexa