Last active
December 15, 2015 04:39
-
-
Save gradha/5203572 to your computer and use it in GitHub Desktop.
Test example for https://github.com/RestKit/RestKit/issues/1286
This file contains 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
#import "test.h" | |
#import "RestKit/RestKit.h" | |
@interface Root : NSObject | |
@property (nonatomic, strong) NSArray *users; | |
@property (nonatomic, strong) NSString *text; | |
@end | |
@implementation Root | |
- (NSString*)description { | |
return [NSString stringWithFormat:@"Root {users:%@, test:%@}", | |
_users, _text]; | |
} | |
@end | |
@interface User : NSObject | |
@property (nonatomic, strong) NSString *name; | |
@end | |
@implementation User | |
- (NSString*)description { | |
return [NSString stringWithFormat:@"User {name:%@}", _name]; | |
} | |
@end | |
static RKObjectManager *manager; | |
@implementation Test | |
+ (void)test | |
{ | |
[Test setupMapper]; | |
[Test retrieveJSON]; | |
} | |
+ (void)setupMapper | |
{ | |
manager = [RKObjectManager managerWithBaseURL:[NSURL | |
URLWithString:@"http://dl.dropbox.com/u/145894/rk/"]]; | |
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] | |
forMIMEType:@"text/plain"]; | |
RKObjectMapping *rootMapping = [RKObjectMapping mappingForClass:[Root class]]; | |
[rootMapping addAttributeMappingsFromDictionary:@{@"extra": @"text"}]; | |
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[User class]]; | |
[userMapping addAttributeMappingsFromDictionary:@{@"name": @"name"}]; | |
[rootMapping addPropertyMapping:[RKRelationshipMapping | |
relationshipMappingFromKeyPath:@"users" toKeyPath:@"users" | |
withMapping:userMapping]]; | |
[manager addResponseDescriptor:[RKResponseDescriptor | |
responseDescriptorWithMapping:rootMapping | |
pathPattern:@"root" keyPath:nil | |
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]]; | |
} | |
+ (void)retrieveJSON | |
{ | |
RKObjectRequestOperation *operation = [manager | |
appropriateObjectRequestOperationWithObject:nil | |
method:RKRequestMethodGET path:@"root" parameters:nil]; | |
[operation setCompletionBlockWithSuccess: | |
^(RKObjectRequestOperation *operation, RKMappingResult *result) { | |
NSData *data = operation.HTTPRequestOperation.responseData; | |
NSLog(@"Got root %@", [result firstObject]); | |
[Test parseJson:data]; | |
} failure:nil]; | |
[manager enqueueObjectRequestOperation:operation]; | |
NSLog(@"Query requested..."); | |
} | |
+ (void)parseJson:(NSData*)input | |
{ | |
NSLog(@"Trying to reparse JSON..."); | |
NSError *error; | |
id parsedData = [RKMIMETypeSerialization objectFromData:input | |
MIMEType:@"application/json" error:&error]; | |
NSLog(@"Got %@", parsedData); | |
if ([parsedData count] < 1 || error) { | |
//deal with error | |
abort(); | |
} | |
RKObjectMapping *mapping = [manager.responseDescriptors[0] mapping]; | |
NSLog(@"Trying to use mapping %@", mapping); | |
NSDictionary *mappingsDictionary = @{ [NSNull null]: mapping }; | |
RKMapperOperation *mapper = [[RKMapperOperation alloc] | |
initWithRepresentation:parsedData | |
mappingsDictionary:mappingsDictionary]; | |
BOOL isMapped = [mapper execute:&error]; | |
if (isMapped && !error) { | |
NSLog(@"Yay, mapping got %@", [mapper.mappingResult firstObject]); | |
} else { | |
NSLog(@"Didn't get anything?"); | |
} | |
NSLog(@"Finished"); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment