Chat-iFly
view release on metacpan or search on metacpan
lib/Chat/iFly.pm view on Meta::CPAN
use strict;
use warnings;
package Chat::iFly;
use HTTP::Thin;
use HTTP::Request::Common;
use JSON;
use URI;
use Ouch;
use Moo;
my $convert_to_string = sub { "$_[0]" };
=head1 NAME
Chat::iFly - An interface to the iFlyChat service.
=head1 SYNOPSIS
use Chat::iFly;
my $chat = Chat::iFly->new(
api_key => 'afsdadfafdsadfsafsd',
static_asset_base_uri => '//www.myserver.com/ifly',
ajax_uri => '//www.myserver.com/chat/login'
);
my $user = {
id => 4321,
name => 'Joe Blow',
avatar_uri => '//www.myserver.com/uploads/joe.blow.avatar.jpg',
profile_uri => '//www.myserver.com/users/4321',
};
my $html_to_inline_into_page = $chat->render_html($user);
my $response_to_chat_login = $chat->render_ajax($user);
=head1 DESCRIPTION
A wrapper needed to authenticate to L<iflychat.com>.
=head2 Setup
You'll need to go here L<https://iflychat.com/iflyapi/index> and register for an API Key. You'll specify that using C<api_key> passed to the constructor.
You'll need to copy the C<public> folder from this distribution onto your web server somewhere. You'll specify where that is using the C<static_asset_base_uri> passed tot he constructor.
You'll need to build 2 things into your web server:
=over
=item Inline HTML
You'll need to inline the result of C<render_html> into any web page where you want the chat to appear.
=item AJAX Method
You'll need to set up an ajax method in your app that returns the result of C<render_ajax>. You'll pass the URL where that can be found into the constructor using C<ajax_uri>.
=back
And finally you need to call C<update_settings> to tell the iFly servers what your settings are.
=head1 METHODS
lib/Chat/iFly.pm view on Meta::CPAN
sub get {
my ($self, $path, $params) = @_;
my $uri = $self->_create_uri($path);
$uri->query_form($params);
return $self->_process_request( GET $uri );
}
=head2 post(path, params)
Performs a C<POST> request, which is used for creating data in the service.
=over
=item path
The path to the REST interface you wish to call.
=item params
A hash reference of parameters you wish to pass to the web service.
=back
=cut
sub post {
my ($self, $path, $params) = @_;
my $uri = $self->_create_uri($path);
return $self->_process_request( POST $uri->as_string, Content_Type => 'application/json', Content => to_json($params) );
}
sub _create_uri {
my $self = shift;
my $path = shift;
return URI->new($self->uri.$path);
}
sub _process_request {
my $self = shift;
$self->_process_response($self->agent->request( @_ ));
}
sub _process_response {
my $self = shift;
my $response = shift;
if ($response->is_success) {
return $response->decoded_content;
}
else {
warn $response->decoded_content;
ouch $response->code, $response->message, $response->decoded_content;
}
}
=head1 PREREQS
L<HTTP::Thin>
L<Ouch>
L<HTTP::Request::Common>
L<JSON>
L<URI>
L<Moo>
=head1 SUPPORT
=over
=item Repository
L<http://github.com/rizen/Chat-iFly>
=item Bug Reports
L<http://github.com/rizen/Chat-iFly/issues>
=back
=head1 AUTHOR
JT Smith <jt_at_plainblack_dot_com>
=head1 LEGAL
This module is Copyright 2014 Plain Black Corporation. It is distributed under the same terms as Perl itself.
=cut
1;
( run in 0.966 second using v1.01-cache-2.11-cpan-437f7b0c052 )