Daioikachan-Client

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

[![Build Status](https://travis-ci.org/ryopeko/Daioikachan-Client.svg?branch=master)](https://travis-ci.org/ryopeko/Daioikachan-Client)
# NAME

Daioikachan::Client - Client for Daioikachan

# SYNOPSIS

    use Daioikachan::Client;

    my $client = Daioikachan::Client->new({
        endpoint => 'http://daioikachan_endpoint.example.com/',
    });

    # Send message to Daioikachan.
    $client->notice({ message => 'foo' });

# DESCRIPTION

Daioikachan::Client is a client for Daioikachan.

# INTERFACE

## Class Method

### `Daioikachan::Client->new($args) :Daioikachan::Client`

Create and returns a new Daioikachan::Client instance.

_$args_:

- endpoint :Str

    Endpoint of Daioikachan server.
    You must specify a this parameter.

- default\_channel :Str = #notify
- headers :ArrayRef

    This parameter is used by Furl::HTTP request.

- ua\_options :Hash or HashRef

lib/Daioikachan/Client.pm  view on Meta::CPAN

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

    my $ua = Furl::HTTP->new(
        agent   => 'daioikachan-agent',
        timeout => 10,
        defined $args->{ua_options} ? %{$args->{ua_options}} : (),
    );

    my $default_channel = (defined $args->{default_channel} && ($args->{default_channel} ne '')) ? $args->{default_channel} : '#notify';
    my $endpoint = (defined $args->{endpoint} && ($args->{endpoint} ne '')) ? $args->{endpoint} : die 'Undefined endpoint';

    my $headers = $args->{headers};

    return bless {
        ua              => $ua,
        headers         => $headers,
        default_channel => $default_channel,
        endpoint        => $endpoint,
    }, $class;
}

sub notice {
    my ($self, $args) = @_;

    my $message = (defined $args->{message} && ($args->{message} ne '')) ? $args->{message} : die 'Undefined message';

    return $self->_send({
        channel => $args->{channel},

lib/Daioikachan/Client.pm  view on Meta::CPAN

        type    => 'privmsg',
    });
}

sub _send {
    my ($self, $args) = @_;

    my $channel = (defined $args->{channel} && ($args->{channel} ne '')) ? $args->{channel} : $self->{default_channel};

    return $self->{ua}->post(
        $self->{endpoint} . $args->{type},
        $self->{headers},
        {
            channel => $channel,
            message => $args->{message},
        },
    );
}

1;
__END__

lib/Daioikachan/Client.pm  view on Meta::CPAN


=head1 NAME

Daioikachan::Client - Client for Daioikachan

=head1 SYNOPSIS

    use Daioikachan::Client;

    my $client = Daioikachan::Client->new({
        endpoint => 'http://daioikachan_endpoint.example.com/',
    });

    # Send message to Daioikachan.
    $client->notice({ message => 'foo' });

=head1 DESCRIPTION

Daioikachan::Client is a client for Daioikachan.

=head1 INTERFACE

lib/Daioikachan/Client.pm  view on Meta::CPAN

=head2 Class Method

=head3 C<< Daioikachan::Client->new($args) :Daioikachan::Client >>

Create and returns a new Daioikachan::Client instance.

I<$args>:

=over

=item endpoint :Str

Endpoint of Daioikachan server.
You must specify a this parameter.

=item default_channel :Str = #notify

=item headers :ArrayRef

This parameter is used by Furl::HTTP request.

t/01_new.t  view on Meta::CPAN

use warnings;

use Test::More;
use Test::Exception;

use Daioikachan::Client;

subtest 'new' => sub {
    my $class = 'Daioikachan::Client';

    my $endpoint = 'http://daioikachan_endpoint.example.com';

    subtest 'valid params' => sub {
        subtest 'when params has a endpoint' => sub {
            my $instance = $class->new({ endpoint => $endpoint });

            is ref $instance, $class, "should be $class\'s instance";
            is $instance->{endpoint}, $endpoint, 'should be equal endpoint';
            is ref $instance->{ua}, 'Furl::HTTP', 'should have a user agent of Furl::HTTP';
        };

        subtest 'when params has a default_channel' => sub {
            my $default_channel = '#default';
            my $instance = $class->new({
                    endpoint => $endpoint,
                    default_channel => $default_channel,
                });

            is $instance->{default_channel}, $default_channel, 'should be equal default_channel';
        };

        subtest 'when params has a header for ua' => sub {
            my $headers = [ 'x-test-daioikachan-header' => 'foo' ];
            my $instance = $class->new({
                    endpoint => $endpoint,
                    headers => $headers,
                });

            is $instance->{headers}, $headers, 'should be equal headers';
        };

        subtest 'when params has a ua_options' => sub {
            my $user_agent = 'test-daioikachan-agent';
            my $instance = $class->new({
                    endpoint => $endpoint,
                    ua_options => {
                        agent => $user_agent,
                    },
                });

            my %headers = @{$instance->{ua}->{headers}};

            is $headers{"User-Agent"}, $user_agent, 'should be equal user-agent';
        };
    };

    subtest 'invalid params' => sub {
        subtest 'when params does not have a endpoint' => sub {
            throws_ok {
                $class->new;
            } qr/Undefined endpoint/, 'throws undefined endpoint error';
        };
    };
};

done_testing;

t/02_notice.t  view on Meta::CPAN

use warnings;

use Test::More;
use Test::Exception;
use Test::Mock::Guard qw/mock_guard/;

use Daioikachan::Client;

subtest 'notice' => sub {
    my $instance = Daioikachan::Client->new({
            endpoint => 'http://daioikachan_endpoint.example.com',
        });

    subtest 'when arguments is not valid' => sub {
        subtest 'arguments does not have a message' => sub {
            throws_ok {
                $instance->notice;
            } qr/Undefined message/, 'throws undefined message error;'
        };
    };

t/03_privmsg.t  view on Meta::CPAN

use warnings;

use Test::More;
use Test::Exception;
use Test::Mock::Guard qw/mock_guard/;

use Daioikachan::Client;

subtest 'privmsg' => sub {
    my $instance = Daioikachan::Client->new({
            endpoint => 'http://daioikachan_endpoint.example.com',
        });

    subtest 'when arguments is not valid' => sub {
        subtest 'arguments does not have a message' => sub {
            throws_ok {
                $instance->notice;
            } qr/Undefined message/, 'throws undefined message error;'
        };
    };

t/04_send.t  view on Meta::CPAN

use strict;
use warnings;

use Test::More;
use Test::Exception;
use Test::Mock::Guard qw/mock_guard/;

use Daioikachan::Client;

subtest '_send' => sub {
    my $endpoint = 'http://daioikachan_endpoint.example.com';
    my $headers = [ 'x-test-daioikachan-header' => 'foo' ];

    my $instance = Daioikachan::Client->new({
            endpoint => $endpoint,
            headers  => $headers,
        });

    my $message = 'foo';

    my $got = {};
    my $guard = mock_guard('Furl::HTTP', {
            post => sub {
                (my $self, $got->{uri}, $got->{headers}, $got->{params}) = @_;

t/04_send.t  view on Meta::CPAN


    subtest 'when arguments has a message' => sub {
        $instance->_send({
                message => $message,
                type    => 'notice',
            });

        is $got->{params}->{message}, $message, 'message should send to channel';
    };

    subtest 'endpoint' => sub {
        $instance->_send({
                message => $message,
                type    => 'notice',
            });

        like $got->{uri}, qr/$endpoint/, 'message should send to endpoint';
    };

    subtest 'send type' => sub {
        my $type = 'notice';
        $instance->_send({
                message => $message,
                type    => $type,
            });

        like $got->{uri}, qr/$endpoint$type/, 'message should send to endpoint with send type';
    };

    subtest 'headers' => sub {
        $instance->_send({
                message => $message,
                type    => 'notice',
            });

        is_deeply $got->{headers}, $headers, 'message should send to endpoint with headers';
    };
};

done_testing;



( run in 0.298 second using v1.01-cache-2.11-cpan-b61123c0432 )