Skip to content

Commit

Permalink
web client for viewing Mondo transactions
Browse files Browse the repository at this point in the history
just for example. not actually included in the dist uploaded to CPAN
but there in the repo for playing with

bump VERSION and Changes for CPAN release
  • Loading branch information
leejo committed Aug 21, 2016
1 parent c0f3188 commit 579ae90
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Changes
@@ -1,5 +1,9 @@
Revision history for Business-Mondo

0.06 2015-08-21
- live API test is absorbed into emulated test so can run the same
test against both live and emulated endpoints

0.05 2015-07-22
- Use Any type for Booleans as some test failures with Bool related
to incorrect coercion (?) of JSON::PP boolean types (GH #4)
Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Expand Up @@ -3,6 +3,7 @@ Makefile.PL
MANIFEST
README.md
end_to_end_emulated.sh
bin/mondojo
lib/Business/Mondo.pm
lib/Business/Mondo/Account.pm
lib/Business/Mondo/Address.pm
Expand Down
1 change: 1 addition & 0 deletions Makefile.PL
Expand Up @@ -21,6 +21,7 @@ WriteMakefile(
'Data::Currency' => 0.06000,
'DateTime' => 1.26,
'DateTime::Format::DateParse' => 0.05,
'Locale::Currency::Format' => 1.35,
},
TEST_REQUIRES => {
'Test::Most' => 0.31,
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -11,7 +11,7 @@ Business::Mondo - Perl library for interacting with the Mondo API

# VERSION

0.05
0.06

# DESCRIPTION

Expand Down
110 changes: 110 additions & 0 deletions bin/mondojo
@@ -0,0 +1,110 @@
#!perl

use strict;
use warnings;

use Mojolicious::Lite;
use Business::Mondo;
use Business::Mondo::Transaction;
use HTML::GoogleMaps::V3;
use Mojo::JSON qw/ decode_json /;
use File::Slurper qw/ read_text /;

$ENV{MOJO_LOG_LEVEL} = 'debug';

get '/' => sub {
my ( $c ) = @_;

return $c->render( json => [
map { { $_->to_hash } } _get_transactions()
] );
};

get '/map' => sub {
my ( $c ) = @_;

my $map = HTML::GoogleMaps::V3->new( zoom => 8 );
my @transactions = _get_transactions();

foreach my $t ( @transactions ) {
if ( my $m = $t->merchant ) {
if ( my $addr = $m->address ) {
$map->add_marker(
point => [ $addr->longitude,$addr->latitude ]
);

# center around most recent transaction
$map->center( [ $addr->longitude,$addr->latitude ] );
}
}
}

my ( $head,$map_div ) = $map->onload_render;

$c->render(
template => 'map',
head => $head,
map => $map_div,
transactions => [ (reverse(@transactions))[0..7] ],
);
};

app->start;

sub _get_transactions {

my @transactions;

if ( $ENV{MONDOJO_TOKEN} && $ENV{MONDOJO_ACCOUNT_ID} ) {

my $mondo = Business::Mondo->new( token => $ENV{MONDOJO_TOKEN} );
@transactions = $mondo->transactions( account_id => $ENV{MONDOJO_ACCOUNT_ID} );

} elsif ( my $file = $ENV{MONDOJO_TEST_FILE} ) {
my $data = decode_json( Encode::encode( 'UTF-8',read_text( $file ) ) );

foreach my $e ( @{ $data->{transactions} // [] } ) {
push( @transactions,Business::Mondo::Transaction->new(
%{ $e },
client => Business::Mondo->new->client,
) );
}

}

return @transactions;
}

__DATA__
@@ map.css.ep
@@ map.html.ep
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<link href="map.css" rel="stylesheet" />
<%== $head %>
</head>
<body onload="html_googlemaps_initialize()">
<div class="box">
<%== $map %>
<div class="transaction_list">
<% foreach my $transaction ( @{ $transactions } ) { %>
<div class="transaction">
<%= $transaction->merchant->emoji if $transaction->merchant %> /
<%= Encode::decode( 'UTF-8',$transaction->currency ) %> /
<%= Encode::decode( 'UTF-8',$transaction->local_currency ) %> /
<%= $transaction->created %> /
<%= $transaction->merchant->name if $transaction->merchant %>
</div>
<% } %>
</div>
</div>
</body>
</html>
@@ marker.html.ep
<div class="merchant_emoji">
<%== $merchant->name %>
</div>
4 changes: 2 additions & 2 deletions lib/Business/Mondo.pm
Expand Up @@ -11,7 +11,7 @@ Business::Mondo - Perl library for interacting with the Mondo API
=head1 VERSION
0.05
0.06
=head1 DESCRIPTION
Expand Down Expand Up @@ -214,7 +214,7 @@ sub transactions {
return Business::Mondo::Account->new(
client => $self->client,
id => $params{account_id},
)->transactions;
)->transactions( 'expand[]' => 'merchant' );
}

=head2 balance
Expand Down
1 change: 1 addition & 0 deletions lib/Business/Mondo/Currency.pm
Expand Up @@ -26,6 +26,7 @@ has [ qw/ currency local_currency / ] => (
return undef if ! $args;

if ( ! ref( $args ) ) {

$args = Data::Currency->new({
code => $args,
});
Expand Down
8 changes: 5 additions & 3 deletions lib/Business/Mondo/Resource.pm
Expand Up @@ -80,16 +80,18 @@ sub to_hash {

delete( $hash{client} );

if ( blessed( $hash{currency} ) ) {
$hash{currency} = $hash{currency}->code;
foreach my $currency_key ( qw/ currency local_currency / ) {
if ( blessed( $hash{$currency_key} ) ) {
$hash{$currency_key} = $hash{$currency_key}->code;
}
}

foreach my $k ( keys %hash ) {
if ( ref( $hash{$k} ) eq 'DateTime' ) {
$hash{$k} = $hash{$k}->iso8601;
} elsif ( my $blessed = blessed( $hash{$k} ) ) {
next if $blessed =~ /Boolean/;
$hash{$k} = $hash{$k}->to_hash;
$hash{$k} = { $hash{$k}->to_hash };
}
}

Expand Down
18 changes: 18 additions & 0 deletions lib/Business/Mondo/Transaction.pm
Expand Up @@ -22,6 +22,7 @@ use Types::Standard qw/ :all /;
use Business::Mondo::Merchant;
use Business::Mondo::Attachment;
use DateTime::Format::DateParse;
use Locale::Currency::Format;

=head1 ATTRIBUTES
Expand Down Expand Up @@ -186,6 +187,23 @@ sub annotations {
return shift->metadata;
}

sub BUILD {
my ( $self,$args ) = @_;

foreach my $c ( 'local_','' ) {

my $amount_accessor = "${c}amount";
my $currency_accessor = "${c}currency";

if ( my $amount = $self->$amount_accessor ) {
my $decimal_precision = decimal_precision( $self->$currency_accessor->code );
my $value = $amount / ( 10 ** $decimal_precision );
$self->$currency_accessor->value( $value );
}
}

};

=head1 SEE ALSO
L<Business::Mondo>
Expand Down
2 changes: 1 addition & 1 deletion lib/Business/Mondo/Version.pm
Expand Up @@ -15,7 +15,7 @@ use warnings;

use Moo::Role;

$Business::Mondo::VERSION = '0.05';
$Business::Mondo::VERSION = '0.06';
$Business::Mondo::API_VERSION = 'v1';
$Business::Mondo::API_URL = 'https://api.getmondo.co.uk';

Expand Down

0 comments on commit 579ae90

Please sign in to comment.