Skip to content

Commit

Permalink
Blockify UAGithubEngine.m
Browse files Browse the repository at this point in the history
- Engine methods now take an extra parameter: successBlock. This block takes an id and returns an id.
- -sendRequest:… methods now return an id instead of an NSString. This id is the JSON returned from the API, parsed for ObjC consumption.
- All calls to -sendRequest:… are wrapped in a call to successBlock().
- When calling engine methods, we populate the successBlock parameter with what we want to be executed on successful completion of the call.
- All the above is in place of using delegation. May (should?) eventually lead to the fiery death of UAGithubEngineDelegate.
  • Loading branch information
Owain R Hunt committed Oct 31, 2011
1 parent ab44358 commit bd9b849
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 183 deletions.
2 changes: 1 addition & 1 deletion UAGithubEngine.h
Expand Up @@ -83,7 +83,7 @@

#pragma mark Comments

- (NSString *)commentsForIssue:(NSInteger)issueNumber forRepository:(NSString *)repositoryPath;
- (id)commentsForIssue:(NSInteger)issueNumber forRepository:(NSString *)repositoryPath success:(id(^)(id obj))successBlock_;
- (NSString *)issueComment:(NSInteger)commentNumber forRepository:(NSString *)repositoryPath;
- (NSString *)addComment:(NSString *)comment toIssue:(NSInteger)issueNumber forRepository:(NSString *)repositoryPath;
- (NSString *)editComment:(NSInteger)commentNumber forRepository:(NSString *)repositoryPath withBody:(NSString *)commentBody;
Expand Down
198 changes: 16 additions & 182 deletions UAGithubEngine.m
Expand Up @@ -110,7 +110,7 @@ - (UAReachability *)reachability
#pragma mark Request Management
#pragma mark

- (NSString *)sendRequest:(NSString *)path requestType:(UAGithubRequestType)requestType responseType:(UAGithubResponseType)responseType withParameters:(id)params page:(NSInteger)page
- (id)sendRequest:(NSString *)path requestType:(UAGithubRequestType)requestType responseType:(UAGithubResponseType)responseType withParameters:(id)params page:(NSInteger)page
{

NSMutableString *urlString = [NSMutableString stringWithFormat:@"%@%@/%@", API_PROTOCOL, API_DOMAIN, path];
Expand Down Expand Up @@ -249,6 +249,7 @@ - (NSString *)sendRequest:(NSString *)path requestType:(UAGithubRequestType)requ
}

__block NSString *uuid = [[NSString stringWithNewUUID] retain];
__block id jsonObj = nil;

[UAGithubURLConnection asyncRequest:urlRequest requestType:requestType responseType:responseType
success:^(NSData *data, NSURLResponse *response)
Expand Down Expand Up @@ -288,199 +289,32 @@ - (NSString *)sendRequest:(NSString *)path requestType:(UAGithubRequestType)requ
[delegate noContentResponseReceivedForConnection:nil];
}

