Business-Mondo

 view release on metacpan or  search on metacpan

lib/Business/Mondo.pm  view on Meta::CPAN

    }

    return $self->client->_get_transaction( \%params );
}

sub accounts {
    my ( $self ) = @_;
    return $self->client->_get_accounts;
}

sub upload_attachment {
    my ( $self,%params ) = @_;

    return Business::Mondo::Attachment->new(
        client => $self->client,
    )->upload( %params );
}

=head1 SEE ALSO

L<Business::Monzo>

=head1 AUTHOR

Lee Johnson - C<leejo@cpan.org>

lib/Business/Mondo/Attachment.pm  view on Meta::CPAN

use DateTime::Format::DateParse;
use Business::Mondo::Exception;

=head1 ATTRIBUTES

The Attachment class has the following attributes (with their type).

    id (Str)
    user_id (Str)
    external_id (Str)
    upload_url (Str)
    file_name (Str)
    file_url (Str)
    file_type (Str)
    created (DateTime)

Note that when when a Str is passed to ->created this will be coerced
to a DateTime object.

=cut

has [ qw/
    id
    user_id
    external_id
    upload_url
    file_name
    file_url
    file_type
/ ] => (
    is  => 'ro',
    isa => Str,
);

has created => (
    is      => 'ro',

lib/Business/Mondo/Attachment.pm  view on Meta::CPAN

        if ( ! ref( $args ) ) {
            $args = DateTime::Format::DateParse->parse_datetime( $args );
        }

        return $args;
    },
);

=head1 Operations on an attachment

=head2 upload

Gets the upload_url and file_url for the given file_name and file_type.
Returns a new L<Business::Mondo::Attachment> object with the attributes
file_name, file_type, file_url, and upload_url populated. Note the
required parameters:

    my $file_details = $Attachment->upload(
        file_name => 'foo.png',   # REQUIRED
        file_type => 'image/png', # REQUIRED
    );

TODO: should probably make this easier - upload should actually upload a given
file (file handle?)

=cut

sub upload {
    my ( $self,%params ) = @_;

    $params{file_name} && $params{file_type} ||
        Business::Mondo::Exception->throw({
            message => "upload requires params: file_name, file_type",
        });

    my $data = $self->client->api_post( 'attachment/upload',{
        file_name => $params{file_name},
        file_type => $params{file_type},
    } );

    return $self->new(
        client    => $self->client,
        file_name => $params{file_name},
        file_type => $params{file_type},
        %{ $data },
    );

lib/Business/Mondo/Attachment.pm  view on Meta::CPAN


    my $data = $self->client->api_post( 'attachment/register',{
        external_id => $params{external_id},
        file_url    => $params{file_url},
        file_type   => $params{file_type},
    } );

    return $self->new(
        client     => $self->client,
        file_name  => $self->file_name,
        upload_url => $self->upload_url,
        %{ $data->{attachment} },
    );
}

=head2 deregister

Removes an attachment

    $attachment->deregister;

mondo_emulator.pl  view on Meta::CPAN


        $c->render( json => {
            webhooks => _webhooks( $account_id ),
        } );
    };

    del '/webhooks/:webhook_id' => sub {
        shift->render( json => {} );
    };

    post '/attachment/upload' => sub {
        my ( $c ) = @_;

        my $file_name = $c->param( 'file_name' )
            || return $c->render( status => 400, text => "file_name required" );

        my $file_type = $c->param( 'file_type' )
            || return $c->render( status => 400, text => "file_type required" );

        my $url = $c->req->url;

        my $entity_id = "user_00009237hliZellUicKuG1";
        $c->render( json => {
            "file_url" => $url->base . "/file/$entity_id/LcCu4ogv1xW28OCcvOTL-foo.png",
            "upload_url" => $url->base . "/upload/$entity_id/LcCu4ogv1xW28OCcvOTL-foo.png?AWSAccessKeyId=AKIAIR3IFH6UCTCXB5PQ\u0026Expires=1447353431\u0026Signature=k2QeDCCQQHaZeynzYKckejqXRGU%!D(MISSING)"
        } );
    };

    post '/attachment/register' => sub {
        my ( $c ) = @_;

        my $external_id = $c->param( 'external_id' )
            || return $c->render( status => 400, text => "external_id required" );

        my $file_url = $c->param( 'file_url' )

mondo_emulator.pl  view on Meta::CPAN

    post '/attachment/deregister' => sub {
        my ( $c ) = @_;

        my $id = $c->param( 'id' )
            || return $c->render( status => 400, text => "id required" );

        $c->render( json => {} );
    }
};

