Skip to content

Commit

Permalink
Added twitter app lookup table and coding
Browse files Browse the repository at this point in the history
  • Loading branch information
stevestreza committed Jul 16, 2012
1 parent 42d3b44 commit 9f8ff52
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Source/ETWAccount.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
//

#import "ETWAccount.h"
#import "ETWTwitterApp.h"

@implementation ETWAccount

Expand All @@ -43,6 +44,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_username forKey:@"username"];
[aCoder encodeObject:_accessTokenKey forKey:@"accessTokenKey"];
[aCoder encodeObject:_accessTokenSecret forKey:@"accessTokenSecret"];
[aCoder encodeObject:self.app.consumerKey forKey:@"appConsumerKey"];
}

- (id)initWithCoder:(NSCoder *)aDecoder{
Expand All @@ -51,6 +53,7 @@ - (id)initWithCoder:(NSCoder *)aDecoder{
_username = [aDecoder decodeObjectForKey:@"username"];
_accessTokenKey = [aDecoder decodeObjectForKey:@"accessTokenKey"];
_accessTokenSecret = [aDecoder decodeObjectForKey:@"accessTokenSecret"];
self.app = [ETWTwitterApp appWithConsumerKey:[aDecoder decodeObjectForKey:@"appConsumerKey"]];
}
return self;
}
Expand Down
2 changes: 2 additions & 0 deletions Source/ETWTwitterApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ typedef void(^ETWTwitterLoginVerifier)(NSString *verifier);

-(void)addAccount:(ETWAccount *)account;

+(ETWTwitterApp *)appWithConsumerKey:(NSString *)key;

// advanced use only
@property (nonatomic, strong) Class accountClass; // defaults to ETWAccount, override in subclasses if needed
@property (nonatomic, strong) Class requestClass; // defaults to ETWRequest, override in subclasses if needed
Expand Down
28 changes: 28 additions & 0 deletions Source/ETWTwitterApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ @interface ETWTwitterApp ()

@implementation ETWTwitterApp

static NSMutableDictionary *sAppsByConsumerKey = NULL;

+(ETWTwitterApp *)appWithConsumerKey:(NSString *)key{
return sAppsByConsumerKey[key];
}

+(void)registerConsumerKeyOfApp:(ETWTwitterApp *)app{
if(!app.consumerKey) return;

if(!sAppsByConsumerKey){
sAppsByConsumerKey = [NSMutableDictionary dictionary];
}
[sAppsByConsumerKey setObject:app forKey:app.consumerKey];
}

+(void)deregisterConsumerKeyOfApp:(ETWTwitterApp *)app{
if(sAppsByConsumerKey && app.consumerKey){
[sAppsByConsumerKey removeObjectForKey:app.consumerKey];
}
}

-(id)init{
if(self = [super init]){
_operationQueue = [[NSOperationQueue alloc] init];
Expand Down Expand Up @@ -71,6 +92,7 @@ -(void)loginWithCallbackURL:(NSURL *)url
accessTokenRequest.type = ETWRequestTypeOAuth;
accessTokenRequest.APIMethod = @"access_token";
accessTokenRequest.app = self;
accessTokenRequest.secure = YES;
accessTokenRequest.parameters = @{ @"oauth_verifier" : verifier, @"oauth_token" : response[@"oauth_token"] };
[accessTokenRequest performOnQueue:opQueue handler:^(NSDictionary *response, NSError *error, ETWRequest *request) {
NSLog(@"Access token! %@", response);
Expand Down Expand Up @@ -113,4 +135,10 @@ -(Class)requestClass{
return _requestClass;
}

-(void)setConsumerKey:(NSString *)consumerKey{
[[self class] deregisterConsumerKeyOfApp:self];
_consumerKey = consumerKey;
[[self class] registerConsumerKeyOfApp:self];
}

@end

0 comments on commit 9f8ff52

Please sign in to comment.