[UAGithubJSONParser parseJSON:data delegate:self connectionIdentifier:uuid requestType:requestType responseType:responseType
success:^(id parsedObjects)
{
[delegate requestSucceeded:uuid];

switch (responseType) {
case UAGithubRepositoriesResponse:
case UAGithubRepositoryResponse:
[delegate repositoriesReceived:parsedObjects forConnection:uuid];
break;

case UAGithubRepositoryTeamsResponse:
break;

case UAGithubMilestonesResponse:
case UAGithubMilestoneResponse:
[delegate milestonesReceived:parsedObjects forConnection:uuid];
break;

case UAGithubIssuesResponse:
case UAGithubIssueResponse:
[delegate issuesReceived:parsedObjects forConnection:uuid];
break;

case UAGithubIssueCommentsResponse:
case UAGithubIssueCommentResponse:
[delegate issueCommentsReceived:parsedObjects forConnection:uuid];
break;

case UAGithubUsersResponse:
case UAGithubUserResponse:
case UAGithubCollaboratorsResponse:
[delegate usersReceived:parsedObjects forConnection:uuid];
break;

case UAGithubIssueLabelsResponse:
case UAGithubIssueLabelResponse:
case UAGithubRepositoryLabelsResponse:
case UAGithubRepositoryLabelResponse:
[delegate labelsReceived:parsedObjects forConnection:uuid];
break;

case UAGithubCommitsResponse:
case UAGithubCommitResponse:
case UAGithubPullRequestCommitsResponse:
[delegate commitsReceived:parsedObjects forConnection:uuid];
break;

case UAGithubCommitCommentsResponse:
case UAGithubCommitCommentResponse:
[delegate commitCommentsReceived:parsedObjects forConnection:uuid];
break;

#pragma mark TODO Two separate methods?
case UAGithubBlobsResponse:
[delegate blobsReceieved:parsedObjects forConnection:uuid];
break;

case UAGithubBlobResponse:
[delegate blobReceived:parsedObjects forConnection:uuid];
break;

case UAGithubRepositoryLanguageBreakdownResponse:
[delegate languagesReceived:parsedObjects forConnection:uuid];
break;

#pragma mark TODO What's the deal with tags?
case UAGithubTagsResponse:
[delegate tagsReceived:parsedObjects forConnection:uuid];
break;

case UAGithubBranchesResponse:
[delegate branchesReceived:parsedObjects forConnection:uuid];
break;

#pragma mark TODO Does this belong here?
case UAGithubTreeResponse:
[delegate treeReceived:parsedObjects forConnection:uuid];
break;

case UAGithubFollowingResponse:
[delegate followingReceived:parsedObjects forConnection:uuid];
break;

case UAGithubFollowersResponse:
[delegate followersReceived:parsedObjects forConnection:uuid];
break;

case UAGithubFollowedResponse:
// Returns 204 no content.
break;

case UAGithubUnfollowedResponse:
// Returns 204 no content.
break;

case UAGithubDeployKeysResponse:
case UAGithubDeployKeyResponse:
[delegate deployKeysReceived:parsedObjects forConnection:uuid];
break;

case UAGithubRepositoryHooksResponse:
case UAGithubRepositoryHookResponse:
[delegate repositoryHooksReceived:parsedObjects forConnection:uuid];
break;

case UAGithubPublicKeysResponse:
case UAGithubPublicKeyResponse:
[delegate publicKeysReceived:parsedObjects forConnection:uuid];
break;

case UAGithubGistsResponse:
case UAGithubGistResponse:
[delegate gistsReceived:parsedObjects forConnection:uuid];
break;

case UAGithubGistCommentsResponse:
case UAGithubGistCommentResponse:
[delegate gistCommentsReceived:parsedObjects forConnection:uuid];
break;

case UAGithubIssueEventsResponse:
case UAGithubIssueEventResponse:
[delegate issueEventsReceived:parsedObjects forConnection:uuid];
break;

case UAGithubPullRequestsResponse:
case UAGithubPullRequestResponse:
[delegate pullRequestsReceived:parsedObjects forConnection:uuid];
break;

case UAGithubPullRequestMergeSuccessStatusResponse:
[delegate pullRequestMergeSuccessStatusReceived:parsedObjects forConnection:uuid];
break;

case UAGithubPullRequestFilesResponse:
[delegate pullRequestFilesReceived:parsedObjects forConnection:uuid];
break;

case UAGithubPullRequestCommentsResponse:
case UAGithubPullRequestCommentResponse:
[delegate pullRequestCommentsReceived:parsedObjects forConnection:uuid];
break;

case UAGithubSHAResponse:
[delegate SHAReceived:parsedObjects forConnection:uuid];
break;

case UAGithubReferencesResponse:
case UAGithubReferenceResponse:
[delegate referencesReceived:parsedObjects forConnection:uuid];
break;

case UAGithubAnnotatedTagResponse:
[delegate annotatedTagsReceived:parsedObjects forConnection:uuid];
break;

case UAGithubRawCommitResponse:
[delegate rawCommitReceived:parsedObjects forConnection:uuid];
break;

default:
break;
}
}
failure:^(id parsedObjects, NSError *error)
{
[delegate requestFailed:uuid withError:error];
}
];
jsonObj = [UAGithubJSONParser parseJSON:data delegate:self connectionIdentifier:uuid requestType:requestType responseType:responseType];
}
failure:^(NSData *data, NSError *error){
[delegate requestFailed:uuid withError:error];
[delegate connectionFinished:uuid];
failure:^(NSData *data, NSError *parserError)
{
[delegate requestFailed:uuid withError:parserError];
[delegate connectionFinished:uuid];
}
];
return uuid;

