File Coverage

lib/Webservice/OVH/Order/Cart/Item.pm
Criterion Covered Total %
statement 9 93 9.6
branch 0 38 0.0
condition n/a
subroutine 3 20 15.0
pod 15 15 100.0
total 27 166 16.2


line stmt bran cond sub pod time code
1              
2             =encoding utf-8
3              
4             =head1 NAME
5              
6             Webservice::OVH::Order::Cart::Item
7              
8             =head1 SYNOPSIS
9              
10             use Webservice::OVH;
11              
12             my $ovh = Webservice::OVH->new_from_json("credentials.json");
13              
14             my $cart = $ovh->order->new_cart(ovh_subsidiary => 'DE');
15              
16             my $items = $cart->items;
17              
18             =head1 DESCRIPTION
19              
20             Provides info for a specific cart item.
21              
22             =head1 METHODS
23              
24             =cut
25              
26             use strict;
27 36     36   224 use warnings;
  36         76  
  36         889  
28 36     36   167 use Carp qw{ carp croak };
  36         63  
  36         802  
29 36     36   160  
  36         69  
  36         38736  
30             our $VERSION = 0.47;
31              
32             =head2 _new
33              
34             Internal Method to create the Item object.
35             This method is not ment to be called directly.
36              
37             =over
38              
39             =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object, $item_id - api id
40              
41             =item * Return: L<Webservice::OVH::Order::Cart>
42              
43             =item * Synopsis: Webservice::OVH::Order::Cart->_new($ovh_api_wrapper, $cart_id, $module);
44              
45             =back
46              
47             =cut
48              
49              
50             my ( $class, %params ) = @_;
51              
52 0     0     die "Missing module" unless $params{module};
53             die "Missing wrapper" unless $params{wrapper};
54 0 0         die "Missing id" unless $params{id};
55 0 0         die "Missing cart" unless $params{cart};
56 0 0          
57 0 0         my $module = $params{module};
58             my $api_wrapper = $params{wrapper};
59 0           my $item_id = $params{id};
60 0           my $cart = $params{cart};
61 0            
62 0           my $cart_id = $cart->id;
63             my $response = $api_wrapper->rawCall( method => 'get', path => "/order/cart/$cart_id/item/$item_id", noSignature => 0 );
64 0           croak $response->error if $response->error;
65 0            
66 0 0         my $porperties = $response->content;
67              
68 0           my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _id => $item_id, _properties => $porperties, _cart => $cart }, $class;
69              
70 0           return $self;
71             }
72 0            
73             =head2 is_valid
74              
75             When the item is deleted on the api side, this method returns 0.
76              
77             =over
78              
79             =item * Return: VALUE
80              
81             =item * Synopsis: print "Valid" if $item->is_valid;
82              
83             =back
84              
85             =cut
86              
87              
88             my ($self) = @_;
89              
90             return $self->{_valid};
91 0     0 1   }
92              
93 0           =head2 _is_valid
94              
95             Intern method to check validity.
96             Difference is that this method carps an error.
97              
98             =over
99              
100             =item * Return: VALUE
101              
102             =item * Synopsis: $item->_is_valid;
103              
104             =back
105              
106             =cut
107              
108              
109             my ($self) = @_;
110              
111             my $item_id = $self->id;
112             carp "Item $item_id is not valid anymore" unless $self->is_valid;
113 0     0     return $self->is_valid;
114             }
115 0            
116 0 0         =head2 _is_valid
117 0            
118             Gets the associated cart.
119              
120             =over
121              
122             =item * Return: L<Webservice::OVH::Order::Cart>
123              
124             =item * Synopsis: my $cart = $item->cart;
125              
126             =back
127              
128             =cut
129              
130              
131             my ($self) = @_;
132              
133             return $self->{_cart};
134             }
135              
136 0     0 1   =head2 id
137              
138 0           Returns the api id.
139              
140             =over
141              
142             =item * Return: VALUE
143              
144             =item * Synopsis: my $id = $item->id;
145              
146             =back
147              
148             =cut
149              
150              
151             my ($self) = @_;
152              
153             return $self->{_id};
154             }
155              
156             =head2 properties
157 0     0 1    
158             Retrieves properties.
159 0           This method updates the intern property variable.
160              
161             =over
162              
163             =item * Return: HASH
164              
165             =item * Synopsis: my $properties = $item->properties;
166              
167             =back
168              
169             =cut
170              
171              
172             my ($self) = @_;
173              
174             return unless $self->_is_valid;
175              
176             my $api = $self->{_api_wrapper};
177             my $cart_id = $self->{_cart}->id;
178             my $item_id = $self->id;
179 0     0 1   my $response = $api->rawCall( method => 'get', path => "/order/cart/$cart_id/item/$item_id", noSignature => 0 );
180             croak $response->error if $response->error;
181 0 0          
182             $self->{_properties} = $response->content;
183 0           return $self->{_properties};
184 0           }
185 0            
186 0           =head2 configuration_add
187 0 0          
188             Setup configuration item for the product
189 0            
190 0           =over
191              
192             =item * Return: L<HASHREF>
193              
194             =item * Synopsis: my $configurations = $item->configuration_add;
195              
196             =back
197              
198             =cut
199              
200              
201             my ($self, $label, $value) = @_;
202              
203             return unless $self->_is_valid;
204              
205             my $api = $self->{_api_wrapper};
206             my $cart_id = $self->{_cart}->id;
207             my $item_id = $self->id;
208              
209 0     0 1   croak "Missing label" unless $label;
210             croak "Missing value" unless $value;
211 0 0          
212             my $body = {label => $label, value => $value};
213 0           my $response = $api->rawCall( method => 'post', path => "/order/cart/$cart_id/item/$item_id/configuration", body => $body, noSignature => 0 );
214 0           croak $response->error if $response->error;
215 0            
216             return $response->content;
217 0 0         }
218 0 0          
219             =head2 configuration_delete
220 0            
221 0           Delete configuration item
222 0 0          
223             =over
224 0            
225             =item * Return: L<HASHREF>
226              
227             =item * Synopsis: my $configurations = $item->configuration_delete;
228              
229             =back
230              
231             =cut
232              
233              
234             my ($self, $configuration_id) = @_;
235              
236             return unless $self->_is_valid;
237              
238             my $api = $self->{_api_wrapper};
239             my $cart_id = $self->{_cart}->id;
240             my $item_id = $self->id;
241              
242             croak "Missing configuration_id" unless $configuration_id;
243 0     0 1    
244             my $response = $api->rawCall( method => 'delete', path => "/order/cart/$cart_id/item/$item_id/configuration/$configuration_id", body => {}, noSignature => 0 );
245 0 0         croak $response->error if $response->error;
246              
247 0           return $response->content;
248 0           }
249 0            
250             =head2 configurations
251 0 0          
252             Exposed property value.
253 0            
254 0 0         =over
255              
256 0           =item * Return: L<ARRAY>
257              
258             =item * Synopsis: my $configurations = $item->configurations;
259              
260             =back
261              
262             =cut
263              
264              
265             my ($self) = @_;
266              
267             return $self->{_properties}->{configurations};
268             }
269              
270             =head2 duration
271              
272             Exposed property value.
273              
274             =over
275 0     0 1    
276             =item * Return: VALUE
277 0            
278             =item * Synopsis: my $duration = $item->duration;
279              
280             =back
281              
282             =cut
283              
284              
285             my ($self) = @_;
286              
287             return $self->{_properties}->{duration};
288             }
289              
290             =head2 offer_id
291              
292             Exposed property value.
293              
294             =over
295              
296 0     0 1   =item * Return: VALUE
297              
298 0           =item * Synopsis: my $offer_id = $item->offer_id;
299              
300             =back
301              
302             =cut
303              
304              
305             my ($self) = @_;
306              
307             return $self->{_properties}->{offerId};
308             }
309              
310             =head2 options
311              
312             Exposed property value.
313              
314             =over
315              
316             =item * Return: HASH
317 0     0 1    
318             =item * Synopsis: my $options = $item->options;
319 0            
320             =back
321              
322             =cut
323              
324              
325             my ($self) = @_;
326              
327             return $self->{_properties}->{options};
328             }
329              
330             =head2 prices
331              
332             Exposed property value.
333              
334             =over
335              
336             =item * Return: HASH
337              
338 0     0 1   =item * Synopsis: my $prices = $item->prices;
339              
340 0           =back
341              
342             =cut
343              
344              
345             my ($self) = @_;
346              
347             return $self->{_properties}->{prices};
348             }
349              
350             =head2 product_id
351              
352             Exposed property value.
353              
354             =over
355              
356             =item * Return: VALUE
357              
358             =item * Synopsis: my $product_id = $item->product_id;
359 0     0 1    
360             =back
361 0            
362             =cut
363              
364              
365             my ($self) = @_;
366              
367             return $self->{_properties}->{productId};
368             }
369              
370             =head2 settings
371              
372             Exposed property value.
373              
374             =over
375              
376             =item * Return: HASH
377              
378             =item * Synopsis: my $settings = $item->settings;
379              
380 0     0 1   =back
381              
382 0           =cut
383              
384              
385             my ($self) = @_;
386              
387             return $self->{_properties}->{settings};
388             }
389              
390             =head2 available_configuration
391              
392             Exposed property value.
393              
394             =over
395              
396             =item * Return: L<ARRAY>
397              
398             =item * Synopsis: my $available_configuration = $item->available_configuration;
399              
400             =back
401 0     0 1    
402             =cut
403 0            
404              
405             my ($self) = @_;
406              
407             return unless $self->_is_valid;
408              
409             my $api = $self->{_api_wrapper};
410             my $cart_id = $self->{_cart}->id;
411             my $item_id = $self->id;
412              
413             my $response = $api->rawCall( method => 'get', path => "/order/cart/$cart_id/item/$item_id/configuration", noSignature => 0 );
414             croak $response->error if $response->error;
415              
416             return $response->content;
417             }
418              
419             =head2 delete
420              
421             Deletes the item and sets the object to invalid.
422 0     0 1    
423             =over
424 0 0          
425             =item * Synopsis: $item->delete;
426 0            
427 0           =back
428 0            
429             =cut
430 0            
431 0 0          
432             my ($self) = @_;
433 0            
434             return unless $self->_is_valid;
435              
436             my $api = $self->{_api_wrapper};
437             my $cart_id = $self->{_cart}->id;
438             my $item_id = $self->id;
439             my $response = $api->rawCall( method => 'delete', path => "/order/cart/$cart_id/item/$item_id", noSignature => 0 );
440             croak $response->error if $response->error;
441              
442             $self->{_valid} = 0;
443             }
444              
445             1;