File Coverage

lib/Webservice/OVH/Email/Domain.pm
Criterion Covered Total %
statement 12 54 22.2
branch 0 20 0.0
condition 0 6 0.0
subroutine 4 8 50.0
pod 3 3 100.0
total 19 91 20.8


line stmt bran cond sub pod time code
1             package Webservice::OVH::Email::Domain;
2              
3             =encoding utf-8
4              
5             =head1 NAME
6              
7             Webservice::OVH::Email::Domain
8              
9             =head1 SYNOPSIS
10              
11             use Webservice::OVH;
12            
13             my $ovh = Webservice::OVH->new_from_json("credentials.json");
14            
15             my $email_domains = $ovh->email->domain->domains;
16            
17             foreach $email_domain (@$email_domains) {
18            
19             $email_domain->name;
20             }
21              
22             =head1 DESCRIPTION
23              
24             Provides access to email domain objects.
25              
26             =head1 METHODS
27              
28             =cut
29              
30 36     36   272 use strict;
  36         69  
  36         1020  
31 36     36   181 use warnings;
  36         96  
  36         969  
32 36     36   204 use Carp qw{ carp croak };
  36         127  
  36         2757  
33              
34             our $VERSION = 0.48;
35              
36 36     36   17789 use Webservice::OVH::Email::Domain::Domain;
  36         107  
  36         21168  
37              
38             =head2 _new
39              
40             Internal Method to create the domain object.
41             This method is not ment to be called external.
42              
43             =over
44              
45             =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object
46              
47             =item * Return: L<Webservice::OVH::Email::Domain>
48              
49             =item * Synopsis: Webservice::OVH::Email::Domain->_new($ovh_api_wrapper, $zone_name, $module);
50              
51             =back
52              
53             =cut
54              
55             sub _new {
56              
57 0     0     my ( $class, %params ) = @_;
58              
59 0 0         die "Missing module" unless $params{module};
60 0 0         die "Missing wrapper" unless $params{wrapper};
61              
62 0           my $module = $params{module};
63 0           my $api_wrapper = $params{wrapper};
64              
65 0           my $self = bless { _module => $module, _api_wrapper => $api_wrapper, _domains => {}, _aviable_domains => [] }, $class;
66              
67 0           my $api = $self->{_api_wrapper};
68 0           my $response = $api->rawCall( method => 'get', path => "/email/domain/", noSignature => 0 );
69 0 0         croak $response->error if $response->error;
70              
71 0           $self->{_aviable_domains} = $response->content;
72              
73 0           return $self;
74             }
75              
76             =head2 domain_exists
77              
78             Returns 1 if email-domain is available for the connected account, 0 if not.
79              
80             =over
81              
82             =item * Parameter: $domain - (required)Domain name, $no_recheck - (optional)only for internal usage
83              
84             =item * Return: VALUE
85              
86             =item * Synopsis: print "mydomain.com exists" if $ovh->email->domain->domain_exists("mydomain.com");
87              
88             =back
89              
90             =cut
91              
92             sub domain_exists {
93              
94 0     0 1   my ( $self, $domain, $no_recheck ) = @_;
95              
96 0 0         if ( !$no_recheck ) {
97              
98 0           my $api = $self->{_api_wrapper};
99 0           my $response = $api->rawCall( method => 'get', path => "/email/domain/", noSignature => 0 );
100 0 0         croak $response->error if $response->error;
101              
102 0           $self->{_aviable_domains} = $response->content;
103              
104 0           my $list = $response->content;
105              
106 0 0         return ( grep { $_ eq $domain } @$list ) ? 1 : 0;
  0            
107              
108             } else {
109              
110 0           my $list = $self->{_aviable_domains};
111              
112 0 0         return ( grep { $_ eq $domain } @$list ) ? 1 : 0;
  0            
113             }
114             }
115              
116             =head2 domains
117              
118             Produces an array of all available email-domains that are connected to the used account.
119              
120             =over
121              
122             =item * Return: L<ARRAY>
123              
124             =item * Synopsis: my $domains = $ovh->email->domain->domains();
125              
126             =back
127              
128             =cut
129              
130             sub domains {
131              
132 0     0 1   my ($self) = @_;
133              
134 0           my $api = $self->{_api_wrapper};
135 0           my $response = $api->rawCall( method => 'get', path => "/email/domain/", noSignature => 0 );
136 0 0         croak $response->error if $response->error;
137              
138 0           my $domain_array = $response->content;
139 0           my $domains = [];
140 0           $self->{_aviable_domains} = $domain_array;
141              
142 0           foreach my $domain (@$domain_array) {
143 0 0         if ( $self->domain_exists( $domain, 1 ) ) {
144 0   0       my $domain = $self->{_domains}{$domain} = $self->{_domains}{$domain} || Webservice::OVH::Email::Domain::Domain->_new( wrapper => $api, id => $domain, module => $self->{_module} );
145 0           push @$domains, $domain;
146             }
147             }
148              
149 0           return $domains;
150              
151             }
152              
153             =head2 domain
154              
155             Returns a single email-domains by name
156              
157             =over
158              
159             =item * Parameter: $domain - domain name
160              
161             =item * Return: L<Webservice::OVH::Email::Domains::Domain>
162              
163             =item * Synopsis: my $email_domain = $ovh->email->domain->domain("mydomain.com");
164              
165             =back
166              
167             =cut
168              
169             sub domain {
170              
171 0     0 1   my ( $self, $domain ) = @_;
172              
173 0 0         if ( $self->domain_exists($domain) ) {
174              
175 0           my $api = $self->{_api_wrapper};
176 0   0       my $domain = $self->{_domains}{$domain} = $self->{_domains}{$domain} || Webservice::OVH::Email::Domain::Domain->_new( wrapper => $api, id => $domain, module => $self->{_module} );
177              
178 0           return $domain;
179             } else {
180              
181 0           carp "Domain $domain doesn't exists";
182 0           return undef;
183             }
184             }
185              
186             1;