File Coverage

blib/lib/Net/Duo/Admin/Phone.pm
Criterion Covered Total %
statement 37 37 100.0
branch 5 6 83.3
condition 2 3 66.6
subroutine 12 12 100.0
pod 6 6 100.0
total 62 64 96.8


line stmt bran cond sub pod time code
1             # Representation of a single Duo phone for the Admin API.
2             #
3             # This class wraps the Duo representation of a single Duo phone, as returned
4             # by (for example) the Admin /phones REST endpoint.
5              
6             package Net::Duo::Admin::Phone 1.01;
7              
8 6     6   3501 use 5.014;
  6         23  
9 6     6   32 use strict;
  6         11  
  6         131  
10 6     6   39 use warnings;
  6         13  
  6         187  
11              
12 6     6   30 use parent qw(Net::Duo::Object);
  6         15  
  6         43  
13              
14 6     6   3610 use Net::Duo::Admin::User;
  6         17  
  6         2966  
15              
16             # Data specification for converting JSON into our object representation. See
17             # the Net::Duo::Object documentation for syntax information.
18             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
19             sub _fields {
20             return {
21 18     18   244 activated => 'simple',
22             capabilities => 'array',
23             extension => ['simple', 'set'],
24             name => ['simple', 'set'],
25             number => ['simple', 'set'],
26             phone_id => 'simple',
27             platform => ['simple', 'set'],
28             postdelay => ['simple', 'set'],
29             predelay => ['simple', 'set'],
30             sms_passcodes_sent => 'simple',
31             type => ['simple', 'set'],
32             users => 'Net::Duo::Admin::User',
33             };
34             }
35             ## use critic
36              
37             # Install our accessors.
38             Net::Duo::Admin::Phone->install_accessors;
39              
40             # Override the new method to support creating a phone from an ID instead
41             # of decoded JSON data.
42             #
43             # $class - Class of object to create
44             # $duo - Net::Duo object to use to create the object
45             # $id_or_data - Phone ID or reference to data
46             #
47             # Returns: Newly-created object
48             # Throws: Net::Duo::Exception on any problem creating the object
49             sub new {
50 4     4 1 91 my ($class, $duo, $id_or_data) = @_;
51 4 100       13 if (!ref($id_or_data)) {
52 1         6 $id_or_data = $duo->call_json('GET', "/admin/v1/phones/$id_or_data");
53             }
54 4         28 return $class->SUPER::new($duo, $id_or_data);
55             }
56              
57             # Override the create method to add the appropriate URI.
58             #
59             # $class - Class of object to create
60             # $duo - Net::Duo object to use to create the object
61             # $data_ref - Data for new object as a reference to a hash
62             #
63             # Returns: Newly-created object
64             # Throws: Net::Duo::Exception on any problem creating the object
65             sub create {
66 1     1 1 91 my ($class, $duo, $data_ref) = @_;
67 1         11 return $class->SUPER::create($duo, '/admin/v1/phones', $data_ref);
68             }
69              
70             # Request an activation URL and barcode for a phone.
71             #
72             # $self - The Net::Duo::Admin::Phone object to request activation URLs for
73             # $args_ref - Arguments for the request (optional)
74             # install - Set to a true value to request an install URL as well
75             # valid_secs - Time the URL will be valid in seconds
76             #
77             # Returns: A reference to a hash with the following possible keys
78             # activation_url - Activation URL for this phone
79             # activation_barcode - Activation URL for QR code for this phone
80             # installation_url - Install URL if one was requested
81             # valid_secs - How long URLs will be valid
82             # Throws: Net::Duo::Exception on any problem getting activation URLs
83             sub activation_url {
84 2     2 1 147 my ($self, $args_ref) = @_;
85 2         3 my %args = %{$args_ref};
  2         10  
86              
87             # Canonicalize the install argument.
88 2 100 66     16 if (defined($args_ref) && defined($args_ref->{install})) {
89 1 50       4 $args{install} = $args{install} ? 1 : 0;
90             }
91              
92             # Make the JSON call and return the results.
93 2         8 my $uri = "/admin/v1/phones/$self->{phone_id}/activation_url";
94 2         10 return $self->{_duo}->call_json('POST', $uri, \%args);
95             }
96              
97             # Commit any changed data and refresh the object from Duo.
98             #
99             # $self - The Net::Duo::Admin::Phone object to commit changes for
100             #
101             # Returns: undef
102             # Throws: Net::Duo::Exception on any problem updating the object
103             sub commit {
104 2     2 1 351 my ($self) = @_;
105 2         19 return $self->SUPER::commit("/admin/v1/phones/$self->{phone_id}");
106             }
107              
108             # Delete the phone from Duo. After this call, the object should be treated as
109             # read-only since it can no longer be usefully updated.
110             #
111             # $self - The Net::Duo::Admin::Phone object to delete
112             #
113             # Returns: undef
114             # Throws: Net::Duo::Exception on any problem deleting the object
115             ## no critic (Subroutines::ProhibitBuiltinHomonyms)
116             sub delete {
117 1     1 1 54 my ($self) = @_;
118 1         7 $self->{_duo}->call_json('DELETE', "/admin/v1/phones/$self->{phone_id}");
119 1         3 return;
120             }
121             ## use critic
122              
123             # Send a new batch of SMS passcodes to a phone.
124             #
125             # $self - The Net::Duo::Admin::Phone object to which to send SMS passcodes
126             #
127             # Returns: undef
128             # Throws: Net::Duo::Exception on any problem sending passcodes
129             sub send_sms_passcodes {
130 1     1 1 71 my ($self) = @_;
131 1         5 my $uri = "/admin/v1/phones/$self->{phone_id}/send_sms_passcodes";
132 1         5 $self->{_duo}->call_json('POST', $uri);
133 1         3 return;
134             }
135              
136             1;
137             __END__