Blitz

 view release on metacpan or  search on metacpan

lib/Blitz.pm  view on Meta::CPAN

package Blitz;

use strict;
use warnings;

use Blitz::API;
use Blitz::Exercise;
use Blitz::Sprint;
use Blitz::Rush;

=head1 NAME

Blitz - Perl module for API access to Blitz

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';

=head1 SYNOPSIS

Blitz provides an interface to the blitz API. Blitz is a
performance and load testing application for testing cloud service apps. 
More information on blitz can be found at http://blitz.io


    use Blitz;

    my $blitz = Blitz->new;

=cut

=head1 SUBROUTINES/METHODS

=head2 new

# Create a new Blitz object

    my $blitz = Blitz->new;

=cut

sub new {
    my $name = shift;
    my $this = shift || {};
    my $self = {
        _authenticated => 0,
        credentials => {
            username => $this->{username},
            api_key   => $this->{api_key},
            host     => $this->{host} || 'blitz.io',
            port     => $this->{port} || 80,
        }
    };
    bless $self;
    return $self;
}


sub _get_set_credentials {
    my $self = shift;
    my $field = shift;
    my $value = shift;
    if ($value) {
        $self->{credentials}{$field} = $value;
    }
    return $self->{credentials}{$field};
}

=head2 username

# Get the currently configured username

    my $user = $blitz->username;

# Set the username

    my $user = $blitz->username('joe@joe.org');

=cut

sub username {
    my $self = shift;
    return _get_set_credentials($self, 'username', @_);
}

=head2 api_key

# Get the currently configured api_key

    my $api_key = $blitz->api_key;

# Set the api_key

    my $api_key = $blitz->api_key('706d5cfbd3338ba110bfg6f46d91f8f3');

=cut

sub api_key {
    my $self = shift;
    return _get_set_credentials($self, 'api_key', @_);
}


=head2 host

# Get the currently configured host

    my $host = $blitz->host;

# Set the host

    my $host = $blitz->host('foo.com');

=cut

sub host {
    my $self = shift;
    return _get_set_credentials($self, 'host', @_);
}

=head2 port

# Get the currently configured port

    my $port = $blitz->port;

# Set the host

    my $port = $blitz->port(8080);

=cut

sub port {
    my $self = shift;
    return _get_set_credentials($self, 'port', @_);
}

=head2 authenticated

Have we been authenticated?

    my $auth = $blitz->authenticated;

=cut

sub authenticated {
    my $self = shift;
    return $self->{_authenticated};
}

=head2 get_client

fetches the existing client object, or 
creates a new Blitz::API->client object

    my $client = $blitz->get_client;
    
=cut

sub get_client {
    my $self = shift;
    if (! $self->{client}) {
        my $client = Blitz::API->client($self->{credentials});
        $self->{client} = $client;
    }
    return $self->{client};
}

sub _run {
    my ($self, $obj, $options, $callback) = @_;
    if ($self->{_authenticated}) {
        my $exercise = $obj->new(
                $self,
                $options,
                $callback
            );
        $exercise->execute();
    }
    else {
        my $client = $self->get_client;
        $client->login(
            sub { 
                my $self = shift;
                my $result = shift;
                if ($result->{ok}) {
                    $self->{_authenticated} = 1;
                    $self->{credentials}{api_key} = $result->{api_key};
                    my $exercise = $obj->new(
                        $self,
                        $options, 
                        $callback
                        );
                    $exercise->execute();
                }
                else {
                    &$callback($result, $result->{error});
                }
            }
        );
    }
}


=head2 sprint

# Sprint

    $blitz->sprint(
        {
            url => 'www.mycoolapp.com',
            region => 'california',
            },
            callback
        }
    );


=cut

sub sprint {
    my $self = shift;
    my $options = shift;
    my $callback = shift;
    _run($self, 'Blitz::Sprint', $options, $callback);
}

=head2 rush

# Rush

    $blitz->rush(
        {
            url => 'www.mycoolapp.com',
            region => 'california',
            pattern => [
                {
                    start => 1,
                    end => 100,
                    duration => 60,
                }
            ]
        },
        callback
        }
    );




( run in 0.866 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )