Created
June 19, 2012 12:58
-
-
Save Dimillian/2954027 to your computer and use it in GitHub Desktop.
BLOCK BLOCK BLOCK
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
//Recreate a partial route from server response, usefull in case of crash or other | |
+(void)recreateRouteFromCurrentServerRoute:(void (^)(WZRoute *))handler | |
{ | |
//Ask the server if the current user have a route to take action for | |
DMRESTRequest *request = [[DMRESTRequest alloc]initWithMethod:@"GET" ressource:@"routes/current" parameters:nil shouldEscapeParameters:NO]; | |
[request executeBlockRequest:^(NSURLResponse *response, NSData *data, NSError *error){ | |
if (data) { | |
NSJSONSerialization *json = [NSJSONSerialization JSONObjectWithData:data | |
options:NSJSONReadingMutableLeaves | |
error:nil]; | |
if (json) { | |
//If we get something then we instantiate a new route | |
NSDictionary *jsonDic = (NSDictionary *)json; | |
// yeah yeah, init the model within the model is ugly, will be changed soon | |
WZRoute *route = [[WZRoute alloc]init]; | |
route.passengers = [[NSMutableArray alloc]init]; | |
[route endRouteWithInformation:jsonDic]; | |
route.status = [[jsonDic objectForKey:@"status"]intValue]; | |
//Now we ask for the route driver detail | |
NSNumber *driverId = [NSNumber numberWithInt:[[jsonDic objectForKey:@"driver_id"]intValue]]; | |
DMRESTRequest *driverRequest = [[DMRESTRequest alloc]initWithMethod:@"GET" | |
ressource:@"users/show" | |
parameters:[NSDictionary dictionaryWithObject:driverId forKey:@"user_id"] shouldEscapeParameters:YES]; | |
//Get the driver detail | |
[driverRequest executeBlockRequest:^(NSURLResponse *response, NSData *data, NSError *error){ | |
if (data) { | |
NSJSONSerialization *driverJson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; | |
//Instanciate driver of the route | |
WZUser *driver = [[WZUser alloc]initWithJSONDic:(NSDictionary *)driverJson]; | |
//Set the route owner to the new driver | |
[route setDriver:driver]; | |
NSNumber *passengerId = [NSNumber numberWithInt:[[jsonDic objectForKey:@"passenger_id"]intValue]]; | |
DMRESTRequest *passengerRequest = [[DMRESTRequest alloc]initWithMethod:@"GET" | |
ressource:@"users/show" | |
parameters:[NSDictionary dictionaryWithObject:passengerId forKey:@"user_id"] shouldEscapeParameters:YES]; | |
//Now we ask for route passengers and add them to the array | |
[passengerRequest executeBlockRequest:^(NSURLResponse *response, NSData *data, NSError *error){ | |
if (data) { | |
NSJSONSerialization *passengerJson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; | |
WZUser *passenger = [[WZUser alloc]initWithJSONDic:(NSDictionary *)passengerJson]; | |
[route addPassenger:passenger]; | |
handler(route); | |
} | |
}]; | |
} | |
}]; | |
} | |
else { | |
handler(nil); | |
} | |
} | |
else { | |
handler(nil); | |
} | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment