File Coverage

blib/lib/Text/vCard/Precisely/Multiple.pm
Criterion Covered Total %
statement 51 51 100.0
branch 4 8 50.0
condition n/a
subroutine 11 11 100.0
pod 4 4 100.0
total 70 74 94.5


line stmt bran cond sub pod time code
1             package Text::vCard::Precisely::Multiple;
2              
3             our $VERSION = '0.27';
4              
5 1     1   102757 use Moose;
  1         381787  
  1         8  
6 1     1   6468 use Moose::Util::TypeConstraints;
  1         2  
  1         8  
7              
8 1     1   1828 use Carp;
  1         1  
  1         70  
9 1     1   592 use Text::vCard::Precisely;
  1         4  
  1         85  
10 1     1   9 use Text::vFile::asData;
  1         2  
  1         15  
11             my $vf = Text::vFile::asData->new();
12 1     1   60 use Path::Tiny;
  1         2  
  1         187  
13              
14             enum 'Version' => [qw( 3.0 4.0 )];
15             has version => ( is => 'ro', isa => 'Version', default => '3.0', required => 1 );
16              
17             subtype 'vCards' => as 'ArrayRef[Text::vCard::Precisely]';
18             coerce 'vCards', from 'Text::vCard::Precisely', via { [$_] };
19             has options => (
20             traits => ['Array'],
21             is => 'ro',
22             isa => 'vCards',
23             coerce => 1,
24             default => sub { [] },
25             handles => {
26             all_options => 'elements',
27             add_option => 'push',
28             clear_options => 'clear',
29              
30             #map_options => 'map',
31             #filter_options => 'grep',
32             #find_option => 'first',
33             #get_option => 'get',
34             #join_options => 'join',
35             count_options => 'count',
36              
37             #has_options => 'count',
38             has_no_options => 'is_empty',
39              
40             #sorted_options => 'sort',
41             },
42             );
43              
44             __PACKAGE__->meta->make_immutable;
45 1     1   7 no Moose;
  1         2  
  1         8  
