Business-Monzo
view release on metacpan or search on metacpan
callback_url => 'http://www.foo.com',
);
$Webhook->delete
}
# pots
my @pots = $monzo->pots();
# attachments
my $Attachment = $monzo->upload_attachment(
file_name => 'foo.png',
file_type => 'image/png',
);
$Attachment->register(
external_id => 'my_id'
);
$Attachment->deregister;
lib/Business/Monzo.pm view on Meta::CPAN
callback_url => 'http://www.foo.com',
);
$Webhook->delete
}
# pots
my @pots = $monzo->pots();
# attachments
my $Attachment = $monzo->upload_attachment(
file_name => 'foo.png',
file_type => 'image/png',
);
$Attachment->register(
external_id => 'my_id'
);
$Attachment->deregister;
lib/Business/Monzo.pm view on Meta::CPAN
Get a list of pots. Will return a list of L<Business::Monzo::Pot>
objects
=cut
sub pots {
my ( $self ) = @_;
return $self->client->_get_pots;
}
sub upload_attachment {
my ( $self,%params ) = @_;
return Business::Monzo::Attachment->new(
client => $self->client,
)->upload( %params );
}
=head1 PAGINATION
As per the Monzo docs: L<https://monzo.com/docs/#pagination> - you can pass
through arguments to the methods (e.g. C<transactions>) to limit the return
data or set date ranges, etc:
# last three months transactions, but only show 5
my $since = DateTime->now->subtract( months => 3 )->iso8601 . "Z";
lib/Business/Monzo/Attachment.pm view on Meta::CPAN
use DateTime::Format::DateParse;
use Business::Monzo::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/Monzo/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::Monzo::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::Monzo::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/Monzo/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;
monzo_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' )
monzo_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 Monzo 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 = $Monzo->balance( account_id => $Account->id ),
'Business::Monzo::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 = $Monzo->upload_attachment(
file_name => 'foo.png',
file_type => 'image/png',
),'Business::Monzo::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::Monzo::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/monzo.t view on Meta::CPAN
);
ok !$proxy_mock->called('detect'), 'UA is reused and proxy not redetected on subsequent runs';
$proxy_mock->clear;
}
sub test_attachment {
my ( $Monzo,$mock,$proxy_mock ) = @_;
isa_ok(
$Monzo->upload_attachment(
file_name => 'foo.png',
file_type => 'image/png',
),
'Business::Monzo::Attachment'
);
ok !$proxy_mock->called('detect'), 'UA is reused and proxy not redetected on subsequent runs';
$proxy_mock->clear;
}
sub _transaction_json {
t/business/monzo/attachment.t view on Meta::CPAN
created
user_id
external_id
file_url
file_type
client
/,
);
throws_ok(
sub { $Attachment->upload },
'Business::Monzo::Exception'
);
is(
$@->message,
'upload requires params: file_name, file_type',
' ... with expected message'
);
no warnings 'redefine';
*Business::Monzo::Client::api_post = sub { {
file_url => 'http://baz',
upload_url => 'http://boz',
} };
isa_ok(
$Attachment = $Attachment->upload(
file_name => 'foo',file_type => 'bar'
),
'Business::Monzo::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::Monzo::Exception'
);
is(
$@->message,
'register requires params: external_id, file_name, file_type',
( run in 1.472 second using v1.01-cache-2.11-cpan-b16cb0d3907 )