Blitz

 view release on metacpan or  search on metacpan

lib/Blitz.pm  view on Meta::CPAN


    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

lib/Blitz.pm  view on Meta::CPAN

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,

lib/Blitz.pm  view on Meta::CPAN

        $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});
                }

lib/Blitz/API.pm  view on Meta::CPAN

Blitz::API - Perl module for API access to Blitz

=cut

=head1 SUBROUTINES/METHODS

=head2 client

Create a blitz client object for executing sprints or rushes

Required parameters are credentials (email address and api-key)

See http://blitz.io for information regarding the api key.

=cut

sub client {
    my $this = shift;
    my $creds = shift;
    my $self = {
        credentials => $creds,
        job_id => undef,
        job_status => 'idle',
    };
    bless $self;
    return $self;
}

=head2 status

status is a get/set method for the current status of a running exercise

lib/Blitz/API.pm  view on Meta::CPAN

    if ($status) {
        $self->{job_status} = $status;
    }
    
    return $self->{job_status};
}

sub _make_url {
    my $self = shift;
    my $path = shift;
    my $url = "http://" . $self->{credentials}{host}; # fix for https later?
    $url .= ":$self->{credentials}{port}" if $self->{credentials}{port} != 80;
    $url .= $path if $path;
    return $url;
}

sub _http_get {
    my $self = shift;
    my $path = shift;

    my $browser = LWP::UserAgent->new;
    my $url = _make_url($self, $path);
    my $response = $browser->get($url,
        'X-API-User'   => $self->{credentials}{username},
        'X-API-Key'    => $self->{credentials}{api_key},
        'X-API-Client' => 'Perl',
    );
    
    return $response;
}

=head2 _decode_response

Decodes the JSON result object from Blitz
Decodes base64 response in content areas of request and response

lib/Blitz/API.pm  view on Meta::CPAN

    my $self = shift;
    my $data = shift;
    my $closure = $self->{callback};
    
    my $json = encode_json($data);
        
    my $browser = LWP::UserAgent->new;
    my $url = _make_url($self, '/api/1/curl/execute');

    my $response = $browser->post($url,
        'X-API-User'     => $self->{credentials}{username},
        'X-API-Key'      => $self->{credentials}{api_key},
        'X-API-Client'   => 'Perl',
        'content-length' => length($json),
        Content          => $json,
    );
    my $result = _decode_response($self, $response);
    
    if ($closure) {
        &$closure($self, $result);
    }
    return $result;



( run in 0.264 second using v1.01-cache-2.11-cpan-4d50c553e7e )