Catalyst::View::vCard - vCard view for Catalyst


Catalyst-View-vCard documentation Contained in the Catalyst-View-vCard distribution.

Index


Code Index:

NAME

Top

Catalyst::View::vCard - vCard view for Catalyst

SYNOPSIS

Top

    # in a controller...
    my $profile = $foo;

    $c->stash->{ vcards }   = [ $profile ];
    $c->stash->{ filename } = $profile->username;
    $c->forward( $c->view( 'vCard' ) );

    # in a view...
    package MyApp::View::vCard;

    use base qw( Catalyst::View::vCard );

    sub convert_to_vcard {
        my( $self, $c, $profile, $vcard ) = @_;

        $vcard->nickname( $profile->username );
        $vcard->fullname( $profile->name ) if $profile->name;
        $vcard->email( $profile->email ) if $profile->show_email;
    }

DESCRIPTION

Top

This is a view to help you serialize objects to vCard output. You can configure the output filename by supplying a name in $c-stash->{ filename }> (a .vcf extension will automatically added for you). A default convert_to_vcard implementation is provided, however you can provide your own to map your object to a Text::vCard object.

METHODS

Top

process( \@vcards )

This method will loop through and call convert_to_vcard on all of the items in the vcards key of the stash.

convert_to_vcard( $self, $c, $in, $out )

This is a default implementation for converting your items to vCard objects. It will try various hash keys or methods based on the naming scheme of Text::vCard's methods.

AUTHOR

Top

Brian Cassidy <bricas@cpan.org>

COPYRIGHT AND LICENSE

Top

SEE ALSO

Top

* Catalyst
* Text::vCard

Catalyst-View-vCard documentation Contained in the Catalyst-View-vCard distribution.
package Catalyst::View::vCard;

use strict;
use warnings;

use base qw( Catalyst::View );

use Text::vCard::Addressbook;

our $VERSION = '0.04';

my @fields = qw(
    fn fullname email bd birthday mailer tz timezone
    title role note prodid rev uid url class nickname
    photo version
);

sub process {
    my ( $self, $c, $vcards ) = @_;
    $vcards = $c->stash->{ vcards } unless ref $vcards;
    my $book = Text::vCard::Addressbook->new;

    for my $object ( @$vcards ) {
        my $vcard = $book->add_vcard;
        $self->convert_to_vcard( $c, $object, $vcard );
    }

    my $filename = $c->stash->{ filename } || 'vcard';

    $c->res->content_type( 'text/x-vcard; charset: UTF-8' );
    $c->res->header(
        'Content-Disposition' => qq(inline; filename="$filename.vcf") );
    $c->res->body( $book->export );

    return 1;
}

sub convert_to_vcard {
    my ( $self, $c, $in, $out ) = @_;

    return unless my $type = ref $in;

    for ( @fields ) {
        my $value
            = $type eq 'HASH' ? $in->{ $_ }
            : $in->can( $_ ) ? $in->$_
            :                  undef;
        $out->$_( $value ) if $value;
    }
}

1;