API-Twitter

 view release on metacpan or  search on metacpan

lib/API/Twitter.pm  view on Meta::CPAN

# ABSTRACT: Twitter.com API Client
package API::Twitter;

use Data::Object::Class;
use Data::Object::Signatures;

use Data::Object::Library qw(
    Str
);

use Net::OAuth ();

extends 'API::Client';

our $VERSION = '0.05'; # VERSION

our $DEFAULT_URL = "https://api.twitter.com";

$Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;

our $VERSION = '0.05'; # VERSION

# ATTRIBUTES

has consumer_key => (
    is       => 'ro',
    isa      => Str,
    required => 1,
);

has consumer_secret => (
    is       => 'rw',
    isa      => Str,
    required => 1,
);

has access_token => (
    is       => 'rw',
    isa      => Str,
    required => 0,
);

has access_token_secret => (
    is       => 'rw',
    isa      => Str,
    required => 0,
);

has oauth_type => (
    is       => 'rw',
    isa      => Str,
    default  => 'protected resource',
    required => 0,
);

# DEFAULTS

has '+identifier' => (
    default  => 'API::Twitter (Perl)',
    required => 0,
);

has '+url' => (
    default  => $DEFAULT_URL,
    required => 0,
);

has '+version' => (
    default  => 1.1,
    required => 0,
);

# CONSTRUCTION

after BUILD => method {

    my $identifier = $self->identifier;
    my $version    = $self->version;
    my $agent      = $self->user_agent;
    my $url        = $self->url;

    $agent->transactor->name($identifier);

    $url->path("/$version");

    return $self;

};

# METHODS

method PREPARE ($ua, $tx, %args) {

    my $req     = $tx->req;
    my $headers = $req->headers;
    my $params  = $req->params->to_hash;
    my $url     = $req->url;

    # default headers
    $headers->header('Content-Type' => 'application/json');

    # append path suffix
    $url->path("@{[$url->path]}.json") if $url->path !~ /\.json$/;

    # oauth data
    my $consumer_key        = $self->consumer_key;
    my $consumer_secret     = $self->consumer_secret;
    my $access_token        = $self->access_token;
    my $access_token_secret = $self->access_token_secret;

    # oauth variables
    my $oauth_consumer_key     = $consumer_key;
    my $oauth_nonce            = Digest::SHA::sha1_base64(time . $$ . rand);
    my $oauth_signature_method = 'HMAC-SHA1',
    my $oauth_timestamp        = time,
    my $oauth_token            = $access_token,
    my $oauth_version          = '1.0';

    # oauth object
    my $base  = $url->clone; $base->query(undef);
    my $oauth = Net::OAuth->request($self->oauth_type)->new(%$params,
        version          => '1.0',
        consumer_key     => $consumer_key,
        consumer_secret  => $consumer_secret,
        request_method   => uc($req->method),
        request_url      => $base,
        signature_method => 'HMAC-SHA1',
        timestamp        => time,
        token            => $access_token,
        token_secret     => $access_token_secret,



( run in 2.325 seconds using v1.01-cache-2.11-cpan-13bb782fe5a )