# convenience methods for file upload emulation, these endpoints
# do not exist in the Mondo API, they are here to fake uploads
get '/file/:entity_id/:file_name' => sub {
    my ( $c ) = @_;

    $c->render( text => "OK" );
};

post '/upload/:entity_id/:file_name' => sub {
    my ( $c ) = @_;

    $c->render( text => "OK" );
};

sub _convert_map_params_to_hash {
    my ( $c,$prefix ) = @_;

    # converts { params[foo] => bar } to { foo => bar }
    # (this is horrible! why not just send JSON in the request body?)

t/002_end_to_end.t  view on Meta::CPAN

	my $Balance = $Mondo->balance( account_id => $Account->id ),
	'Business::Mondo::Balance'
);

like( $Balance->account_id,qr/acc_0000/,'->account_id' );
ok( $Balance->balance,'->balance' );
isa_ok( $Balance->currency,'Data::Currency','->currency' );
is( $Balance->spend_today,0,'->spend_today' );

note( "Attachment" );
isa_ok( my $Attachment = $Mondo->upload_attachment(
	file_name => 'foo.png',
	file_type => 'image/png',
),'Business::Mondo::Attachment' );

is( $Attachment->file_name,'foo.png','->file_name' );
is( $Attachment->file_type,'image/png','->file_type' );
like( $Attachment->file_url,qr/^http/,'->file_url' );
like( $Attachment->upload_url,qr/^http/,'->upload_url' );

isa_ok( $Attachment = $Attachment->register(
	external_id => $Transaction->id,
	file_url    => 'http://www.nyan.cat/cats/original.gif',
	file_type   => 'image/gif',
),'Business::Mondo::Attachment' );

like( $Attachment->user_id,qr/user_/,'->user_id' );
isa_ok( $Attachment->created,'DateTime' );
is( $Attachment->external_id,$Transaction->id,'->id' );
like( $Attachment->id,qr/attach_/,'->id' );
ok( $Attachment->file_name,'->file_name' );
is( $Attachment->file_type,'image/gif','->file_type' );
like( $Attachment->file_url,qr/^http/,'->file_url' );
like( $Attachment->upload_url,qr/^http/,'->upload_url' );

ok( $Attachment->deregister,'->deregister' );

done_testing();

t/business/mondo.t  view on Meta::CPAN

        $Mondo->balance( account_id => 1 ),
        'Business::Mondo::Balance'
    );
}

sub test_attachment {

    my ( $Mondo,$mock ) = @_;

    isa_ok(
        $Mondo->upload_attachment(
            file_name => 'foo.png',
            file_type => 'image/png',
        ),
        'Business::Mondo::Attachment'
    );
}

sub _transaction_json {

    return decode_json( qq{{

t/business/mondo/attachment.t  view on Meta::CPAN

        created
        user_id
        external_id
        file_url
        file_type
        client
    /,
);

throws_ok(
    sub { $Attachment->upload },
    'Business::Mondo::Exception'
);

is(
    $@->message,
    'upload requires params: file_name, file_type',
    ' ... with expected message'
);

no warnings 'redefine';
*Business::Mondo::Client::api_post = sub { {
    file_url => 'http://baz',
    upload_url => 'http://boz',
} };

isa_ok(
    $Attachment = $Attachment->upload(
        file_name => 'foo',file_type => 'bar'
    ),
    'Business::Mondo::Attachment'
);

is( $Attachment->file_name,'foo','->file_name' );
is( $Attachment->file_type,'bar','->file_type' );
is( $Attachment->upload_url,'http://boz','->upload_url' );
is( $Attachment->file_url,'http://baz','->file_url' );

throws_ok(
    sub { $Attachment->register },
    'Business::Mondo::Exception'
);

is(
    $@->message,
    'register requires params: external_id, file_name, file_type',



( run in 1.917 second using v1.01-cache-2.11-cpan-b16cb0d3907 )