Acme-ReturnValue
view release on metacpan or search on metacpan
t/pms/Envolve.pm view on Meta::CPAN
use strict;
use warnings;
package Chat::Envolve;
BEGIN {
$Chat::Envolve::VERSION = '1.0007';
}
use Any::Moose;
use MIME::Base64 qw(encode_base64url);
use Digest::HMAC_SHA1 qw(hmac_sha1_hex);
use Encode qw(encode);
has api_key => (
is => 'ro',
required => 1,
trigger => sub {
my ($self, $value) = @_;
confess 'EnvolveAPI: Invalid API Key' unless $value =~ m/\d+-\w+/;
my @key_parts = split /-/, $self->api_key;
$self->secret($key_parts[1]);
$self->site_id($key_parts[0]);
}
);
has secret => (
is => 'rw',
);
has site_id => (
is => 'rw',
);
sub get_tags {
my ($self, $first_name, %options) = @_;
my $command = ($first_name) ? $self->get_login_command($first_name, %options) : $self->get_logout_command;
my $html = q{
<script type="text/javascript">
envoSn=%s;
env_commandString="%s";
</script>
<script type="text/javascript" src="//d.envolve.com/env.nocache.js"></script>
};
return sprintf $html, $self->site_id, $command;
}
sub get_login_command {
my ($self, $first_name, %options) = @_;
my %params = ( fn => $first_name );
$params{ln} = $options{last_name} if exists $options{last_name};
$params{pic} = $options{picture_url} if exists $options{picture_url};
$params{admin} = 't' if exists $options{is_admin} && $options{is_admin};
return $self->sign_command_string(
$self->generate_command_string('login', %params)
);
}
sub get_logout_command {
my ($self) = @_;
return $self->sign_command_string(
$self->generate_command_string('logout')
);
}
sub generate_command_string {
my ($self, $command, %params) = @_;
my $command_string = (time() * 1000)
.';v=0.2'
.',c='.$command;
foreach my $key (keys %params) {
my $value = ($key eq 'admin') ? $params{$key} : encode_base64url(encode("UTF-8",$params{$key}));
$command_string .= ',' . $key . '=' . $value;
chomp $command_string;
}
return $command_string;
}
sub sign_command_string {
my ($self, $command_string) = @_;
my $hash = hmac_sha1_hex( $command_string, $self->secret);
return $hash . ';' . $command_string;
}
no Any::Moose;
__PACKAGE__->meta->make_immutable;
=head1 NAME
Chat::Envolve - A Perl API for the Envolve web chat system.
=head1 VERSION
version 1.0007
=head1 SYNOPSIS
my $chat = Chat::Envolve->new(
api_key => $key,
);
my $html = $chat->get_tags('Joe');
my $command = $chat->get_login_command('Joe');
=head1 DESCRIPTION
This is a Perl API for the Envolve L<http://www.envolve.com> chat system. If you'd like to see it in use, check out The Lacuna Expanse L<http://www.lacunaexpanse.com>. Currently Envolve has not exposed much functionality, but using this API will allo...
=head1 METHODS
=head2 new ( api_key => '111-xxx' )
Constructor. Requires both params.
=over
=item api_key
The API key provided by Envolve.
=back
=head2 get_login_command ( first_name , [ options ] )
Returns a signed login command string that can be used to log a user into a chat by calling some javascript.
<script type="text/javascript">
env_executeCommand(command_string_goes_here);
</script>
If you prefer you can just inline it into the web page using the C<get_tags> method.
=over
=item first_name
A string, either the first name of the user, or their alias.
=item options
( run in 2.140 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )