File Coverage

lib/Net/API/Stripe/Product/PackageDimension.pm
Criterion Covered Total %
statement 23 63 36.5
branch 0 18 0.0
condition n/a
subroutine 8 15 53.3
pod 6 6 100.0
total 37 102 36.2


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## Stripe API - ~/lib/Net/API/Stripe/Product/PackageDimension.pm
3             ## Version v0.100.1
4             ## Copyright(c) 2020 DEGUEST Pte. Ltd.
5             ## Author: Jacques Deguest <jack@deguest.jp>
6             ## Created 2019/11/02
7             ## Modified 2020/05/16
8             ##
9             ##----------------------------------------------------------------------------
10             BEGIN
11             {
12             use strict;
13 2     2   871 use warnings;
  2         4  
  2         66  
14 2     2   12 use parent qw( Net::API::Stripe::Generic );
  2         5  
  2         61  
15 2     2   10 use vars qw( $VERSION );
  2         5  
  2         12  
16 2     2   141 our( $VERSION ) = 'v0.100.1';
  2         6  
  2         128  
17 2     2   39 };
18              
19             use strict;
20 2     2   10 use warnings;
  2         3  
  2         55  
21 2     2   11  
  2         3  
  2         1278  
22             {
23             my $self = shift( @_ );
24             # Default to using the imperial measurement, ie default measurement system in the U.S.
25 1     1 1 2925446 # User needs to activate the metric system
26             $self->{use_metric} = 0;
27             $self->SUPER::init( @_ );
28 1         87 return( $self );
29 1         40 }
30 1         7  
31              
32              
33 0     0 1   {
34             my $self = shift( @_ );
35 0     0 1   if( @_ )
36             {
37             # Check provided and convert data on the fly
38             my $val = shift( @_ );
39 0     0 1   $self->{use_metric} = $val;
40 0 0         # Convert all data to metric, so it can be converted back after into inch and ounces when used for Stripe api calls
41             foreach my $k ( qw( height length width ) )
42             {
43 0           next if( !CORE::length( $self->{ $k } ) );
44 0           my $v = $self->_set_get_convert_size( $k, $self->{ $k } );
45             $self->{ $k } = $v;
46 0           }
47             if( CORE::length( $self->{weight} ) )
48 0 0         {
49 0           my $v = $self->_set_get_convert_weight( 'weight', $self->{weight} );
50 0           $self->{weight} = $v;
51             }
52 0 0         }
53             return( $self->{use_metric} );
54 0           }
55 0            
56              
57              
58 0           {
59             my $self = shift( @_ );
60             my $field = shift( @_ );
61 0     0 1   if( @_ )
62             {
63 0     0 1   my $num = shift( @_ );
64             return( $self->_set_get_number( $field, $num ) ) if( !$self->{use_metric} );
65             # Helper method from Net::API::Stripe::Generic
66             # If metric option is on, convert the metric value into inch to be compliant with Stripe
67 0     0     my $new = $self->_convert_measure({ from => 'cm', value => "$num" });
68 0           return( $self->_set_get_number( $field, $new ) );
69 0 0         }
70             # No argument, just retrieving the value
71 0           else
72 0 0         {
73             my $val = $self->{ $field };
74             return( $val ) if( !$self->{use_metric} );
75 0           my $new = $self->_convert_measure({ from => 'inch', value => "$val" });
76 0           require Module::Generic::Number;
77             return( Module::Generic::Number->new( $new ) );
78             }
79             }
80              
81 0           {
82 0 0         my $self = shift( @_ );
83 0           my $field = shift( @_ );
84 0           if( @_ )
85 0           {
86             my $num = shift( @_ );
87             return( $self->_set_get_number( $field, $num ) ) if( !$self->{use_metric} );
88             # Helper method from Net::API::Stripe::Generic
89             # If metric option is on, convert the metric value into inch to be compliant with Stripe
90             my $new = $self->_convert_measure({ from => 'gram', value => "$num" });
91 0     0     return( $self->_set_get_number( $field, $new ) );
92 0           }
93 0 0         # No argument, just retrieving the value
94             else
95 0           {
96 0 0         my $val = $self->{ $field };
97             return( $val ) if( !$self->{use_metric} );
98             my $new = $self->_convert_measure({ from => 'gram', value => "$val" });
99 0           require Module::Generic::Number;
100 0           return( Module::Generic::Number->new( $new ) );
101             }
102             }
103              
104             1;
105 0            
106 0 0          
107 0           =encoding utf8
108 0            
109 0           =head1 NAME
110              
111             Net::API::Stripe::Product::PackageDimension - A Stripe Product Package Dimension Object
112              
113             =head1 SYNOPSIS
114              
115             # In inches
116             my $pkg = $stripe->product->package_dimensions({
117             height => 6,
118             length => 20,
119             # Ounce
120             weight => 21
121             width => 12
122             });
123              
124             # Then, because we are in EU
125             $pkg->use_metric( 1 );
126             my $width = $pkg->width;
127             # returns in centimetres: 30.48
128              
129             =head1 VERSION
130              
131             v0.100.1
132              
133             =head1 DESCRIPTION
134              
135             The dimensions of this SKU for shipping purposes.
136              
137             This is instantiated by method B<package_dimensions> in module L<Net::API::Stripe::Product>
138              
139             =head1 CONSTRUCTOR
140              
141             =head2 new( %ARG )
142              
143             Creates a new L<Net::API::Stripe::Order::SKU::PackageDimensions> object.
144             It may also take an hash like arguments, that also are method of the same name.
145              
146             =head1 METHODS
147              
148             =head2 height decimal
149              
150             Height, in inches.
151              
152             =head2 length decimal
153              
154             Length, in inches.
155              
156             =head2 use_metric Boolean
157              
158             By providing a boolean value, you can change the value returned to you.
159              
160             Stripe uses and requires, unfortunately, the use of C<inch> and C<ounce> although the vast majority of the word uses the metric system. So this feature makes it possible to get the proper value while still sending Stripe the values in the proper measurement system.
161              
162             If on, this will convert all values from inch to metric, or ounce to gram or vice versa.
163              
164             Internally the values will always be in C<inch> and C<ounce>.
165              
166             So, after having retrieved a L<Net::API::Stripe::Order::SKU> object from Stripe you could do something like this:
167              
168             my $sku = $stripe->skus( retrieve => $id ) || die( $stripe->error );
169             $sku->package_dimensions->use_metric( 1 );
170             # Width in centimetres
171             my $width = $skup->package_dimensions->width;
172              
173             =head2 weight decimal
174              
175             Weight, in ounces.
176              
177             =head2 width decimal
178              
179             Width, in inches.
180              
181             =head1 API SAMPLE
182              
183             {
184             "id": "prod_fake123456789",
185             "object": "product",
186             "active": true,
187             "attributes": [],
188             "caption": null,
189             "created": 1541833574,
190             "deactivate_on": [],
191             "description": null,
192             "images": [],
193             "livemode": false,
194             "metadata": {},
195             "name": "Provider, Inc investor yearly membership",
196             "package_dimensions": null,
197             "shippable": null,
198             "statement_descriptor": null,
199             "type": "service",
200             "unit_label": null,
201             "updated": 1565089803,
202             "url": null
203             }
204              
205             =head1 HISTORY
206              
207             =head2 v0.1
208              
209             Initial version
210              
211             =head1 AUTHOR
212              
213             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
214              
215             =head1 SEE ALSO
216              
217             Stripe API documentation:
218              
219             L<https://stripe.com/docs/api/products/object>
220              
221             =head1 COPYRIGHT & LICENSE
222              
223             Copyright (c) 2019-2020 DEGUEST Pte. Ltd.
224              
225             You can use, copy, modify and redistribute this package and associated
226             files under the same terms as Perl itself.
227              
228             =cut