File Coverage

blib/lib/PayProp/API/Public/Client/Request/Tags.pm
Criterion Covered Total %
statement 69 69 100.0
branch 10 10 100.0
condition 13 26 50.0
subroutine 19 19 100.0
pod 7 7 100.0
total 118 131 90.0


line stmt bran cond sub pod time code
1             package PayProp::API::Public::Client::Request::Tags;
2              
3 9     9   2258589 use strict;
  9         26  
  9         406  
4 9     9   51 use warnings;
  9         26  
  9         820  
5              
6 9     9   67 use Mouse;
  9         17  
  9         87  
7             with qw/ PayProp::API::Public::Client::Role::APIRequest /;
8              
9 9     9   11900 use PayProp::API::Public::Client::Response::Tag;
  9         1053  
  9         10678  
10              
11              
12             has '+url' => (
13             default => sub {
14             my ( $self ) = @_;
15             return $self->abs_domain . '/api/agency/' . $self->api_version . '/tags';
16             },
17             );
18              
19             sub list_p {
20 7     7 1 168 my ( $self, $args ) = @_;
21              
22 7   50     672 $args //= {};
23 7         49 my $params = $args->{params};
24              
25             return $self
26             ->api_request_p({
27             params => $params,
28 7     7   133 handle_response_cb => sub { $self->_get_tags( @_ ) },
29             })
30 7         329 ;
31             }
32              
33             sub create_p {
34 6     6 1 228 my ( $self, $args ) = @_;
35              
36 6   50     60 $args //= {};
37 6         30 my $content = $args->{content};
38              
39             return $self
40             ->api_request_p({
41             method => 'POST',
42             content => { json => $content },
43 6     6   174 handle_response_cb => sub { $self->_get_tags( @_ ) },
44             })
45 6         792 ;
46             }
47              
48             sub link_entities_p {
49 5     5 1 340 my ( $self, $args ) = @_;
50              
51 5   50     105 $args //= {};
52 5         55 my $content = $args->{content};
53 5   50     75 my $path_params = $args->{path_params} // {};
54              
55 5         45 $path_params->{fragment} = 'entities';
56 5         1150 $self->ordered_path_params([qw/ fragment entity_type entity_id /]);
57              
58             return $self
59             ->api_request_p({
60             method => 'POST',
61             path_params => $path_params,
62             content => { json => $content },
63 5     5   60 handle_response_cb => sub { $self->_get_tags( @_ ) },
64             })
65 5         285 ;
66             }
67              
68             sub list_tagged_entities_p {
69 4     4 1 268 my ( $self, $args ) = @_;
70              
71 4   50     56 $args //= {};
72 4         48 my $params = $args->{params};
73 4   50     72 my $path_params = $args->{path_params} // {};
74              
75 4         44 $path_params->{fragment} = 'entities';
76 4         140 $self->ordered_path_params([qw/ external_id fragment /]);
77              
78             return $self
79             ->api_request_p({
80             method => 'GET',
81             params => $params,
82             path_params => $path_params,
83 4     4   72 handle_response_cb => sub { $self->_get_tags( @_ ) },
84             })
85 4         244 ;
86             }
87              
88             sub update_p {
89 3     3 1 225 my ( $self, $args ) = @_;
90              
91 3   50     93 $args //= {};
92 3         36 my $req_content = $args->{content};
93 3   50     48 my $path_params = $args->{path_params} // {};
94              
95 3         99 $self->ordered_path_params([qw/ external_id /]);
96              
97             return $self
98             ->api_request_p({
99             method => 'PUT',
100             path_params => $path_params,
101             content => { json => $req_content },
102 3     3   66 handle_response_cb => sub { $self->_get_tags( @_ ) },
103             })
104 3         237 ;
105             }
106              
107             sub delete_p {
108 2     2 1 96 my ( $self, $args ) = @_;
109              
110 2   50     36 $args //= {};
111 2   50     38 my $path_params = $args->{path_params} // {};
112              
113 2         56 $self->ordered_path_params([qw/ external_id /]);
114              
115             return $self
116             ->api_request_p({
117             method => 'DELETE',
118             path_params => $path_params,
119 2     2   38 handle_response_cb => sub { $self->_get_tags( @_ ) },
120             })
121 2         102 ;
122             }
123              
124             sub delete_entity_link_p {
125 1     1 1 44 my ( $self, $args ) = @_;
126              
127 1   50     11 $args //= {};
128 1         5 my $params = $args->{params};
129 1   50     6 my $path_params = $args->{path_params} // {};
130              
131 1         9 $path_params->{fragment} = 'entities';
132 1         18 $self->ordered_path_params([qw/ external_id fragment /]);
133              
134             return $self
135             ->api_request_p({
136             params => $params,
137             method => 'DELETE',
138             path_params => $path_params,
139 1     1   8 handle_response_cb => sub { $self->_get_tags( @_ ) },
140             })
141 1         59 ;
142             }
143              
144             sub _get_tags {
145 28     28   97 my ( $self, $response_json ) = @_;
146              
147 28 100       141 return $response_json if $response_json->{message};
148              
149 25 100       102 my $want_array = ref $response_json->{items} ? 1 : 0;
150              
151             return PayProp::API::Public::Client::Response::Tag->new(
152             id => $response_json->{id},
153             name => $response_json->{name},
154 25 100       488 ) unless $want_array;
155              
156 16         27 my @tags;
157 16   50     571 for my $tag ( @{ $response_json->{items} // [] } ) {
  16         136  
158             my $Tag = PayProp::API::Public::Client::Response::Tag->new(
159             id => $tag->{id},
160             name => $tag->{name},
161              
162             ( $tag->{type} ? ( type => $tag->{type} ) : () ),
163 32 100       975 ( $tag->{links} ? ( links => $tag->{links} ) : () ),
    100          
164             );
165              
166 32         1541 push( @tags, $Tag );
167             }
168              
169 16         97 return \@tags;
170             }
171              
172             __PACKAGE__->meta->make_immutable;
173              
174             __END__