OpenToken/Serializer.pm

filter: Statementstmt 100% 38 / 38 Branchbran 92.8% 13 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 2 / 2 totaltotal 98.2% 57 / 58 CC 10 SCAR 15.8
CC
10
Cov
98%
freeze
6.0
thaw
5.0
BEGIN
1.0
CRAP 4.8
« OpenToken.pm ↑ Summary OpenToken/Token.pm »
1 package Crypt::OpenToken::Serializer;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $WS = qr/[\t ]/; # WS, as per OpenToken spec (just tab and space)
7 7 our $CRLF = qr/[\r\n]/; # CRLF, as per OpenToken spec
8  
9 16 ▶sub thaw {
Subroutine thaw: called CC=5 SCAR=16.1
Pod documented
10 16 my $str = shift;
11 16 my %data;
12  
13 16 ▶ while ($str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str) 45 16 61
14 45 my ($key, $val);
15 45 my ($quote, $remainder);
16  
17 45 ($key, $remainder) = ($str =~ /^$WS*(\S+)$WS*=$WS*(.*)$/s);
18  
19 45 ▶ if ($remainder =~ /^['"]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder =~ /^['"]/) { } 15 30 45
20 15 ($quote, $val, $remainder)
21 = ($remainder =~ /^(['"])(.*?)(?<!\\)\1$WS*?$CRLF+(.*)/s);
22 15 $val =~ s/\\(['"])/$1/g;
23 }
24 else {
25 30 ($val, $remainder) = split /$CRLF+/, $remainder, 2;
26 }
27 45 $str = $remainder;
28  
29 45 ▶ if (exists $data{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data{$key}) { } 2 43 45
30 $data{$key} = [
31 2 ▶ (ref($data{$key}) ? @{ $data{$key} } : $data{$key}),
Branch: 1 branch
BranchTrueFalseTotal
ref $data{$key} ? : 1 1 2
32 $val,
33 ];
34 }
35 else {
36 43 $data{$key} = $val;
37 }
38 }
39 16 return %data;
40 }
41  
42 11 ▶sub freeze {
Subroutine freeze: called CC=6 SCAR=17.9
Pod documented
43 11 my (%data) = @_;
44 11 my $str;
45  
46 11 foreach my $key (sort keys %data) {
47 27 my $val = $data{$key};
48 27 ▶ my @vals = ref($val) eq 'ARRAY' ? @{$val} : ($val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 1 26 27
49 27 foreach my $v (@vals) {
50 29 ▶ $v = '' unless (defined $v);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 29 29
51 29 ▶ if ($v =~ /\W/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /\W/) 15 14 29
52 15 $v =~ s/(['"])/\\$1/g;
53 15 $v = "'" . $v . "'";
54 }
55 29 $str .= "$key = $v\n";
56 }
57 }
58  
59 11 return $str;
60 }
61  
62 7 1;
63  
64 =for stopwords OpenTokens
65  
66 =head1 NAME
67  
68 Crypt::OpenToken::Serializer - Serialize payloads for OpenTokens
69  
70 =head1 SYNOPSIS
71  
72 use Crypt::OpenToken::Serializer;
73  
74 # data to serialize
75 my $data = {
76 foo => 'bar',
77 bar => 'baz',
78 };
79  
80 # freeze/serialize the data
81 my $payload = Crypt::OpenToken::Serializer::freeze(%{$data});
82  
83 # thaw/deserialize the data
84 my %thawed = Crypt::OpenToken::Serializer::thaw($payload);
85  
86 =head1 DESCRIPTION
87  
88 This module implements the serialization routine described in the OpenToken
89 specification for generating the payload format.
90  
91 Highlights:
92  
93 =over
94  
95 =item *
96  
97 A line-based format in the form of "key = value".
98  
99 =item *
100  
101 Within quoted-strings, B<both> double and single quotes must be escaped by a
102 preceding backslash.
103  
104 =item *
105  
106 Encoded with UTF-8 and is guaranteed to support the transport of multi-byte
107 characters.
108  
109 =item *
110  
111 Key names might not be unique. OpenToken supports multiple values for a key
112 name by simply adding another key-value pair.
113  
114 =item *
115  
116 Key names are case-sensitive. It is RECOMMENDED that all key names be
117 lowercase and use hyphens to separate "words".
118  
119 =back
120  
121 =head1 METHODS
122  
123 =over
124  
125 =item Crypt::OpenToken::Serializer::thaw($string)
126  
127 Thaws the given serialized data, returning a hash of data back to the caller.
128  
129 If the data contained any repeating keys, those are represented in the hash as
130 having an ARRAYREF as a value.
131  
132 =item Crypt::OpenToken::Serializer::freeze(%data)
133  
134 Freezes the given data, returning a serialized string back to the caller.
135  
136 =back
137  
138 =head1 AUTHOR
139  
140 Graham TerMarsch (cpan@howlingfrog.com)
141  
142 =head1 COPYRIGHT & LICENSE
143  
144 C<Crypt::OpenToken> is Copyright (C) 2010, Socialtext, and is released under
145 the Artistic-2.0 license.
146  
147 =cut
« OpenToken.pm ↑ Summary OpenToken/Token.pm »