API-Handle
view release on metacpan or search on metacpan
lib/API/Handle.pm view on Meta::CPAN
use feature ':5.10';
use String::CamelCase qw/camelize decamelize/;
use Tie::Hash::Indexed;
has _config => (
is => 'rw'
, isa => 'Nour::Config'
, handles => [ qw/config/ ]
, required => 1
, lazy => 1
, default => sub {
require Nour::Config;
return new Nour::Config ( -base => 'config' );
}
);
has _printer => (
is => 'rw'
, isa => 'Nour::Printer'
, handles => [ qw/verbose debug info warn warning error fatal dumper/ ]
, required => 1
, lazy => 1
, default => sub {
my $self = shift;
my %conf = $self->config->{printer} ? %{ $self->config->{printer} } : (
timestamp => 1
, verbose => 1
, dumper => 1
, debug => 1
, pid => 1
);
require Nour::Printer;
return new Nour::Printer ( %conf );
}
);
has _database => (
is => 'rw'
, isa => 'Nour::Database'
, required => 1
, lazy => 1
, default => sub {
my $self = shift;
my %conf = $self->config->{database} ? %{ $self->config->{database} } : (
# default options here
);
%conf = ();
require Nour::Database;
return new Nour::Database ( %conf );
}
);
#has _util # TODO factor all the _methods to $self->util->methods... or not
has _json => (
is => 'rw'
, isa => 'JSON::XS'
, lazy => 1
, required => 1
, default => sub {
require JSON::XS;
return JSON::XS->new->utf8->ascii->relaxed;
}
);
has _xml => (
is => 'rw'
, isa => 'XML::TreePP'
, lazy => 1
, required => 1
, default => sub {
require XML::TreePP;
return new XML::TreePP (
output_encoding => 'UTF-8'
, utf8_flag => 1
, attr_prefix => '-'
, indent => 2
, use_ixhash => 1
);
}
);
has ua => (
is => 'rw'
, isa => 'LWP::UserAgent'
, lazy => 1
, required => 1
, default => sub {
require LWP::UserAgent;
return new LWP::UserAgent;
}
);
has uri => (
is => 'rw'
, isa => 'Str'
, required => 1
, lazy => 1
, default => sub { '' }
);
sub BUILD {
my $self = shift;
# Initialize attributes like 'uri' that may be set
# in the configuration YAML.
for my $attr ( keys %{ $self->config } ) {
$self->$attr( $self->config->{ $attr } )
if $self->can( $attr );
lib/API/Handle/Google/DFP.pm view on Meta::CPAN
has _config => (
is => 'rw'
, isa => 'Nour::Config'
, handles => {
config => 'config'
, merge_config => 'merge'
, write_config => 'write'
}
, required => 1
, lazy => 1
, default => sub {
my $self = shift;
require Nour::Config;
return new Nour::Config (
-base => 'config/google/dfp'
);
}
);
# This is where we configure how the user-agent transforms
# outgoing and incoming requests and responses.
lib/API/Handle/Google/DFP.pm view on Meta::CPAN
$prev = $self->$next( @args );
# Put code that depends on API::Handle::BUILD after this $prev line.
my $conf = $self->config;
my $time = time;
# Uncomment this to view loaded configuration.
# $self->dumper( 'config', $conf );
if ( $conf->{auth}{token}{access_token} and $conf->{auth}{token}{expires_at} and $conf->{auth}{token}{expires_at} > $time ) {
$self->ua->default_header( 'Authorization' => "$conf->{auth}{token}{token_type} $conf->{auth}{token}{access_token}" );
}
elsif ( $conf->{auth}{token}{refresh_token} ) {
my $req = new HTTP::Request;
$req->headers->header( 'Content-Type' => 'application/x-www-form-urlencoded' );
$req->method( 'POST' );
$req->uri( $conf->{auth}{uri}{token} );
my $data = $self->_encode( type => 'form', data => {
grant_type => 'refresh_token'
lib/API/Handle/Google/DFP.pm view on Meta::CPAN
if ( $res->code == 200 ) {
my $data = $self->_decode( type => 'json', data => $res->content );
$data->{expires_at} = time + $data->{expires_in};
$self->merge_config( $conf->{auth}->{token}, $data );
$self->write_config( 'config/google/dfp/auth/private/token.yml', $conf->{auth}->{token} );
}
}
if ( $conf->{auth}{token}{access_token} and $conf->{auth}{token}{expires_at} and $conf->{auth}{token}{expires_at} > $time ) {
$self->ua->default_header( 'Authorization' => "$conf->{auth}{token}{token_type} $conf->{auth}{token}{access_token}" );
}
else {
carp 'no access token';
}
# Setup match-spec vars for request_prepare.
my ( $scheme, $host, $path ) = $self->uri =~ /^(https?):\/\/([^\/]+)(\/.+)$/;
# Add request wrapper.
$self->ua->add_handler(
lib/API/Handle/OpenX.pm view on Meta::CPAN
has _config => (
is => 'rw'
, isa => 'Nour::Config'
, handles => {
config => 'config'
, merge_config => 'merge'
, write_config => 'write'
}
, required => 1
, lazy => 1
, default => sub {
my $self = shift;
require Nour::Config;
return new Nour::Config (
-base => 'config/openx'
);
}
);
# This is where we configure how the user-agent transforms
# outgoing and incoming requests and responses.
( run in 0.757 second using v1.01-cache-2.11-cpan-0a6323c29d9 )