Skip to content

Instantly share code, notes, and snippets.

@adamjernst
Created October 14, 2011 17:06
Show Gist options
  • Save adamjernst/1287681 to your computer and use it in GitHub Desktop.
Save adamjernst/1287681 to your computer and use it in GitHub Desktop.
spotsWithURLString:near:parameters:block: in AEURLConnection
+ (void)spotsWithURLString:(NSString *)urlString near:(CLLocation *)location parameters:(NSDictionary *)parameters block:(void (^)(NSArray *records))block {
NSDictionary *mutableParameters = [NSMutableDictionary dictionaryWithDictionary:parameters];
if (location) {
[mutableParameters setValue:[NSString stringWithFormat:@"%1.7f", location.coordinate.latitude] forKey:@"lat"];
[mutableParameters setValue:[NSString stringWithFormat:@"%1.7f", location.coordinate.longitude] forKey:@"lng"];
}
// AFGowallaAPIClient would manage an instance of AEURLRequestFactory set with authorization headers,
// and offer +baseURL with the API's base URL.
NSURLRequest *request = [[AFGowallaAPIClient requestFactory] requestWithURL:[[AFGowallaAPIClient baseURL] URLByAppendingPathComponent:urlString]
method:@"GET"
parameters:mutableParameters];
[AEURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
processor:[AEURLConnection chainedResponseProcessor:
[AEExpect contentType:[AEJSONProcessor defaultAcceptableJSONContentTypes]],
[AEExpect statusCode:[AEExpect defaultAcceptableStatusCodes]],
[AEJSONProcessor JSONResponseProcessor],
[AEExpect responseClass:[NSDictionary class]],
^(NSURLResponse *response, NSDictionary *object, NSError **error) {
NSMutableArray *mutableRecords = [NSMutableArray array];
for (NSDictionary *attributes in [object valueForKeyPath:@"spots"]) {
Spot *spot = [[[Spot alloc] initWithAttributes:attributes] autorelease];
[mutableRecords addObject:spot];
}
return [NSArray arrayWithArray:mutableRecords];
}, nil]
completionHandler:^(NSURLResponse *response, NSArray *records, NSError *error){
block(records);
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment