File Coverage

blib/lib/WebService/Freshservice/User.pm
Criterion Covered Total %
statement 43 123 34.9
branch 0 40 0.0
condition n/a
subroutine 22 34 64.7
pod n/a
total 65 197 32.9


line stmt bran cond sub pod time code
1             package WebService::Freshservice::User;
2              
3 2     2   154047 use v5.010;
  2         9  
4 2     2   16 use strict;
  2         6  
  2         60  
5 2     2   15 use warnings;
  2         6  
  2         84  
6 2     2   16 use Method::Signatures 20140224;
  2         40  
  2         16  
7 2     2   1101 use List::MoreUtils qw(any);
  2         6  
  2         52  
8 2     2   1879 use Carp qw( croak );
  2         7  
  2         140  
9 2     2   16 use JSON qw( encode_json );
  2         5  
  2         17  
10 2     2   1262 use WebService::Freshservice::User::CustomField;
  2         9  
  2         84  
11 2     2   20 use Moo;
  2         7  
  2         12  
12 2     2   2434 use MooX::HandlesVia;
  2         22111  
  2         16  
13 2     2   393 use namespace::clean;
  2         7  
  2         21  
14              
15             # ABSTRACT: Freshservice User
16              
17             our $VERSION = '0.004'; # VERSION: Generated by DZP::OurPkg:Version
18              
19              
20             my $Ref = sub {
21             croak("api isn't a 'WebService::Freshservice::API' object!") unless $_[0]->DOES("WebService::Freshservice::API");
22             };
23              
24             # Library Fields
25             has 'api' => ( is => 'rw', required => 1, isa => $Ref );
26             has 'id' => ( is => 'ro', required => 1 );
27             has '_attributes' => ( is => 'rwp', lazy => 1, builder => 1 );
28             has '_attributes_rw' => ( is => 'rwp', lazy => 1, builder => 1 );
29             has '_raw' => ( is => 'rwp', lazy => 1, builder => 1, clearer => 1 );
30              
31             # Fixed fields
32             has 'active' => ( is => 'rwp', lazy => 1, builder => '_build_user', clearer => 1 );
33             has 'created_at' => ( is => 'rwp', lazy => 1, builder => '_build_user', clearer => 1 );
34             has 'deleted' => ( is => 'rwp', lazy => 1, builder => '_build_user', clearer => 1 );
35             has 'department_names' => ( is => 'rwp', lazy => 1, builder => '_build_user', clearer => 1 );
36             has 'helpdesk_agent' => ( is => 'rwp', lazy => 1, builder => '_build_user', clearer => 1 );
37             has 'updated_at' => ( is => 'rwp', lazy => 1, builder => '_build_user', clearer => 1 );
38              
39             # Updateable Fields
40             has 'address' => ( is => 'rw', lazy => 1, builder => '_build_user', clearer => 1 );
41             has 'description' => ( is => 'rw', lazy => 1, builder => '_build_user', clearer => 1 );
42             has 'email' => ( is => 'rw', lazy => 1, builder => '_build_user', clearer => 1 );
43             has 'external_id' => ( is => 'rw', lazy => 1, builder => '_build_user', clearer => 1 );
44             has 'language' => ( is => 'rw', lazy => 1, builder => '_build_user', clearer => 1 );
45             has 'location_name' => ( is => 'rw', lazy => 1, builder => '_build_user', clearer => 1 );
46             has 'job_title' => ( is => 'rw', lazy => 1, builder => '_build_user', clearer => 1 );
47             has 'mobile' => ( is => 'rw', lazy => 1, builder => '_build_user', clearer => 1 );
48             has 'name' => ( is => 'rw', lazy => 1, builder => '_build_user', clearer => 1 );
49             has 'phone' => ( is => 'rw', lazy => 1, builder => '_build_user', clearer => 1 );
50             has 'time_zone' => ( is => 'rw', lazy => 1, builder => '_build_user', clearer => 1 );
51              
52 2 0   2   3598 method _build__raw {
  0     0      
  0            
53 0           return $self->api->get_api( "itil/requesters/".$self->id.".json" );
54             }
55              
56 2 0   2   1940 method _build_user {
  0     0      
  0            
57             # Grab our calling method by dropping 'WebService::Freshservice::User::'
58 0           my $caller = substr((caller 1)[3],32);
59 0           return $self->_raw->{user}{$caller};
60             }
61              
62 2 0   2   1861 method _build__attributes {
  0     0      
  0            
63 0           my @attributes = qw(
64             active custom_field created_at deleted department_names
65             helpdesk_agent updated_at
66             );
67 0           push(@attributes, @{$self->_attributes_rw});
  0            
68 0           return \@attributes;
69             }
70              
71 2 0   2   1886 method _build__attributes_rw {
  0     0      
  0            
72 0           my @attributes = qw(
73             address description email external_id language
74             location_name job_title mobile name phone time_zone
75             );
76 0           return \@attributes;
77             }
78              
79 2 0   2   1835 method _clear_all {
  0     0      
  0            
80 0           foreach my $attr (@{$self->_attributes}) {
  0            
81 0           my $clearer = "clear_$attr";
82 0           $self->$clearer;
83             }
84 0           $self->_clear_raw;
85 0           return;
86             }
87              
88              
89 2 0   2   2001 method delete_requester {
  0     0      
  0            
90 0           return $self->api->delete_api( "itil/requesters/".$self->id.".json" );
91             }
92              
93              
94 2 0   2   12300 method update_requester(:$attr?, :$value?) {
  0 0   0      
  0            
  0            
  0            
  0            
  0            
  0            
  0            
95 0 0         if ( $attr ) {
96 0 0         croak "'value' required if providing an 'attr'" unless $value;
97 0           croak "'$attr' is not a valid attribute, valid attributes are".join(" ", @{$self->_attributes_rw}) unless
98 0 0   0     any { $_ eq $attr } @{$self->_attributes_rw};
  0            
  0            
99 0           my $update->{user}{$attr} = $value;
100 0           $self->api->put_api( "itil/requesters/".$self->id.".json", $update);
101 0           $self->_clear_all;
102 0           return 1;
103             }
104 0           $self->api->put_api( "itil/requesters/".$self->id.".json", $self);
105 0           $self->_clear_all;
106 0           return 1;
107             }
108              
109              
110             has 'custom_field' => (
111             is => 'rwp',
112             handles_via => 'Hash',
113             lazy => 1,
114             builder => 1,
115             clearer => 1,
116             handles => {
117             _get_cf => 'get',
118             _set_cf => 'set',
119             custom_fields => 'keys'
120             },
121             );
122              
123 2 0   2   2874 method _build_custom_field {
  0     0      
  0            
124 0           my $fields = { };
125 0           while (my ( $key, $value ) = each %{ $self->_raw->{user}{custom_field} } ) {
  0            
126 0           $fields->{$key} = WebService::Freshservice::User::CustomField->new(
127             id => $self->id,
128             api => $self->api,
129             field => $key,
130             value => $value,
131             );
132             }
133 0           return $fields;
134             }
135              
136              
137 2 0   2   20705 method set_custom_field(:$field, :$value, :$update = 1) {
  0 0   0      
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
138 0           my $custom_field = $self->_get_cf($field);
139 0           $custom_field->value($value);
140 0 0         $custom_field->update_custom_field if $update;
141 0           return;
142             }
143              
144              
145 2 0   2   6966 method get_custom_field($field) {
  0 0   0      
  0            
  0            
  0            
146             croak "Custom field must exist in Freshservice"
147 0 0         unless exists $self->_raw->{user}{custom_field}{$field};
148 0           return $self->_get_cf($field);
149             }
150              
151             # Internal method that returns a clean perl data structure
152             # for encode_json
153 2 0   2   2216 method TO_JSON {
  0     0      
  0            
154             my $data->{user} = {
155 0           address => $self->address,
156             description => $self->description,
157             custom_field => $self->custom_field,
158             email => $self->email,
159             external_id => $self->external_id,
160             language => $self->language,
161             location_name => $self->location_name,
162             job_title => $self->job_title,
163             mobile => $self->mobile,
164             name => $self->name,
165             phone => $self->phone,
166             time_zone => $self->time_zone,
167             };
168 0           return $data;
169             }
170              
171             1;
172              
173             __END__
174              
175             =pod
176              
177             =encoding UTF-8
178              
179             =head1 NAME
180              
181             WebService::Freshservice::User - Freshservice User
182              
183             =head1 VERSION
184              
185             version 0.004
186              
187             =head1 SYNOPSIS
188              
189             use WebService::Freshservice::User;
190              
191             my $request = WebService::Freshservice::User->new( api => $api, id => '1234567890' );
192              
193             Requires an 'WebService::Freshservice::API' object and user id.
194              
195             =head1 DESCRIPTION
196              
197             Provides a Freshservice user object. Though users are referred to
198             as 'Requesters' and 'Agents', agents are a super set of a user.
199              
200             =head1 METHODS
201              
202             =head2 delete_requester
203              
204             $requester->delete_requester;
205              
206             Returns 1 on success. Croaks on failure.
207              
208             =head2 update_requester
209              
210             $requester->update_requester;
211              
212             The following attributes can be updated and 'PUT' against
213             the API:
214              
215             address description email external_id language
216             location_name job_title mobile name phone time_zone
217              
218             Optionally takes named attributes of 'attr' and 'value' if only updating
219             a single attribute.
220              
221             $requester->update_requester( attr => 'address', value => 'Some new address' );
222              
223             API returns 200 OK and no content regardless if the put resulted in a
224             successful change. Returns 1, croaks on failure.
225              
226             =head2 custom_fields
227              
228             $requester->custom_fields;
229              
230             Will return a hash of WebService::Freshservice::User::CustomField objects. Returns
231             an empty object if your Freshservice instance doesn't have any custom fields
232             configured.
233              
234             =head2 set_custom_field
235              
236             $requester->set_custom_field(
237             field => 'cf_field_name',
238             value => 'field value',
239             );
240              
241             Set a custom field value. Takes an optional attribute of 'update'
242             which can be set to '0' and it will not 'PUT' the changes (handy
243             if you are trying to limit API calls and want are calling
244             '$requester->update_requester' later);
245              
246             =head2 get_custom_field
247              
248             say $updated->get_custom_field('cf_field_name')->value;
249              
250             or
251              
252             $custom_field = $updated->get_custom_field('cf_field_name');
253              
254             Returns a WebService::Freshservice::User::CustomField object of
255             the named Custom Field. Croaks if the field doesn't exist in
256             Freshservice.
257              
258             =head1 AUTHOR
259              
260             Leon Wright <techman@cpan.org>
261              
262             =head1 COPYRIGHT AND LICENSE
263              
264             This software is copyright (c) 2016 by Leon Wright.
265              
266             This is free software; you can redistribute it and/or modify it under
267             the same terms as the Perl 5 programming language system itself.
268              
269             =cut