46              
47             sub load_arrayref {
48 1     1 1 2592 my $self = shift;
49 1         3 my $ref = shift;
50 1 50       4 croak "Attribute must be an ArrayRef: $ref" unless ref($ref) eq 'ARRAY';
51 1         38 $self->clear_options();
52              
53 1         3 foreach my $data (@$ref) {
54 2         55 my $vc = Text::vCard::Precisely->new( version => $self->version() );
55 2         11 $self->add_option( $vc->load_hashref($data) );
56             }
57 1         5 return $self;
58             }
59              
60             sub load_file {
61 1     1 1 858 my $self = shift;
62 1         2 my $filename = shift;
63 1 50       49 open my $vcf, "<", $filename or croak "couldn't open vcf: $!";
64 1         52 my $objects = $vf->parse($vcf)->{'objects'};
65 1         4278 close $vcf;
66              
67 1         47 $self->clear_options();
68 1         3 foreach my $data (@$objects) {
69 5 50       15 croak "$filename contains unvalid vCard data." unless $data->{'type'} eq 'VCARD';
70 5         139 my $vc = Text::vCard::Precisely->new( version => $self->version() );
71 5         20 my $hashref = $vc->_make_hashref($data);
72 5         19 $self->add_option( $vc->load_hashref($hashref) );
73             }
74 1         26 return $self;
75             }
76              
77             sub as_string {
78 2     2 1 172 my $self = shift;
79 2         4 my $str = '';
80 2         80 foreach my $vc ( $self->all_options() ) {
81 7         263 $str .= $vc->as_string();
82             }
83 2         116 return $str;
84             }
85              
86             sub as_file {
87 1     1 1 686 my ( $self, $filename ) = @_;
88 1 50       4 croak "No filename was set!" unless $filename;
89              
90 1         5 my $file = path($filename);
91 1         43 $file->spew( { binmode => ":encoding(UTF-8)" }, $self->as_string() );
92 1         1881 return $file;
93             }
94              
95             1;
96              
97             __END__
98              
99             =encoding UTF8
100              
101             =head1 NAME
102              
103             Text::vCard::Precisely::Multiple - some add-on for Text::vCard::Precisely
104              
105             =head1 SYNOPSIS
106              
107             use Text::vCard::Precisely::Multiple;
108             my $vcm = Text::vCard::Precisely::Multiple->new(); # default is 3.0
109             my $vcm4 = Text::vCard::Precisely::Multiple->new( version => '4.0' ); # for using 4.0
110            
111             my $path = path( 'some', 'dir', 'example.vcf' );
112             $vcm->load_file($path);
113              
114             or
115              
116             my $arrayref = [
117             {
118             N => [ 'Gump', 'Forrest', '', 'Mr.', '' ],
119             FN => 'Forrest Gump',
120             ORG => 'Bubba Gump Shrimp Co.',
121             TITLE => 'Shrimp Man',
122             TEL => [
123             { types => ['WORK','VOICE'], content => '(111) 555-1212' },
124             ],
125             ADR =>[{
126             types => ['work'],
127             pref => 1,
128             extended => 100,
129             street => 'Waters Edge',
130             city => 'Baytown',
131             region => 'LA',
132             post_code => '30314',
133             country => 'United States of America'
134             }],
135             EMAIL => 'forrestgump@example.com',
136             REV => '20080424T195243Z',
137             },{
138             N => [ 'One', 'Other', '', '', '' ],
139             FN => 'Other One',
140             TEL => [
141             { types => ['HOME','VOICE'], content => '(404) 555-1212', preferred => 1 },
142             ],
143             ADR =>[{
144             types => ['home'],
145             extended => 42,
146             street => 'Plantation St.',
147             city => 'Baytown',
148             region => 'LA',
149             post_code => '30314',
150             country => 'United States of America'
151             }],
152             EMAIL => 'other.one@example.com',
153             REV => '20080424T195243Z',
154             },
155             ];
156              
157             $vcm->load_arrayref($arrayref);
158              
159             and
160              
161             $vcm->as_string();
162              
163             or
164              
165             $vcm->as_file('output.vcf');
166              
167             =cut
168              
169             =head1 DESCRIPTION
170              
171             If you have a file that contains multiple vCards, This module may be useful.
172              
173             =head1 Constructors
174              
175             =head2 load_arrayref($ArrayRef)
176              
177             Accepts an ArrayRef that looks like below:
178              
179             my $arrayref = [
180             {
181             N => [ 'Gump', 'Forrest', '', 'Mr.', '' ],
182             FN => 'Forrest Gump',
183             ORG => 'Bubba Gump Shrimp Co.',
184             TITLE => 'Shrimp Man',
185             TEL => [
186             { types => ['WORK','VOICE'], content => '(111) 555-1212' },
187             ],
188             ADR =>[{
189             types => ['work'],
190             pref => 1,
191             extended => 100,
192             street => 'Waters Edge',
193             city => 'Baytown',
194             region => 'LA',
195             post_code => '30314',
196             country => 'United States of America'
197             }],
198             EMAIL => 'forrestgump@example.com',
199             REV => '20080424T195243Z',
200             },{...}
201             ];
202              
203             =head2 load_file($file_name)
204              
205             Accepts a file name
206              
207             =head1 METHODS
208              
209             =head2 as_string()
210              
211             Returns the vCards as a single string that is serialized.
212              
213             =head2 as_file($filename)
214              
215             Write vCards formated text into a single file to $filename.
216             Dies if not successful
217              
218             =head1 SIMPLE GETTERS/SETTERS
219              
220             These methods accept and return strings
221              
222             =head2 version()
223              
224             returns Version number of the vcard.
225             Defaults to B<'3.0'> and this method is B<READONLY>
226              
227             =head1 for under perl-5.12.5
228              
229             This module uses Text::vCard::Precisely and it require you to use 5.12.5 and later
230              
231             =head1 SEE ALSO
232              
233             =over
234              
235             =item
236              
237             L<RFC 2426|https://tools.ietf.org/html/rfc2426>
238              
239             =item
240              
241             L<RFC 2425|https://tools.ietf.org/html/rfc2425>
242              
243             =item
244              
245             L<RFC 6350|https://tools.ietf.org/html/rfc6350>
246              
247             =item
248              
249             L<Text::vFile::asData>
250              
251             =back
252              
253             =head1 AUTHOR
254              
255             Yuki Yoshida(L<worthmine|https://github.com/worthmine>)
256              
257             =head1 LICENSE
258              
259             This is free software; you can redistribute it and/or modify it under the same terms as Perl.