Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

Commit

Permalink
Optimize STwitterUser code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sinoru Dev committed Mar 25, 2012
1 parent 661b5e8 commit 9df269e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 76 deletions.
11 changes: 1 addition & 10 deletions STwitter/STwitterUser.h
Expand Up @@ -9,21 +9,12 @@
#import <Foundation/Foundation.h>


@interface STwitterUser : NSObject {
NSURLConnection *getUserProfileImageConnection;
NSURLConnection *getUserProfileImageURLConnection;
NSMutableData *getUserProfileImageActiveDownload;
NSURLResponse *getUserProfileImageResponse;
NSData *getUserProfileImageResult;
}
@interface STwitterUser : NSObject

- (NSDictionary *)getUserProfileImageAndURLWithScreenName:(NSString *)screenName size:(NSString *)size;
- (NSURL *)getUserProfileImageURLWithScreenName:(NSString *)screenName size:(NSString *)size;

@property (nonatomic, strong) NSMutableData *getUserProfileImageActiveDownload;
@property (nonatomic, strong) NSURLResponse *getUserProfileImageResponse;
@property (nonatomic, strong) NSData *getUserProfileImageResult;
@property (nonatomic, strong) NSURLConnection *getUserProfileImageConnection;
@property (nonatomic, strong) NSURLConnection *getUserProfileImageURLConnection;

@end
85 changes: 19 additions & 66 deletions STwitter/STwitterUser.m
Expand Up @@ -10,89 +10,58 @@

#import "STwitterOAuthTool.h"
#import "NSString (RFC3875PercentEscapes).h"
#import "STwitterRequest.h"


@implementation STwitterUser

@synthesize getUserProfileImageActiveDownload;
@synthesize getUserProfileImageResponse;
@synthesize getUserProfileImageResult;
@synthesize getUserProfileImageConnection;
@synthesize getUserProfileImageURLConnection;

- (NSDictionary *)getUserProfileImageAndURLWithScreenName:(NSString *)screenName size:(NSString *)size
{
NSMutableDictionary *httpBodyParameterDict;
NSData *httpBodyParameterData;
NSMutableDictionary *parameterDict;
NSURL *apiURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.twitter.com/1/users/profile_image/%@.json", screenName]];

// Make HTTP Body Dictionary and Data
if (size) {
httpBodyParameterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[size stringByAddingRFC3875PercentEscapesUsingEncoding:NSUTF8StringEncoding], [@"size" stringByAddingRFC3875PercentEscapesUsingEncoding:NSUTF8StringEncoding], nil];
httpBodyParameterData = [[STwitterOAuthTool generateHTTPBodyString:httpBodyParameterDict] dataUsingEncoding:NSUTF8StringEncoding];
[parameterDict setObject:size forKey:@"size"];
}

// Create Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:apiURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0f];
STwitterRequest *request = [[STwitterRequest alloc] initWithURL:apiURL parameters:parameterDict requestMethod:STwitterRequestMethodPOST];

// Set HTTP Method to POST
[request setHTTPMethod:@"POST"];
NSURLResponse *response = nil;
NSError *error = nil;

// Set HTTP Body
if (size)
[request setHTTPBody:httpBodyParameterData];
NSData *receivedData = [NSURLConnection sendSynchronousRequest:[request signedURLRequest] returningResponse:&response error:&error];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.getUserProfileImageConnection = connection;

if (getUserProfileImageConnection)
{
getUserProfileImageActiveDownload = [[NSMutableData alloc] init];

while (getUserProfileImageConnection && [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);

if (getUserProfileImageResult) {
return [NSDictionary dictionaryWithObjectsAndKeys:getUserProfileImageResult, @"image", [getUserProfileImageResponse URL], @"url", nil];
}
else {
return nil;
}
}
else {
return nil;
if (!error && receivedData) {
return [NSDictionary dictionaryWithObjectsAndKeys:receivedData, @"image", [response URL], @"url", nil];
}

return nil;
}

- (NSURL *)getUserProfileImageURLWithScreenName:(NSString *)screenName size:(NSString *)size
{
NSMutableDictionary *httpBodyParameterDict;
NSData *httpBodyParameterData;
NSMutableDictionary *parameterDict;
NSURL *apiURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.twitter.com/1/users/profile_image/%@.json", screenName]];

// Make HTTP Body Dictionary and Data
if (size) {
httpBodyParameterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[size stringByAddingRFC3875PercentEscapesUsingEncoding:NSUTF8StringEncoding], [@"size" stringByAddingRFC3875PercentEscapesUsingEncoding:NSUTF8StringEncoding], nil];
httpBodyParameterData = [[STwitterOAuthTool generateHTTPBodyString:httpBodyParameterDict] dataUsingEncoding:NSUTF8StringEncoding];
[parameterDict setObject:size forKey:@"size"];
}

// Create Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:apiURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0f];

// Set HTTP Method to POST
[request setHTTPMethod:@"POST"];
STwitterRequest *request = [[STwitterRequest alloc] initWithURL:apiURL parameters:parameterDict requestMethod:STwitterRequestMethodPOST];

// Set HTTP Body
if (size)
[request setHTTPBody:httpBodyParameterData];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:[request signedURLRequest] delegate:self];
self.getUserProfileImageURLConnection = connection;

if (getUserProfileImageURLConnection)
{
while (getUserProfileImageURLConnection && [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);

if (getUserProfileImageResult) {
if (getUserProfileImageResponse) {
return [getUserProfileImageResponse URL];
}
else {
Expand All @@ -108,26 +77,10 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon
{
self.getUserProfileImageResponse = response;

if (connection == getUserProfileImageURLConnection)
if (connection == getUserProfileImageURLConnection) {
[self.getUserProfileImageURLConnection cancel];
self.getUserProfileImageURLConnection = nil;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[getUserProfileImageActiveDownload appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Error: %@", [error localizedDescription]);

self.getUserProfileImageConnection = nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
getUserProfileImageResult = [[NSData alloc] initWithData:getUserProfileImageActiveDownload];

self.getUserProfileImageConnection = nil;
}
}

@end

0 comments on commit 9df269e

Please sign in to comment.