File Coverage

blib/lib/Business/ID/SIM.pm
Criterion Covered Total %
statement 29 29 100.0
branch 7 12 58.3
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 42 47 89.3


line stmt bran cond sub pod time code
1             package Business::ID::SIM;
2              
3             our $DATE = '2015-09-03'; # DATE
4             our $VERSION = '0.07'; # VERSION
5              
6 1     1   630 use 5.010001;
  1         3  
7 1     1   4 use warnings;
  1         2  
  1         20  
8 1     1   4 use strict;
  1         2  
  1         16  
9              
10 1     1   1162 use DateTime;
  1         140807  
  1         404  
11              
12             require Exporter;
13             our @ISA = qw(Exporter);
14             our @EXPORT = qw(parse_sim);
15              
16             # legend: S = lack of samples
17              
18             # less organized than the convoluted code in NIK
19              
20             my %provinces = (
21             '06' => "Nanggroe Aceh Darussalam",
22             '07' => "Sumatera Utara",
23             '08' => "Sumatera Barat",
24             '09' => "Riau, Kepulauan Riau",
25             '10' => undef, # S
26             '11' => "Sumatera Selatan",
27             '12' => "DKI Jakarta", # most frequently encountered
28             '13' => "Jawa Barat",
29             '14' => "Jawa Tengah, DI Yogyakarta",
30             '15' => "Jawa Timur",
31             '16' => "Bali",
32             '17' => "Kalimantan Timur",
33             '18' => "Kalimantan Selatan",
34             '19' => "Sulawesi Selatan",
35             '20' => "Sulawesi Utara, Gorontalo",
36             '21' => undef, # S
37             '22' => 'Papua',
38             '23' => 'Kalimantan Tengah',
39             '24' => 'Sulawesi Tengah',
40             '25' => 'Lampung',
41             #'30' => "Banten", # ?, SS
42              
43             );
44              
45             our %SPEC;
46              
47             $SPEC{parse_sim} = {
48             v => 1.1,
49             summary => 'Validate Indonesian driving license number (nomor SIM)',
50             args => {
51             sim => {
52             summary => 'Input to be parsed',
53             schema => 'str*',
54             pos => 0,
55             req => 1,
56             },
57             },
58             };
59             sub parse_sim {
60 3     3 1 9317 my %args = @_;
61              
62 3 50       13 my $sim = $args{sim} or return [400, "Please specify sim"];
63 3         5 my $res = {};
64              
65 3         21 $sim =~ s/\D+//g;
66 3 50       9 return [400, "Not 12 digit"] unless length($sim) == 12;
67              
68 3         10 $res->{prov_code} = substr($sim, 4, 2);
69 3 100       20 return [400, "Unknown province code"] unless $provinces{$res->{prov_code}};
70 1         4 $res->{area_code} = substr($sim, 4, 4);
71              
72 1         5 my ($y, $m) = $sim =~ /^(..)(..)/;
73 1         9 my $today = DateTime->today;
74 1         1198 $y += int($today->year / 100) * 100;
75 1 50       11 $y -= 100 if $y > $today->year;
76 1         7 eval { $res->{dob} = DateTime->new(day=>1, month=>$m, year=>$y) };
  1         5  
77 1 50       212 return [400, "Invalid year and month of birth: $y, $m"] if $@;
78 1         6 $res->{serial} = $sim =~ /(....)$/;
79 1 50       5 return [400, "Serial starts from 1, not 0"] if $res->{serial} < 1;
80              
81 1         18 [200, "OK", $res];
82             }
83              
84             1;
85             # ABSTRACT: Validate Indonesian driving license number (nomor SIM)
86              
87             __END__