App-SD

 view release on metacpan or  search on metacpan

lib/App/SD/Replica/hm.pm  view on Meta::CPAN

package App::SD::Replica::hm;
use Any::Moose;
extends 'App::SD::ForeignReplica';
use Params::Validate qw(:all);
use Memoize;
use Prophet::ChangeSet;
use File::Temp 'tempdir';
use Carp;
use Try::Tiny;

has hm               => ( isa => 'Net::Jifty', is => 'rw' );
has remote_url       => ( isa => 'Str',        is => 'rw' );
has foreign_username => ( isa => 'Str',        is => 'rw' );
has props            => ( isa => 'HashRef',    is => 'rw' );

use constant scheme       => 'hm';
use constant pull_encoder => 'App::SD::Replica::hm::PullEncoder';
use constant push_encoder => 'App::SD::Replica::hm::PushEncoder';

# XXX TODO - kill the query requirement by refactoring sub run in the superclass
use constant query => '';

=head2 BUILD

Open a connection to the source identified by C<$self->{url}>.

=cut

# XXX: this should be called from superclass, or better, have individual attributes have their own builders.

sub BUILD {
    my $self = shift;

    # Require rather than use to defer load
    try {
        require Net::Jifty;
    } catch {
        die "SD requires Net::Jifty to sync with a Hiveminder server.\n".
        "'cpan Net::Jifty' may sort this out for you";
    };

    my ( $server, $props ) = $self->{url} =~ m/^hm:(.*?)(?:\|(.*))?$/
        or die
        "Can't parse Hiveminder server spec. Expected hm:http://hiveminder.com or hm:http://hiveminder.com|props";

    my ($username, $password) = $self->extract_auth_from_uri($server);

    if ( $password ) {
        try {
            $self->_hiveminder_login($username, $password);
        } catch {
            die "Bad username or password specified in URL! ".
                "Error message was:\n".
                _hiveminder_clean_login_error($_);
        };
    }
    else {
        ($username, $password) = $self->login_loop(
            uri      => $self->remote_url,
            username => $username,
            # remind the user that hiveminder logins are email addresses
            username_prompt => sub {
                my $uri = shift;
                return "Login email for $uri: ";
            },
            login_callback => \&_hiveminder_login,
            catch_callback => sub {
                my ($verbose_error) = @_;
                warn _hiveminder_clean_login_error($verbose_error);



( run in 3.510 seconds using v1.01-cache-2.11-cpan-75ffa21a3d4 )