Skip to content

Instantly share code, notes, and snippets.

@semifor
Created June 19, 2010 00:04
Show Gist options
  • Select an option

  • Save semifor/444403 to your computer and use it in GitHub Desktop.

Select an option

Save semifor/444403 to your computer and use it in GitHub Desktop.
package Net::Twitter::Role::OAuth::Netrc::Credentials;
use Moose::Role;
use Carp;
use Net::Netrc;
=head1 SYNOPSIS
use Net::Twitter;
my $nt = Net::Twitter->new(
traits => [qw/API::REST OAuth::Netrc::Credentials/],
username => $my_twitter_name,
app_name => $my_twitter_app_name,
);
Expects .netrc entries is the following format:
# $HOME/.netrc
machine APP_NAME.oauth.twitter
login CONSUMER_KEY
password CONSUMER_SECRET
machine TWITTER_USER_NAME.APP_NAME.oauth.twitter
login ACCESS_TOKEN
password ACCESS_TOKEN_SECRET
=cut
with 'Net::Twitter::Role::OAuth';
sub _netrc_lpa {
my $self = shift;
my $machine = join '.', @_;
my $netrc = Net::Netrc->lookup($machine)
|| croak ".netrc machine $machine does not exist";
return $netrc->lpa;
}
around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
my $args = $class->$orig(@_);
my $app_name = delete $args->{app_name} || croak "attribute app_name is required";
my $username = delete $args->{username} || croak "attribute username is required";
my $base_name = delete $args->{netrc_base_name} || 'oauth.twitter';
@{$args}{qw/consumer_key consumer_secret/} = $class->_netrc_lpa($app_name, $base_name);
@{$args}{qw/access_token access_token_secret/} = $class->_netrc_lpa($username, $app_name, $base_name);
return $args;
};
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment