| 1 | package Business::Monzo::Resource; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | =head1 NAME | ||||||||||||||
| 4 | |||||||||||||||
| 5 | Business::Monzo::Resource | ||||||||||||||
| 6 | |||||||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||||||
| 8 | |||||||||||||||
| 9 | This is a base class for Monzo resource classes, it implements common | ||||||||||||||
| 10 | behaviour. You shouldn't use this class directly, but extend it instead. | ||||||||||||||
| 11 | |||||||||||||||
| 12 | =cut | ||||||||||||||
| 13 | |||||||||||||||
| 14 | 11 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 11 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 11 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 11 | ▶ | use Carp qw/ confess carp /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 11 | ▶ | use Mojo::JSON qw/ encode_json /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 11 | ▶ | use Scalar::Util qw/ blessed /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 11 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | |||||||||||||||
| 23 | =head1 ATTRIBUTES | ||||||||||||||
| 24 | |||||||||||||||
| 25 | The Resource class has the following attributes (with their type). | ||||||||||||||
| 26 | |||||||||||||||
| 27 | client (Business::Monzo::Client) - REQUIRED | ||||||||||||||
| 28 | url (Str) | ||||||||||||||
| 29 | url_no_id (Str) | ||||||||||||||
| 30 | |||||||||||||||
| 31 | =cut | ||||||||||||||
| 32 | |||||||||||||||
| 33 | has client => ( | ||||||||||||||
| 34 | is => 'ro', | ||||||||||||||
| 35 | 44 | ▶ | isa => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||
| 36 | 44 | ▶ | confess( "$_[0] is not a Business::Monzo::Client" ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | if ref $_[0] ne 'Business::Monzo::Client'; | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 44 | $Business::Monzo::Resource::client = $_[0]; | |||||||||||||
| 40 | }, | ||||||||||||||
| 41 | 11 | required => 1, | |||||||||||||
| 42 | ); | ||||||||||||||
| 43 | |||||||||||||||
| 44 | has [ qw/ url / ] => ( | ||||||||||||||
| 45 | is => 'ro', | ||||||||||||||
| 46 | lazy => 1, | ||||||||||||||
| 47 | 5 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 48 | 5 | my ( $self ) = @_; | |||||||||||||
| 49 | 5 | join( '/',$self->url_no_id,$self->id ) | |||||||||||||
| 50 | }, | ||||||||||||||
| 51 | 11 | ); | |||||||||||||
| 52 | |||||||||||||||
| 53 | has [ qw/ url_no_id / ] => ( | ||||||||||||||
| 54 | is => 'ro', | ||||||||||||||
| 55 | lazy => 1, | ||||||||||||||
| 56 | 5 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 57 | 5 | my ( $self ) = @_; | |||||||||||||
| 58 | 5 | return join( | |||||||||||||
| 59 | '/', | ||||||||||||||
| 60 | $self->client->api_url, | ||||||||||||||
| 61 | lc( ( split( ':',ref( $self ) ) )[-1] ) . 's', | ||||||||||||||
| 62 | ); | ||||||||||||||
| 63 | }, | ||||||||||||||
| 64 | 11 | ); | |||||||||||||
| 65 | |||||||||||||||
| 66 | =head1 METHODS | ||||||||||||||
| 67 | |||||||||||||||
| 68 | =head2 to_hash | ||||||||||||||
| 69 | |||||||||||||||
| 70 | Returns a hash representation of the object. | ||||||||||||||
| 71 | |||||||||||||||
| 72 | my %data = $transaction->to_hash; | ||||||||||||||
| 73 | |||||||||||||||
| 74 | =cut | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 38 | ▶ | sub to_hash { | ||||||||||||
|
Subroutine
to_hash: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||
| 77 | 38 | my ( $self ) = @_; | |||||||||||||
| 78 | |||||||||||||||
| 79 | 38 | my %hash = %{ $self }; | |||||||||||||
| 80 | |||||||||||||||
| 81 | 38 | delete( $hash{client} ); | |||||||||||||
| 82 | |||||||||||||||
| 83 | 38 | foreach my $currency_key ( qw/ currency local_currency / ) { | |||||||||||||
| 84 | 76 | ▶ | if ( blessed( $hash{$currency_key} ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 9 | $hash{$currency_key} = $hash{$currency_key}->code; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 38 | foreach my $k ( keys %hash ) { | |||||||||||||
| 90 | 266 | ▶ | if ( ref( $hash{$k} ) eq 'DateTime' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 91 | 29 | $hash{$k} = $hash{$k}->iso8601; | |||||||||||||
| 92 | } elsif ( my $blessed = blessed( $hash{$k} ) ) { | ||||||||||||||
| 93 | 24 | ▶ | next if $blessed =~ /Boolean/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | 12 | $hash{$k} = { $hash{$k}->to_hash }; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | 38 | return %hash; | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | =head2 as_json | ||||||||||||||
| 102 | |||||||||||||||
| 103 | Returns a json string representation of the object. | ||||||||||||||
| 104 | |||||||||||||||
| 105 | my $json = $transaction->as_json; | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =cut | ||||||||||||||
| 108 | |||||||||||||||
| 109 | 8 | ▶ | sub as_json { | ||||||||||||
|
Subroutine
as_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 110 | 8 | my ( $self ) = @_; | |||||||||||||
| 111 | |||||||||||||||
| 112 | 8 | return encode_json( { $self->to_hash } ); | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | # for JSON encoding modules (convert_blessed) | ||||||||||||||
| 116 | 10 | ▶ | sub TO_JSON { shift->to_hash; } | ||||||||||||
|
Subroutine
TO_JSON: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 117 | |||||||||||||||
| 118 | =head2 get | ||||||||||||||
| 119 | |||||||||||||||
| 120 | Returns a new instance of the object populated with the attributes having called | ||||||||||||||
| 121 | the API | ||||||||||||||
| 122 | |||||||||||||||
| 123 | my $populated_object = $object->get; | ||||||||||||||
| 124 | |||||||||||||||
| 125 | This is for when you have instantiated an object with the id, so calling the API | ||||||||||||||
| 126 | will retrieve the full details for the entity. | ||||||||||||||
| 127 | |||||||||||||||
| 128 | =cut | ||||||||||||||
| 129 | |||||||||||||||
| 130 | 2 | ▶ | sub get { | ||||||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 131 | 2 | my ( $self,$sub_key ) = @_; | |||||||||||||
| 132 | |||||||||||||||
| 133 | 2 | my $data = $self->client->api_get( $self->url ); | |||||||||||||
| 134 | 2 | ▶ | $data = $data->{$sub_key} if $sub_key; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 135 | |||||||||||||||
| 136 | return $self->new( | ||||||||||||||
| 137 | client => $self->client, | ||||||||||||||
| 138 | 2 | %{ $data }, | |||||||||||||
| 139 | ); | ||||||||||||||
| 140 | } | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =head1 AUTHOR | ||||||||||||||
| 143 | |||||||||||||||
| 144 | Lee Johnson - C<leejo@cpan.org> | ||||||||||||||
| 145 | |||||||||||||||
| 146 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 147 | the same terms as Perl itself. If you would like to contribute documentation, | ||||||||||||||
| 148 | features, bug fixes, or anything else then please raise an issue / pull request: | ||||||||||||||
| 149 | |||||||||||||||
| 150 | https://github.com/leejo/business-monzo | ||||||||||||||
| 151 | |||||||||||||||
| 152 | =cut | ||||||||||||||
| 153 | |||||||||||||||
| 154 | 11 | 1; | |||||||||||||
| 155 | |||||||||||||||
| 156 | # vim: ts=4:sw=4:et | ||||||||||||||