| 1 | package Business::GoCardless::Resource; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | Business::GoCardless::Resource | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | This is a base class for gocardless resource classes, it implements common | ||||||||||||||||||||
| 10 | behaviour. You shouldn't use this class directly, but extend it instead. | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =cut | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 55 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 55 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 55 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 55 | ▶ | use Carp qw/ carp confess /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 55 | ▶ | use JSON (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head2 endpoint | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | The gocardless API endpoint that corresponds to the resource, for example a | ||||||||||||||||||||
| 26 | L<Business::GoCardless::Bill> object will have an endpoint of "bills". This | ||||||||||||||||||||
| 27 | is handled automatically, you do not need to pass this unless the name of the | ||||||||||||||||||||
| 28 | resource differs significantly from the endpoint. | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =head2 client | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | The client object, defaults to L<Business::GoCardless::Client>. | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | has endpoint => ( | ||||||||||||||||||||
| 37 | is => 'ro', | ||||||||||||||||||||
| 38 | 350 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 39 | 350 | my ( $self ) = @_; | |||||||||||||||||||
| 40 | 350 | my ( $class ) = ( split( ':',ref( $self ) ) )[-1]; | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 350 | ▶ | confess( "You must subclass Business::GoCardless::Resource" ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | if $class eq 'Resource'; | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 350 | $class =~ s/([a-z])([A-Z])/$1 . '_' . lc( $2 )/eg; | |||||||||||||||||||
| 46 | 350 | $class = lc( $class ); | |||||||||||||||||||
| 47 | 350 | return "/${class}s/%s"; | |||||||||||||||||||
| 48 | }, | ||||||||||||||||||||
| 49 | 55 | ); | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | has client => ( | ||||||||||||||||||||
| 52 | is => 'ro', | ||||||||||||||||||||
| 53 | 332 | ▶ | isa => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||
| 54 | 332 | ▶ | confess( "$_[0] is not a Business::GoCardless::Client" ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | if ref $_[0] ne 'Business::GoCardless::Client' | ||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||
| 57 | 55 | required => 1, | |||||||||||||||||||
| 58 | ); | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | =head1 METHODS | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =head2 find_with_client | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | Calls the gocardless API and populates the resource object with the data. | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | my $Bill = Business::GoCardless::Bill->new( client => $self->client ); | ||||||||||||||||||||
| 67 | $Bill->find_with_client; | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 21 | ▶ | sub find_with_client { | ||||||||||||||||||
|
Subroutine
find_with_client: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||
| 72 | 21 | my ( $self,$sub_key ) = @_; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 21 | ▶ | my $path = sprintf( $self->endpoint,( $self->id // '' ) ); | ||||||||||||||||||
|
Condition100%2/2
$self->id // ""
MC/DC100%1/1
$self->id // ""
$self->id
Truth table100%2/2
A
A= $self->id
| |||||||||||||||||||||
| 75 | 21 | my $data = $self->client->api_get( $path ); | |||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 21 | ▶ | $data = $data->{$sub_key} if $sub_key; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 21 | foreach my $attr ( keys( %{ $data } ) ) { | |||||||||||||||||||
| 80 | # as per https://developer.gocardless.com/api-reference/#overview-backwards-compatibility | ||||||||||||||||||||
| 81 | # | ||||||||||||||||||||
| 82 | # The following changes are considered backwards compatible | ||||||||||||||||||||
| 83 | # "Adding new properties to the responses from existing API endpoints" | ||||||||||||||||||||
| 84 | # | ||||||||||||||||||||
| 85 | # so we need to be able to handle attributes we don't know about yet, hence the eval | ||||||||||||||||||||
| 86 | 233 | eval { $self->$attr( $data->{$attr} ); }; | |||||||||||||||||||
| 87 | 233 | ▶ | $@ && do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 0 | carp( "Couldn't set $attr on @{[ ref( $self ) ]}: $@" ); | |||||||||||||||||||
| 89 | }; | ||||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 21 | return $self; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 74 | ▶ | sub _operation { | ||||||||||||||||||
|
Subroutine
_operation: called CC=8 SCAR=21.1
| |||||||||||||||||||||
| 96 | 74 | my ( $self,$operation,$method,$params,$suffix ) = @_; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 74 | ▶ | $method //= 'api_post', | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$method //= "api_post"
MC/DC100%1/1
$method //= "api_post"
$method
Truth table100%2/2
A
A= $method
| |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | my $uri = $operation | ||||||||||||||||||||
| 101 | ? sprintf( $self->endpoint,$self->id ) . "/$operation" | ||||||||||||||||||||
| 102 | : sprintf( $self->endpoint,$self->id ); | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 74 | ▶ | $uri .= "/$suffix" if $suffix; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 74 | my $data = $self->client->$method( $uri,$params ); | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 74 | ▶ | if ( $self->client->api_version > 1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | # v2 responses wrap the resource in a key, e.g. { payments => { ... } } | ||||||||||||||||||||
| 110 | 69 | my $key = $self->_resource_key; | |||||||||||||||||||
| 111 | 69 | ▶ | $data = $data->{$key} if ref( $data->{$key} ) eq 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 74 | foreach my $attr ( keys( %{ $data } ) ) { | |||||||||||||||||||
| 115 | 251 | eval { $self->$attr( $data->{$attr} ); }; | |||||||||||||||||||
| 116 | 251 | ▶ | $@ && do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 0 | carp( "Couldn't set $attr on @{[ ref( $self ) ]}: $@" ); | |||||||||||||||||||
| 118 | }; | ||||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 74 | return $self; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | # the following are for the v2 API, and used by the resource classes to | ||||||||||||||||||||
| 125 | # implement their operations (cancel, update, etc) | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | # the key the resource is wrapped in for requests and responses, which is the | ||||||||||||||||||||
| 128 | # first part of the endpoint, e.g. /billing_requests/%s -> billing_requests | ||||||||||||||||||||
| 129 | 86 | ▶ | sub _resource_key { | ||||||||||||||||||
|
Subroutine
_resource_key: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 130 | 86 | my ( $self ) = @_; | |||||||||||||||||||
| 131 | 86 | my ( $key ) = $self->endpoint =~ m!^/([^/]+)!; | |||||||||||||||||||
| 132 | 86 | return $key; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | # POST to an action, e.g. /payments/PM123/actions/cancel, any params for the | ||||||||||||||||||||
| 136 | # action are sent wrapped in a data key as required by the API | ||||||||||||||||||||
| 137 | 51 | ▶ | sub _action { | ||||||||||||||||||
|
Subroutine
_action: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 138 | 51 | my ( $self,$action,%params ) = @_; | |||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 51 | ▶ | return $self->_operation( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | undef,'api_post', | ||||||||||||||||||||
| 142 | ( keys( %params ) ? { data => { %params } } : undef ), | ||||||||||||||||||||
| 143 | "actions/$action", | ||||||||||||||||||||
| 144 | ); | ||||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | # PUT to the resource with the params to update | ||||||||||||||||||||
| 148 | 17 | ▶ | sub _update { | ||||||||||||||||||
|
Subroutine
_update: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 149 | 17 | my ( $self,%params ) = @_; | |||||||||||||||||||
| 150 | 17 | return $self->_operation( | |||||||||||||||||||
| 151 | undef,'api_put',{ $self->_resource_key => { %params } } | ||||||||||||||||||||
| 152 | ); | ||||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | # DELETE the resource | ||||||||||||||||||||
| 156 | 1 | ▶ | sub _delete { | ||||||||||||||||||
|
Subroutine
_delete: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 157 | 1 | my ( $self ) = @_; | |||||||||||||||||||
| 158 | 1 | return $self->_operation( undef,'api_delete' ); | |||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 30 | ▶ | sub uri { | ||||||||||||||||||
|
Subroutine
uri: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 162 | 30 | my ( $self ) = @_; | |||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 30 | my $endpoint = sprintf( $self->endpoint,$self->id ); | |||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 30 | ▶ | return $self->client->api_version > 1 | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 167 | ? join( '/',$self->client->base_url . $self->client->api_path . $endpoint ) | ||||||||||||||||||||
| 168 | : join( '/',$self->client->base_url . $endpoint ); | ||||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =head2 to_hash | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | Returns a hash representation of the object. | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | my %data = $Bill->to_hash; | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | =head2 to_json | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | Returns a json string representation of the object. | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | my $json = $Bill->to_json; | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | =cut | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 2 | ▶ | sub to_hash { | ||||||||||||||||||
|
Subroutine
to_hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 186 | 2 | my ( $self ) = @_; | |||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 2 | my %hash = %{ $self }; | |||||||||||||||||||
| 189 | 2 | delete( $hash{client} ); | |||||||||||||||||||
| 190 | 2 | return %hash; | |||||||||||||||||||
| 191 | } | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | 1 | ▶ | sub to_json { | ||||||||||||||||||
|
Subroutine
to_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 194 | 1 | my ( $self ) = @_; | |||||||||||||||||||
| 195 | 1 | return JSON->new->canonical->encode( { $self->to_hash } ); | |||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | =head1 AUTHOR | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | Lee Johnson - C<lee@payprop.com> | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 203 | the same terms as Perl itself. If you would like to contribute documentation, | ||||||||||||||||||||
| 204 | features, bug fixes, or anything else then please raise an issue / pull request: | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | https://github.com/payprop/business-gocardless | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | 55 | 1; | |||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | # vim: ts=4:sw=4:et | ||||||||||||||||||||