From 579ae902351cb10f358819d631696e7d38e635a8 Mon Sep 17 00:00:00 2001 From: Lee Johnson Date: Sun, 29 May 2016 16:11:51 +0200 Subject: [PATCH] web client for viewing Mondo transactions 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 --- Changes | 4 ++ MANIFEST | 1 + Makefile.PL | 1 + README.md | 2 +- bin/mondojo | 110 ++++++++++++++++++++++++++++++ lib/Business/Mondo.pm | 4 +- lib/Business/Mondo/Currency.pm | 1 + lib/Business/Mondo/Resource.pm | 8 ++- lib/Business/Mondo/Transaction.pm | 18 +++++ lib/Business/Mondo/Version.pm | 2 +- 10 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 bin/mondojo diff --git a/Changes b/Changes index a755c03..19f8bd8 100644 --- a/Changes +++ b/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) diff --git a/MANIFEST b/MANIFEST index 67073d5..86650c5 100644 --- a/MANIFEST +++ b/MANIFEST @@ -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 diff --git a/Makefile.PL b/Makefile.PL index d039d94..6c899a2 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -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, diff --git a/README.md b/README.md index 420bc41..9f1e2b7 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Business::Mondo - Perl library for interacting with the Mondo API # VERSION -0.05 +0.06 # DESCRIPTION diff --git a/bin/mondojo b/bin/mondojo new file mode 100644 index 0000000..4f090a1 --- /dev/null +++ b/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 + + + + + + <%== $head %> + + +
+ <%== $map %> +
+ <% foreach my $transaction ( @{ $transactions } ) { %> +
+ <%= $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 %> +
+ <% } %> +
+
+ + + +@@ marker.html.ep +
+ <%== $merchant->name %> +
diff --git a/lib/Business/Mondo.pm b/lib/Business/Mondo.pm index 9f9de8d..d47814f 100644 --- a/lib/Business/Mondo.pm +++ b/lib/Business/Mondo.pm @@ -11,7 +11,7 @@ Business::Mondo - Perl library for interacting with the Mondo API =head1 VERSION -0.05 +0.06 =head1 DESCRIPTION @@ -214,7 +214,7 @@ sub transactions { return Business::Mondo::Account->new( client => $self->client, id => $params{account_id}, - )->transactions; + )->transactions( 'expand[]' => 'merchant' ); } =head2 balance diff --git a/lib/Business/Mondo/Currency.pm b/lib/Business/Mondo/Currency.pm index d83cb85..abb98d2 100644 --- a/lib/Business/Mondo/Currency.pm +++ b/lib/Business/Mondo/Currency.pm @@ -26,6 +26,7 @@ has [ qw/ currency local_currency / ] => ( return undef if ! $args; if ( ! ref( $args ) ) { + $args = Data::Currency->new({ code => $args, }); diff --git a/lib/Business/Mondo/Resource.pm b/lib/Business/Mondo/Resource.pm index 3c5bbf7..f9d75b6 100644 --- a/lib/Business/Mondo/Resource.pm +++ b/lib/Business/Mondo/Resource.pm @@ -80,8 +80,10 @@ 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 ) { @@ -89,7 +91,7 @@ sub to_hash { $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 }; } } diff --git a/lib/Business/Mondo/Transaction.pm b/lib/Business/Mondo/Transaction.pm index ba811b6..885df97 100644 --- a/lib/Business/Mondo/Transaction.pm +++ b/lib/Business/Mondo/Transaction.pm @@ -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 @@ -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 diff --git a/lib/Business/Mondo/Version.pm b/lib/Business/Mondo/Version.pm index 6723e41..b6faf1a 100644 --- a/lib/Business/Mondo/Version.pm +++ b/lib/Business/Mondo/Version.pm @@ -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';