return jsonObj;
}


- (NSString *)sendRequest:(NSString *)path requestType:(UAGithubRequestType)requestType responseType:(UAGithubResponseType)responseType withParameters:(id)params
- (id)sendRequest:(NSString *)path requestType:(UAGithubRequestType)requestType responseType:(UAGithubResponseType)responseType withParameters:(id)params
{
return [self sendRequest:path requestType:requestType responseType:responseType withParameters:params page:0];
return [self sendRequest:path requestType:requestType responseType:responseType withParameters:params page:04];
}


- (NSString *)sendRequest:(NSString *)path requestType:(UAGithubRequestType)requestType responseType:(UAGithubResponseType)responseType page:(NSInteger)page
- (id)sendRequest:(NSString *)path requestType:(UAGithubRequestType)requestType responseType:(UAGithubResponseType)responseType page:(NSInteger)page
{
return [self sendRequest:path requestType:requestType responseType:responseType withParameters:nil page:page];
}


- (NSString *)sendRequest:(NSString *)path requestType:(UAGithubRequestType)requestType responseType:(UAGithubResponseType)responseType
- (id)sendRequest:(NSString *)path requestType:(UAGithubRequestType)requestType responseType:(UAGithubResponseType)responseType
{
return [self sendRequest:path requestType:requestType responseType:responseType withParameters:nil page:0];
}
Expand Down Expand Up @@ -656,9 +490,9 @@ - (NSString *)deleteIssue:(NSInteger)issueNumber inRepository:(NSString *)reposi

#pragma mark Comments

- (NSString *)commentsForIssue:(NSInteger)issueNumber forRepository:(NSString *)repositoryPath
- (id)commentsForIssue:(NSInteger)issueNumber forRepository:(NSString *)repositoryPath success:(id(^)(id obj))successBlock_;
{
return [self sendRequest:[NSString stringWithFormat:@"repos/%@/issues/%d/comments", repositoryPath, issueNumber] requestType:UAGithubIssueCommentsRequest responseType:UAGithubIssueCommentsResponse];
return successBlock_([self sendRequest:[NSString stringWithFormat:@"repos/%@/issues/%d/comments", repositoryPath, issueNumber] requestType:UAGithubIssueCommentsRequest responseType:UAGithubIssueCommentsResponse]);
}


Expand All @@ -679,7 +513,7 @@ - (NSString *)addComment:(NSString *)comment toIssue:(NSInteger)issueNumber forR
- (NSString *)editComment:(NSInteger)commentNumber forRepository:(NSString *)repositoryPath withBody:(NSString *)commentBody
{
NSDictionary *commentDictionary = [NSDictionary dictionaryWithObject:commentBody forKey:@"body"];
return [self sendRequest:[NSString stringWithFormat:@"repos/:user/:repo/issues/comments/:id", repositoryPath, commentNumber] requestType:UAGithubIssueCommentEditRequest responseType:UAGithubIssueCommentResponse withParameters:commentDictionary];
return [self sendRequest:[NSString stringWithFormat:@"repos/%@/issues/comments/%d", repositoryPath, commentNumber] requestType:UAGithubIssueCommentEditRequest responseType:UAGithubIssueCommentResponse withParameters:commentDictionary];
}


Expand Down

0 comments on commit bd9b849

Please sign in to comment.