Created
December 6, 2011 06:02
-
-
Save semifor/1436955 to your computer and use it in GitHub Desktop.
For Lady_Aleena - revised Twitter::Keys
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 Twitter::Keys; | |
| use strict; | |
| use warnings; | |
| use base 'Exporter'; | |
| our @EXPORT = qw(consumer_key consumer_secret access_token access_token_secret); | |
| my %tokens; | |
| # I used <DATA> for simplicity | |
| for ( <DATA> ) { | |
| chomp; | |
| my @line = split /\|/; | |
| @{$tokens{$line[-1]}}{qw(access_token access_token_secret user_id screen_name)} = @line; | |
| } | |
| # | |
| # I used the same names for the subroutines twitter uses. Thought it would | |
| # be less confusing to have access_token than access_key, e.g. | |
| # | |
| # | |
| # The prototype () makes them constants | |
| # | |
| sub consumer_key () { 'in there' } | |
| sub consumer_secret () { 'in there too' } | |
| # | |
| # Not that there's much savings, here, but when 2 or more subroutines do | |
| # virtually the same thing, I put all the logic in a central subroutine | |
| # and call it with the parameter(s) necessary to change the behavior. | |
| # | |
| sub access_token { _element_by_screen_name(access_token => @_) } | |
| sub access_token_secret { _element_by_screen_name(access_token_secret => @_) } | |
| sub _element_by_screen_name { | |
| my ($element, $screen_name) = @_; | |
| return exists $tokens{$screen_name} && $tokens{$screen_name}{$element}; | |
| } | |
| 1; | |
| __DATA__ | |
| my-token|my-secret|1234|foo |
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
| #!perl | |
| use warnings; | |
| use strict; | |
| use Test::More tests => 6; | |
| BEGIN { use_ok 'Twitter::Keys' } | |
| is consumer_key(), 'in there', 'consumer_key'; | |
| is consumer_secret(), 'in there too', 'consumer_secret'; | |
| is access_token('foo'), 'my-token', 'access_token'; | |
| is access_token_secret('foo'), 'my-secret', 'access_token_secret'; | |
| is access_token('bar'), '', 'non-existent key'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment