Created
November 23, 2009 20:18
-
-
Save semifor/241341 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Net::Twitter::Role::RateLimit; | |
| use Moose::Role; | |
| use namespace::autoclean; | |
| requires qw/ua/; | |
| has rate_remaining => ( isa => 'Int', is => 'rw', init_arg => undef, lazy => 1, | |
| default => sub { shift->_ensure_rate('rate_remaining') } ); | |
| has rate_reset => ( isa => 'Int', is => 'rw', init_arg => undef, lazy => 1, | |
| default => sub { shift->_ensure_rate('rate_reset' ) } ); | |
| has rate_limit => ( isa => 'Int', is => 'rw', init_arg => undef, lazy => 1, | |
| default => sub { shift->_ensure_rate('rate_limit' ) } ); | |
| sub _ensure_rate { | |
| my ( $self, $attr ) = @_; | |
| my $r = $self->rate_limit_status || return; | |
| return $self->{$attr}; | |
| } | |
| around rate_limit_status => sub { | |
| my $orig = shift; | |
| my $self = shift; | |
| my $r = $self->$orig(@_) || return; | |
| @{$self}{qw/rate_remaining rate_reset rate_limit/} = | |
| @{$r}{qw/remaining_hits reset_time_in_seconds hourly_limit/}; | |
| return $r; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment