| 1 | package Catalyst::View::vCard; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 2 | ▶ | use base qw( Catalyst::View ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 2 | ▶ | use Text::vCard::Addressbook; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 2 | our $VERSION = '0.04'; | |||||||||||||
| 11 | |||||||||||||||
| 12 | 2 | my @fields = qw( | |||||||||||||
| 13 | fn fullname email bd birthday mailer tz timezone | ||||||||||||||
| 14 | title role note prodid rev uid url class nickname | ||||||||||||||
| 15 | photo version | ||||||||||||||
| 16 | ); | ||||||||||||||
| 17 | |||||||||||||||
| 18 | =head1 NAME | ||||||||||||||
| 19 | |||||||||||||||
| 20 | Catalyst::View::vCard - vCard view for Catalyst | ||||||||||||||
| 21 | |||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||
| 23 | |||||||||||||||
| 24 | # in a controller... | ||||||||||||||
| 25 | my $profile = $foo; | ||||||||||||||
| 26 | |||||||||||||||
| 27 | $c->stash->{ vcards } = [ $profile ]; | ||||||||||||||
| 28 | $c->stash->{ filename } = $profile->username; | ||||||||||||||
| 29 | $c->forward( $c->view( 'vCard' ) ); | ||||||||||||||
| 30 | |||||||||||||||
| 31 | # in a view... | ||||||||||||||
| 32 | package MyApp::View::vCard; | ||||||||||||||
| 33 | |||||||||||||||
| 34 | use base qw( Catalyst::View::vCard ); | ||||||||||||||
| 35 | |||||||||||||||
| 36 | sub convert_to_vcard { | ||||||||||||||
| 37 | my( $self, $c, $profile, $vcard ) = @_; | ||||||||||||||
| 38 | |||||||||||||||
| 39 | $vcard->nickname( $profile->username ); | ||||||||||||||
| 40 | $vcard->fullname( $profile->name ) if $profile->name; | ||||||||||||||
| 41 | $vcard->email( $profile->email ) if $profile->show_email; | ||||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | =head1 DESCRIPTION | ||||||||||||||
| 45 | |||||||||||||||
| 46 | This is a view to help you serialize objects to vCard output. You can configure | ||||||||||||||
| 47 | the output filename by supplying a name in C<$c->stash->{ filename }> (a C<.vcf> | ||||||||||||||
| 48 | extension will automatically added for you). A default C<convert_to_vcard> | ||||||||||||||
| 49 | implementation is provided, however you can provide your own to map your object | ||||||||||||||
| 50 | to a L<Text::vCard> object. | ||||||||||||||
| 51 | |||||||||||||||
| 52 | =head1 METHODS | ||||||||||||||
| 53 | |||||||||||||||
| 54 | =head2 process( \@vcards ) | ||||||||||||||
| 55 | |||||||||||||||
| 56 | This method will loop through and call C<convert_to_vcard> on all of the items in the | ||||||||||||||
| 57 | C<vcards> key of the stash. | ||||||||||||||
| 58 | |||||||||||||||
| 59 | =cut | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 0 | ▶ | sub process { | ||||||||||||
|
Subroutine
process: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||
| 62 | 0 | my ( $self, $c, $vcards ) = @_; | |||||||||||||
| 63 | 0 | ▶ | $vcards = $c->stash->{ vcards } unless ref $vcards; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | 0 | my $book = Text::vCard::Addressbook->new; | |||||||||||||
| 65 | |||||||||||||||
| 66 | 0 | for my $object ( @$vcards ) { | |||||||||||||
| 67 | 0 | my $vcard = $book->add_vcard; | |||||||||||||
| 68 | 0 | $self->convert_to_vcard( $c, $object, $vcard ); | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 0 | ▶ | my $filename = $c->stash->{ filename } || 'vcard'; | ||||||||||||
|
Condition0%0/2
$c->stash->{'filename'} || 'vcard'
MC/DC0%0/1
$c->stash->{'filename'} || 'vcard'
$c->stash->{'filename'}
Truth table0%0/2
A
A= $c->stash->{'filename'}
| |||||||||||||||
| 72 | |||||||||||||||
| 73 | 0 | $c->res->content_type( 'text/x-vcard; charset: UTF-8' ); | |||||||||||||
| 74 | 0 | $c->res->header( | |||||||||||||
| 75 | 'Content-Disposition' => qq(inline; filename="$filename.vcf") ); | ||||||||||||||
| 76 | 0 | $c->res->body( $book->export ); | |||||||||||||
| 77 | |||||||||||||||
| 78 | 0 | return 1; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | =head2 convert_to_vcard( $self, $c, $in, $out ) | ||||||||||||||
| 82 | |||||||||||||||
| 83 | This is a default implementation for converting your items to vCard | ||||||||||||||
| 84 | objects. It will try various hash keys or methods based on the naming | ||||||||||||||
| 85 | scheme of L<Text::vCard>'s methods. | ||||||||||||||
| 86 | |||||||||||||||
| 87 | =cut | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 0 | ▶ | sub convert_to_vcard { | ||||||||||||
|
Subroutine
convert_to_vcard: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||
| 90 | 0 | my ( $self, $c, $in, $out ) = @_; | |||||||||||||
| 91 | |||||||||||||||
| 92 | 0 | ▶ | return unless my $type = ref $in; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | |||||||||||||||
| 94 | 0 | for ( @fields ) { | |||||||||||||
| 95 | my $value | ||||||||||||||
| 96 | 0 | ▶ | = $type eq 'HASH' ? $in->{ $_ } | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 97 | : $in->can( $_ ) ? $in->$_ | ||||||||||||||
| 98 | : undef; | ||||||||||||||
| 99 | 0 | ▶ | $out->$_( $value ) if $value; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | } | ||||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | =head1 AUTHOR | ||||||||||||||
| 104 | |||||||||||||||
| 105 | Brian Cassidy E<lt>bricas@cpan.orgE<gt> | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 108 | |||||||||||||||
| 109 | Copyright 2006-2009 by Brian Cassidy | ||||||||||||||
| 110 | |||||||||||||||
| 111 | This library is free software; you can redistribute it and/or modify | ||||||||||||||
| 112 | it under the same terms as Perl itself. | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =head1 SEE ALSO | ||||||||||||||
| 115 | |||||||||||||||
| 116 | =over 4 | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =item * L<Catalyst> | ||||||||||||||
| 119 | |||||||||||||||
| 120 | =item * L<Text::vCard> | ||||||||||||||
| 121 | |||||||||||||||
| 122 | =back | ||||||||||||||
| 123 | |||||||||||||||
| 124 | =cut | ||||||||||||||
| 125 | |||||||||||||||
| 126 | 2 | 1; | |||||||||||||