File Coverage

lib/Data/UUID/Base64URLSafe.pm
Criterion Covered Total %
statement 29 29 100.0
branch 1 2 50.0
condition n/a
subroutine 10 10 100.0
pod 5 5 100.0
total 45 46 97.8


line stmt bran cond sub pod time code
1             package Data::UUID::Base64URLSafe;
2 1     1   459 use 5.008001;
  1         3  
3 1     1   4 use strict;
  1         1  
  1         17  
4 1     1   3 use warnings;
  1         1  
  1         21  
5 1     1   229 use MIME::Base64;
  1         435  
  1         58  
6              
7             our $VERSION = "0.35";
8              
9 1     1   5 use base qw( Data::UUID );
  1         1  
  1         231  
10             our @EXPORT = @Data::UUID::EXPORT;
11              
12             =encoding utf-8
13              
14             =head1 NAME
15              
16             Data::UUID::Base64URLSafe - getting Data::UUID with URLSafe strings
17              
18             =head1 SYNOPSIS
19              
20             use Data::UUID::Base64URLSafe;
21             my $ug = Data::UUID::Base64URLSafe->new();
22             my $uuid = $ug->create_b64_urlsafe(); # make an unique UUID
23             $uuid = $ug->create_from_name_b64_urlsafe( , ); # from namespace and string
24             my $str = $ug->from_b64_urlsafe(< Base64-URLSafe || Base64 >); # decoding from Base64
25             my $bin = $ug->create_from_name( , );
26             my $uuid2 = $ug->to_b64_urlsafe($bin); # encoding from binary
27              
28             =head1 DESCRIPTION
29              
30             Data::UUID::Base64URLSafe is a wrapper module for Data::UUID.
31              
32             L creates wonderful Globally/Universally Unique
33             Identifiers (GUIDs/UUIDs). This module is a subclass of that
34             module which adds a method to get a URL-safe Base64-encoded
35             version of the UUID using L.
36             What that means is that you can get a 22-character UUID string which
37             you can use safely in URLs.
38              
39             It will help you when you wanna make user-ID on your web applications.
40              
41             =head1 METHODS
42              
43             =head2 new
44              
45             =cut
46              
47             sub new {
48 1     1 1 754 my $class = shift;
49 1         387 return bless $class->SUPER::new(), $class;
50             };
51              
52             =head2 create_b64_urlsafe
53              
54             Create a URL-safe Base64-encoded UUID:
55              
56             my $uuid = $ug->create_b64_urlsafe();
57              
58             =cut
59              
60             sub create_b64_urlsafe {
61 2     2 1 901 my $self = shift;
62 2         93 my $uuid = $self->create();
63 2         7 return MIME::Base64::encode_base64url($uuid);
64             }
65              
66             =head2 create_from_name_b64_urlsafe
67              
68             Creates a URL-safe Base64 encoded UUID with the namespace and data
69             specified (See the L docs on create_from_name
70              
71             =cut
72              
73             sub create_from_name_b64_urlsafe {
74 1     1 1 866 my $self = shift;
75 1         19 my $uuid = $self->create_from_name(@_);
76 1         4 return MIME::Base64::encode_base64url($uuid);
77             }
78              
79             =head2 from_b64_urlsafe
80              
81             Convert a (URL-safe or not) Base64-encoded UUID to its canonical binary representation
82              
83             my $uuid = $ug−>create_from_name_b64_urlsafe(, );
84             my $bin = $ug->from_b64_urlsafe($uuid);
85              
86             =cut
87              
88             sub from_b64_urlsafe {
89 1     1 1 544 my $self = shift;
90 1         2 my $b64 = shift;
91 1 50       5 $b64 =~ tr[-_][+/] if $b64 =~ m|[-_]|;
92 1         4 return MIME::Base64::decode_base64($b64);
93             }
94              
95             =head2 to_b64_urlsafe
96              
97             Convert a binary UUID to a URL-safe Base64 encoded UUID
98              
99             my $bin = $ug->create_from_name(, );
100             my $uuid = $ug−>to_b64_urlsafe($bin);
101              
102             =cut
103              
104             sub to_b64_urlsafe {
105 1     1 1 427 my $self = shift;
106 1         2 my $bin = shift;
107 1         4 return MIME::Base64::encode_base64url($bin);
108             }
109              
110             1;
111             __END__