GrowthForecast-Aggregator-Declare

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

                    descriptions => ['Total count of posts', 'Posted bloggers'],
                    query => 'SELECT COUNT(*), COUNT(DISTINCT member_id) FROM entry',
                );
            };
        };
        for my $query (@queries) {
            $query->run(
                dbh => $dbh,
                ua  => $ua,
                service => 'blog_service',
                endpoint => 'http://exapmle.com/api/',
            );
        }

DESCRIPTION
    GrowthForecast::Aggregator::Declare is a declarative client library for
    GrowthForecast

DSL
    gf { ... }
        This makes a scope to declare GrowthForecast metrics.

lib/GrowthForecast/Aggregator/Callback.pm  view on Meta::CPAN

    required => 1,
);

no Mouse;

sub run {
    my $self = shift;
    my %args = @_;

    my $service  = $args{service}  // die "Missing mandatory parameter: service";
    my $endpoint = $args{endpoint} // die "Missing mandatory parameter: endpoint";
    my $ua       = $args{ua}       // die "Missing mandatory parameter: ua";

    $endpoint =~ s!/$!!;

    my $url = "$endpoint/$service/$self->{section}/$self->{name}";

    my ($number) = $self->code->();
    my $req = POST $url, [
        number => $number,
        description => encode_utf8($self->description),
    ];
    my $res = $ua->request($req);
    return $res;
}

lib/GrowthForecast/Aggregator/Callback.pm  view on Meta::CPAN

=head1 ARGUMENTS FOR 'run' METHOD

=over 4

=item service

Service name.

This module send request to "/api/$service/$section/$name"

=item endpoint

Endpoint URL, contains '/api'.

E.g. http://example.com/api/

=item ua

Instance of HTTP client. I tested on L<Furl>.

=back

lib/GrowthForecast/Aggregator/DB.pm  view on Meta::CPAN

);

no Mouse;

sub run {
    my $self = shift;
    my %args = @_;

    my $dbh      = $args{dbh}      // die "Missing mandatory parameter: dbh";
    my $service  = $args{service}  // die "Missing mandatory parameter: service";
    my $endpoint = $args{endpoint} // die "Missing mandatory parameter: endpoint";
    my $ua       = $args{ua}       // die "Missing mandatory parameter: ua";

    $endpoint =~ s!/$!!;

    my $url = "$endpoint/$service/$self->{section}/$self->{name}";

    my $sth = $dbh->prepare($self->query);
    $sth->execute(@{$self->binds});
    my ($number) = $sth->fetchrow_array();
    my $req = POST $url, [
        number => $number,
        description => encode_utf8($self->description),
    ];
    my $res = $ua->request($req);
    return $res;

lib/GrowthForecast/Aggregator/DB.pm  view on Meta::CPAN

=item dbh

Data source database handle.

=item service

Service name.

This module send request to "/api/$service/$section/$name"

=item endpoint

Endpoint URL, contains '/api'.

E.g. http://example.com/api/

=item ua

Instance of HTTP client. I tested on L<Furl>.

=back

lib/GrowthForecast/Aggregator/DBMulti.pm  view on Meta::CPAN

    default => sub { +[ ] },
);

no Mouse;

sub run {
    my $self = shift;
    my %args = @_;
    my $dbh = $args{dbh} // die "Missing mandatory parameter: dbh";
    my $service = $args{service} // die "Missing mandatory parameter: service";
    my $endpoint = $args{endpoint} // die "Missing mandatory parameter: endpoint";
    my $ua = $args{ua} // die "Missing mandatory parameter: ua";

    $endpoint =~ s!/$!!;

    my @numbers = $dbh->selectrow_array($self->query, {}, @{$self->binds});

    my @res;
    for (my $i=0; $i<@{$self->names}; $i++) {
        my $name = $self->names->[$i];

        my $url = "$endpoint/$service/$self->{section}/$name";

        my $req = POST $url, [
            number => $numbers[$i],
            description => encode_utf8($self->descriptions->[$i]),
        ];
        my $res = $ua->request($req);
        push @res, $res;
    }
    return @res;
}

lib/GrowthForecast/Aggregator/DBMulti.pm  view on Meta::CPAN

=item dbh

Data source database handle.

=item service

Service name.

This module send request to "/api/$service/$section/$name"

=item endpoint

Endpoint URL, contains '/api'.

E.g. http://example.com/api/

=item ua

Instance of HTTP client. I tested on L<Furl>.

=back

lib/GrowthForecast/Aggregator/Declare.pm  view on Meta::CPAN

                descriptions => ['Total count of posts', 'Posted bloggers'],
                query => 'SELECT COUNT(*), COUNT(DISTINCT member_id) FROM entry',
            );
        };
    };
    for my $query (@queries) {
        $query->run(
            dbh => $dbh,
            ua  => $ua,
            service => 'blog_service',
            endpoint => 'http://exapmle.com/api/',
        );
    }

=head1 DESCRIPTION

GrowthForecast::Aggregator::Declare is a declarative client library for L<GrowthForecast>

=head1 DSL

=over 4

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

    my $ua = LWP::UserAgent->new();
    no warnings 'redefine';
    my @REQ;
    local *LWP::UserAgent::request = sub {
        push @REQ, $_[1];
        return HTTP::Response->new;
    };
    for (@queries) {
        $_->run(
            service => 'test',
            endpoint => 'http://gf/api/',
            ua => $ua,
        );
    }
    is(0+@REQ, 1);
    is($REQ[0]->uri, 'http://gf/api/test/member/count');
    like($REQ[0]->content, qr/description=member\+count/);
    like($REQ[0]->content, qr/number=4649/);
    note $REQ[0]->content;
};

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

    my @REQ;
    local *LWP::UserAgent::request = sub {
        push @REQ, $_[1];
        return HTTP::Response->new;
    };
    my $dbh = setup_db();
    for (@queries) {
        $_->run(
            dbh => $dbh,
            service => 'test',
            endpoint => 'http://gf/api/',
            ua => $ua,
        );
    }
    is(0+@REQ, 1);
    is($REQ[0]->uri, 'http://gf/api/test/member/count');
    like($REQ[0]->content, qr/description=member\+count/);
    like($REQ[0]->content, qr/number=4/);
    note $REQ[0]->content;
};

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

    my @REQ;
    local *LWP::UserAgent::request = sub {
        push @REQ, $_[1];
        return HTTP::Response->new;
    };
    my $dbh = setup_db();
    for (@queries) {
        $_->run(
            dbh => $dbh,
            service => 'test',
            endpoint => 'http://gf/api/',
            ua => $ua,
        );
    }
    is(0+@REQ, 2);
    is($REQ[0]->uri, 'http://gf/api/test/entry/count');
    like($REQ[0]->content, qr/description=entry\+count/);
    like($REQ[0]->content, qr/number=4/);
    note $REQ[0]->content;

    is($REQ[1]->uri, 'http://gf/api/test/entry/unique');



( run in 1.236 second using v1.01-cache-2.11-cpan-2b1a40005be )