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