Subscribed unsubscribe Subscribe Subscribe

目の前に僕らの道がある

勉強会とか、技術的にはまったことのメモ

API経由でWassrをいじってみた。2

Net::Wassrはすでに偉大な先駆者が作成*1されているので、Net::WassrMinusと名付けました。
とりあえず、一通りのAPIは実装したのですが、public_timelineとrepliesのAPIに投げるとはステータス500が返ってきてしまいます。引数とは間違っていないはずなので原因はちょっと分かりません。

コアモジュールはできあがったので、これを利用した何かを作りたいなと思います。

一応、githubにソースを上げておきます。

lib/Net/WassrMinus.pm

package Net::WassrMinus;
use strict;
use warnings;
use utf8;

use Carp;
use JSON;
use LWP::UserAgent;
use Readonly;

use Net::WassrMinus::API;

Readonly my $NET_LOCATON => 'api.wassr.jp:80';
Readonly my $REALM       => 'API Authentication';

my $post = sub {
    my ($ua, $url, $options) = (@_);
    my $response = $ua->post($url, $options);

    croak 'Bad Request: ', $response->as_string if not $response->is_success;

    my $status = decode_json($response->decoded_content);
    croak 'Bad Response: ', $status->{error} if $status->{error};

    return $status;
};

my $get = sub {
    my ($ua, $url, $options) = (@_);
    my $response = $ua->post($url, $options);

    croak 'Bad Request: ', $response->as_string if not $response->is_success;

    return decode_json($response->decoded_content);
};

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

    $options->{user}     ||= '';
    $options->{password} ||= '';

    my $ua = LWP::UserAgent->new;
    $ua->credentials($NET_LOCATON, $REALM, $options->{user}, $options->{password});

    my $self = {
        ua     => $ua,
        source => $options->{source} || __PACKAGE__,
    };

    return bless $self, $class;
}

sub update {
    my ($self, $tweet, $rid, $image) = (@_);
    return $post->(
        $self->{ua}, $UPDATE_API,
        {
            source           => $self->{source},
            status           => $tweet,
            reply_status_rid => $rid   || '',
            image            => $image || '',
        },
    );
}

sub user_timeline {
    my ($self, $user) = (@_);
    return $get->( $self->{ua}, $USER_TIMELINE_API, { id => $user } );
}

sub friends_timeline {
    my ($self, $user, $page) = (@_);
    return $get->(
        $self->{ua}, $FRIENDS_TIMELINE_API,
        {
            id   => $user,
            page => $page || 1,
        },
    );
}

#FIXME: for some resones, status code 500 returns
sub public_timeline {
    my ($self) = (@_);
    return $get->( $self->{ua}, $PUBLIC_TIMELINE_API);
}

#FIXME: for some resones, status code 500 returns
sub replies {
    my ($self) = (@_);
    return $get->( $self->{ua}, $REPLIES_API);
}

sub show {
    my ($self, $user) = (@_);
    return $get->( $self->{ua}, $SHOW_API, { id => $user });
}

1;

lib/Net/WassrMinus/API.pm

package Net::WassrMinus::API;
use strict;
use warnings;
use utf8;

use Exporter;
use Readonly;

our @ISA = qw(Exporter);
our @EXPORT = qw(
    $UPDATE_API  $USER_TIMELINE_API   $FRIENDS_TIMELINE_API
    $REPLIES_API $PUBLIC_TIMELINE_API $SHOW_API
);

Readonly our $BASE_URL             => 'http://api.wassr.jp/statuses/';
Readonly our $UPDATE_API           => $BASE_URL . 'update.json';
Readonly our $USER_TIMELINE_API    => $BASE_URL . 'user_timeline.json';
Readonly our $FRIENDS_TIMELINE_API => $BASE_URL . 'friends_timeline.json';
Readonly our $PUBLIC_TIMELINE_API  => $BASE_URL . 'public_timeline.json';
Readonly our $REPLIES_API          => $BASE_URL . 'replies.json';
Readonly our $SHOW_API             => $BASE_URL . 'show.json';

1;

*1:http://coderepos.org/share/browser/lang/perl/Net-Wassr/trunk