Skip to content

Instantly share code, notes, and snippets.

@amiel
Last active December 15, 2015 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amiel/5294848 to your computer and use it in GitHub Desktop.
Save amiel/5294848 to your computer and use it in GitHub Desktop.
My RestKit mappings
// I have a RestKitManager.m that holds general methods to help set up mappings and routings
@implementation RestKitManager
- (void)initialize {
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:[AppDelegate appUrlForKey:BASE_URL_KEY]]];
[RKObjectManager setSharedManager:objectManager];
// NSLog(@"requestserilaziton mime type: %@", [objectManager requestSerializationMIMEType]);
// [objectManager setAcceptHeaderWithMIMEType:@"application/json"];
[self registerMappings];
[self setupDateFormatting];
[self setupRoutings];
}
- (void)addResponseMapping:(RKObjectMapping*)mapping forPathPattern:(NSString*)pathPattern {
RKObjectManager* objectManager = [RKObjectManager sharedManager];
RKResponseDescriptor *descriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:pathPattern keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:descriptor];
}
- (void)addRequestMapping:(RKObjectMapping*)mapping forClass:(Class)class rootKeyPath:(NSString*)keyPath {
RKObjectManager* objectManager = [RKObjectManager sharedManager];
RKRequestDescriptor* descriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping
objectClass:class
rootKeyPath:keyPath];
[objectManager addRequestDescriptor:descriptor];
};
- (void)setupDateFormatting {
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSTimeZone *localTime = [NSTimeZone systemTimeZone];
dateFormatter.timeZone = localTime;
[RKObjectMapping setPreferredDateFormatter:dateFormatter];
}
- (void)addRoute:(RKRoute*)route {
RKObjectManager* objectManager = [RKObjectManager sharedManager];
[objectManager.router.routeSet addRoute:route];
}
- (void)registerMappings {
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"errorMessage"]];
RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
[RKObjectManager.sharedManager addResponseDescriptor:errorDescriptor];
[self addResponseMapping:[Team requestMapping] forPathPattern:@"teams"];
[self addResponseMapping:[User responseMapping] forPathPattern:@"teams/:teamID/memberships"];
[self addResponseMapping:[Project requestMapping] forPathPattern:@"teams/:teamID/projects"];
[self addResponseMapping:[Project requestMapping] forPathPattern:@"teams/:teamID/projects/:projectID"];
[self addRequestMapping:[Project responseMapping] forClass:[Project class] rootKeyPath:@"project"];
// ... then a bunch more
}
- (void)setupRoutings {
[self addRoute:[RKRoute routeWithName:@"teams" pathPattern:@"teams" method:RKRequestMethodGET]];
[self addRoute:[RKRoute routeWithClass:[Project class] pathPattern:@"teams/:teamID/projects" method:RKRequestMethodPOST]];
[self addRoute:[RKRoute routeWithClass:[Project class] pathPattern:@"teams/:teamID/projects/:projectID" method:RKRequestMethodPUT]];
[self addRoute:[RKRoute routeWithRelationshipName:@"memberships" objectClass:[Team class] pathPattern:@"teams/:teamID/memberships" method:RKRequestMethodGET]];
[self addRoute:[RKRoute routeWithRelationshipName:@"projects" objectClass:[Team class] pathPattern:@"teams/:teamID/projects" method:RKRequestMethodGET]];
// ... then a bunch more
}
+ (RestKitManager *)sharedRestKit {
static dispatch_once_t pred;
static RestKitManager *foo = nil;
dispatch_once(&pred, ^{ foo = [[self alloc] init]; });
return foo;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment