WWW-Phanfare-Class

 view release on metacpan or  search on metacpan

lib/WWW/Phanfare/Class.pm  view on Meta::CPAN

package WWW::Phanfare::Class;
use Moose;
use MooseX::Method::Signatures;
use WWW::Phanfare::Class::CacheAPI;
use WWW::Phanfare::API;
use WWW::Phanfare::Class::Account;

has 'api_key'       => ( is=>'ro', isa=>'Str', required=>1 );
has 'private_key'   => ( is=>'ro', isa=>'Str', required=>1 );
has 'email_address' => ( is=>'ro', isa=>'Str' );
has 'password'      => ( is=>'ro', isa=>'Str' );
sub _childclass { 'WWW::Phanfare::Class::Account' }

# Initialize account
has 'account' => (
  is         => 'ro',
  isa        => 'WWW::Phanfare::Class::Account',
  lazy_build => 1,
);
sub _build_account {
  my $self = shift;

  my $api = $self->api;

  # Login to create session
  my $session;
  if ( $self->email_address and $self->password ) {
    $session = $api->Authenticate(
      email_address => $self->email_address,
      password      => $self->password,
    );
  } else {
    $session = $api->AuthenticateGuest();
  }

  warn sprintf "*** Error: Could not login: %s\n", $session->{code_value}
    unless $session->{stat} eq 'ok';

  # Create account object with session data
  #my $account = WWW::Phanfare::Class::Account->new(
  my $type = $self->_childclass;
  my $account = $type->new(
    uid => $session->{session}{uid},
    gid => $session->{session}{public_group_id},
    parent => $self,
    name => '',
    id => 0,
  );
  $account->setattributes( $session->{session} );
  return $account;
} 

# Initialize API  
has api => (
  isa        => 'WWW::Phanfare::API',
  is         => 'rw',
  lazy_build => 1,
);
sub _build_api {
  my $self = shift;

  # Create an API Agent
  WWW::Phanfare::Class::CacheAPI->new(
    api_key     => $self->api_key,
    private_key => $self->private_key,
  );
}


# Get a subnode, by name of name.id
#
method get ( Str $name ) {
  $self->account->list;
}

sub AUTOLOAD {
  my $self = shift @_;
  our $AUTOLOAD;

  my $name = $AUTOLOAD;
  $name =~ s/.*:://;

  return $self->get($name);
}


=head1 NAME

WWW::Phanfare::Class - Object interface to Phanfare library

=head1 VERSION

Version 0.03

=cut

our $VERSION = '0.03';

=head1 SYNOPSIS

    use WWW::Phanfare::Class;

    $class = WWW::Phanfare::Class->new(
      api_key       => 'secret',
      private_key   => 'secret',
      email_address => 's@c.et',
      password      => 'secret',
    );

    # Site Name
    my($sitename) = $class->account->names;

    # Site Object
    my $site = $class->account->$sitename;

    # Album Names
    my @albums = $site->names;

    # Photo filenames in Album's Main Section
    my @filenames = $site->"Family Vacation"->"Main Section"->Full->names;

    # Upload Image and set Caption
    my $folder = $site->"Family Vacation"->"Main Section"->Full;
    my $image = $folder->add( 'filename.jpg', slurp 'filename.jpg', '2009-09-15T00:00:00' );
    $image->attribute( Caption => 'Family on Vacation' );


=head1 DESCRIPTION

WWW::Phanfare::Class creates an object tree for Phanfare site. Each 
Phanfare object can be referred to by name or by object reference.


=head1 BEFORE GETTING STARTED

Users of this module must possess own Phanfare account and API key.


=head1 TREE HIERARKI

Class is the top object and it has one Account to access data using
Phanfare API.

The tree hierarki is as follows:

=over

    An Account has a nuber of Sites.
    A Site has a number of Years.
    A Year has a number of Albums.
    An Album has a number of Sections.
    A Section has a number of Renditions.
    A Rendition has a number of Images.

=back


=head1 NAME CLASH

In Phanfare it's possible for multiple objects to have same name. For example
multi albums can have same name. In such cases WWW::Phanfare::Class will
append the id on the names that are not uniq. Example:

    Kids Photos
    Family Vacation.12345678



( run in 1.426 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )