File Coverage

blib/lib/Net/API/Stripe/Product/PackageDimension.pm
Criterion Covered Total %
statement 10 52 19.2
branch 0 18 0.0
condition n/a
subroutine 4 12 33.3
pod 6 6 100.0
total 20 88 22.7


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