File Coverage

blib/lib/Business/Mondo/Resource.pm
Criterion Covered Total %
statement 41 41 100.0
branch 9 10 90.0
condition n/a
subroutine 11 11 100.0
pod 3 4 75.0
total 64 66 96.9


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