Type checking, we're only constructed once per app, so lets blow up nicely at the...
[catagits/Catalyst-Authentication-Credential-FBConnect.git] / lib / Catalyst / Authentication / Credential / FBConnect.pm
index 33b0c07..6d549ef 100644 (file)
@@ -1,43 +1,34 @@
 package Catalyst::Authentication::Credential::FBConnect;
-use strict;
-use warnings;
-
 use Moose;
-
-has _config => ( is => 'rw' );
-has debug => ( is => 'rw' );
-has key => ( is => 'rw' );
-has secret => ( is => 'rw' );
-has app_name => ( is => 'rw' );
-has fbconnect => ( is => 'rw' );
-
+use MooseX::Types::Moose qw/ Bool /;
+use MooseX::Types::Common::String qw/ NonEmptySimpleStr /;
 use WWW::Facebook::API;
 use Catalyst::Exception ();
+use namespace::autoclean;
 
-sub new {
-       my ($class, $config, $c, $realm) = @_;
-
-       my $self = { _config => {
-               %{ $config },
-               %{ $realm->{config} }
-       } };
+has debug => ( is => 'ro', isa => Bool, );
+has key => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
+has secret => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
+has app_name => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
+has fbconnect => ( is => 'ro', lazy_build => 1, init_arg => undef, isa => 'WWW::Facebook::API' );
 
-       bless $self, $class;
+sub BUILDARGS {
+       my ($class, $config, $c, $realm) = @_;
 
-       $self->debug( $self->_config->{debug} );
+    return $config;
+}
 
-       $self->key( $self->_config->{key} );
-       $self->secret( $self->_config->{secret} );
-       $self->app_name( $self->_config->{app_name} );
+sub BUILD {
+    my ($self) = @_;
+    $self->fbconnect; # Ensure lazy value is built.
+}
 
-       $self->fbconnect( WWW::Facebook::API->new(
+sub _build_fbconnect {
+    my $self = shift;
+       WWW::Facebook::API->new(
                desktop => 0,
-               app_name => $self->app_name,
-               api_key => $self->key,
-               secret => $self->secret
-       ) );
-
-       return $self;
+               map { $_ => $self->$_() } qw/ app_name api_key secret /
+       );
 }
 
 sub authenticate {