WWW-Facebook-API

 view release on metacpan or  search on metacpan

lib/WWW/Facebook/API.pm  view on Meta::CPAN

    api_version  => '1.0',
    apps_uri     => 'http://apps.facebook.com/',
    server_uri   => 'http://api.facebook.com/restserver.php',
    (   map { $_ => q{} }
            qw(
            api_key             secret      desktop
            last_call_success   last_error  skipcookie
            popup               next        session_key
            session_expires     session_uid callback
            app_path            ua          query
            config              app_id      call_as_apikey
            )
    ),
);

for ( keys %attributes ) {
    ## no critic
    my $attribute = eval qq( 
        sub $_ {
            my \$self = shift;
            return \$self->{$_} = shift if defined \$_[0];
            return \$self->{$_} if defined \$self->{$_};
            return \$self->{$_} = '$attributes{$_}';
        }
        1;
    );
    croak "Cannot create attribute $_: $@\n" if not $attribute;
}

sub _set_from_outside {
    my $self = shift;

    my $app_path = '_' . ( $self->{'app_path'} || $self->app_path );
    $app_path =~ tr/a-z/A-Z/;
    $app_path =~ tr/A-Za-z0-9_/_/c;

    my %ENV_VARS = qw(
        WFA_API_KEY     api_key
        WFA_SECRET      secret
        WFA_DESKTOP     desktop
        WFA_SESSION_KEY session_key
    );

    $self->_set_from_file( $app_path, %ENV_VARS ) if $self->{'config'};
    $self->_set_from_env( $app_path, %ENV_VARS );

    return;
}

sub _set_from_file {
    my $self     = shift;
    my $app_path = shift;
    my %ENV_VARS = @_;

    open my $config, '<', $self->{'config'}    ## no critic
        or croak "Cannot open $self->{'config'}";

    while (<$config>) {
        carp "Config line: $_" if $self->{'debug'};
        chomp;
        my ( $key, $val ) = split m/=/xms, $_, 2;
        next if !$key;
        carp "Key/Val pair: $key -> $val" if $self->{'debug'};
        for ( $key, $val ) {
            s/\A\s+//xms;
            s/\s+\Z//xms;
        }
        $ENV{$key} ||= $val;
    }

    close $config or croak "Cannot close $self->{'config'}";

    return;
}

sub _set_from_env {
    my $self     = shift;
    my $app_path = shift;
    my %ENV_VARS = @_;

    for ( keys %ENV_VARS ) {
        if ( exists $ENV{ $_ . $app_path } ) {
            $self->{ $ENV_VARS{$_} } ||= $ENV{ $_ . $app_path };
        }
        elsif ( exists $ENV{$_} ) {
            $self->{ $ENV_VARS{$_} } ||= $ENV{$_};
        }
    }

    return;
}

sub new {
    my ( $self, %args ) = @_;
    my $class = ref $self || $self;
    $self = bless \%args, $class;
    $self->_set_from_outside();    # set api_key etc. if needed

    $self->{'ua'} ||=
        LWP::UserAgent->new( agent => "Perl-WWW-Facebook-API/$VERSION" );
    my $is_attribute = join q{|}, keys %attributes;
    delete $self->{$_} for grep { !/^ $is_attribute $/xms } keys %{$self};

    # set up default namespaces
    $self->$_($self) for map {"\L$_"} @namespaces;

    # set up default attributes
    $self->$_ for keys %attributes;

    return $self;
}

sub log_string {
    my ( $self, $params, $response ) = @_;
    my $string = "\nparams = \n";

    $string .= "\t$_:$params->{$_}\n" for sort keys %{$params};
    $string .= "response =\n$response\n";

    return $string;
}



( run in 0.542 second using v1.01-cache-2.11-cpan-71847e10f99 )