<commit_before>package Posterous;

use common::sense;
use Moose;

use JSON qw/ decode_json /;
use Carp qw/ croak       /;

use Posterous::Request;
use URI::URL;
use LWP::UserAgent;

use constant baseurl => 'http://posterous.com';

has email       => ( is => 'rw', isa => 'Str', required => 1 );
has password    => ( is => 'rw', isa => 'Str', required => 1 );
has ua          => ( is => 'ro', isa => 'LWP::UserAgent', builder => '_build_ua');
has api_token   => ( is => 'rw', isa => 'Str', lazy_build => 1, builder => '_fetch_api_token');
has api_formats => ( is => 'ro', isa => 'HashRef', builder => '_build_api_formats');

sub _build_ua { return LWP::UserAgent->new(timeout => 10) }

sub _build_api_formats
{
    {
        get_post         => "/api/2/users/%s/sites/%s/posts/%s",
        get_public_posts => "/api/2/users/%s/sites/%s/posts/public",
        sites            => "/api/2/users/%s/sites",
        site             => "/api/2/users/%s/sites/%s",
        delete_site      => "/api/2/users/%s/sites/%s",
        get_site_subscribers => "/api/2/users/1/sites/%s/subscribers",
    }
}

sub _fetch_api_token
{
    my ($self) = @_;

    my $request = Posterous::Request->new(GET => 'http://posterous.com/api/2/auth/token');
    $request->authorization_basic($self->email(), $self->password());

    my $response = $self->_fetch($request);
    return $response->{api_token};
}

sub _fetch
{
    my ($self, $request) = @_;

    my $response = $self->ua()->request($request);

    if ($ENV{DEBUG}) {
        use Data::Dumper;
        print Dumper($response);
    }

    return undef unless $response->is_success();
    return decode_json $response->content();
}

sub _api_url
{
    my ($self, $format_id, @params) = @_;
    return baseurl . sprintf($self->api_formats($format_id), @params);
}

sub sites
{
    my ($self, $user) = @_;
    $user ||= 'me';

    my $request = Posterous::Request->new(GET => $self->_api_url('sites', $user));
    $request->add_api_token($self->api_token());

    my $response = $self->_fetch($request);
    return $response;
}

sub site
{
    my ($self, $user, $site) = @_;
    $user ||= 'me';
    $site ||= 'primary';

    my $request = Posterous::Request->new(GET => $self->_api_url('site', $user, $site));
    $request->add_api_token($self->api_token());

    my $response = $self->_fetch($request);
    return $response;
}

sub delete_site
{
    my ($self, $site, $user) = @_;
    croak "Error: delete_site requests a site to delete!" unless $site;<commit_msg>Fixing some bugs unit testing shook loose.<commit_after>package Posterous;

use common::sense;
use Moose;

use JSON qw/ decode_json /;
use Carp qw/ croak       /;

use Posterous::Request;
use URI::URL;
use LWP::UserAgent;

use constant baseurl => 'http://posterous.com';

has email       => ( is => 'rw', isa => 'Str', required => 1 );
has password    => ( is => 'rw', isa => 'Str', required => 1 );
has ua          => ( is => 'ro', isa => 'LWP::UserAgent', builder => '_build_ua');
has api_token   => ( is => 'rw', isa => 'Str', lazy_build => 1, builder => '_fetch_api_token');
has api_formats => (
    is => 'ro',
    isa => 'HashRef',
    builder => '_build_api_formats',
    traits => ['Hash'],
    handles => {
        get_api_format => 'get',
    },
);

sub _build_ua { return LWP::UserAgent->new(timeout => 10) }

sub _build_api_formats
{
    {
        auth_token           => '/api/2/auth/token',
        get_post             => '/api/2/users/%s/sites/%s/posts/%s',
        get_public_posts     => '/api/2/users/%s/sites/%s/posts/public',
        sites                => '/api/2/users/%s/sites',
        site                 => '/api/2/users/%s/sites/%s',
        delete_site          => '/api/2/users/%s/sites/%s',
        get_site_subscribers => '/api/2/users/1/sites/%s/subscribers',
    }
}

sub _fetch_api_token
{
    my ($self) = @_;

    my $request = Posterous::Request->new(GET => $self->_api_url('auth_token'));
    $request->authorization_basic($self->email(), $self->password());

    my $response = $self->_fetch($request);
    return $response->{api_token};
}

sub _fetch
{
    my ($self, $request) = @_;

    my $response = $self->ua()->request($request);

    if ($ENV{DEBUG}) {
        use Data::Dumper;
        print Dumper($response);
    }

    return undef unless $response->is_success();
    return decode_json $response->content();
}

sub _api_url
{
    my ($self, $format_id, @params) = @_;
    return baseurl . sprintf($self->get_api_format($format_id), @params);
}

sub sites
{
    my ($self, $user) = @_;
    $user ||= 'me';

    my $request = Posterous::Request->new(GET => $self->_api_url('sites', $user));
    $request->add_api_token($self->api_token());

    my $response = $self->_fetch($request);
    return $response;
}

sub site
{
    my ($self, $user, $site) = @_;
    $user ||= 'me';
    $site ||= 'primary';

    my $request = Posterous::Request->new(GET => $self->_api_url('site', $user, $site));
    $request->add_api_token($self->api_token());

    my $response = $self->_fetch($request);
    return $response;
}

sub delete_site
{
    my ($self, $site, $user) = @_;
    croak "Error: delete_site requests a site to delete!" unless $site;
{'fraction_non_alphanumeric': 0.19888117283950618, 'fraction_numerical': 0.005015432098765432, 'mean_word_length': 2.933990895295903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 116, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1627956', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1802, 'n_words': 386}