-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMCPaginationRestKit.m
63 lines (49 loc) · 2.17 KB
/
MCPaginationRestKit.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// MCPaginationRestKit.m
// Manticore iOSInfiniteScroll
//
// Created by Richard Fung on 3/11/13.
// Copyright (c) 2013 Yeti LLC. All rights reserved.
//
#import "MCPaginationHelper.h"
#import "MCPaginationRestKit.h"
#import <RestKit/RestKit.h>
@implementation MCPaginationHelper(RestKit)
+(void)setupMapping:(RKObjectManager*)manager {
NSIndexSet *successCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
NSIndexSet *failCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError);
NSIndexSet *serverFailCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassServerError);
NSIndexSet *redirectCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassRedirection);
// RestKit object mappings
RKObjectMapping* MCMetaResponseMapping = [RKObjectMapping mappingForClass:[MCMeta class]];
[MCMetaResponseMapping addAttributeMappingsFromDictionary:@{
@"next":@"next",
@"total_count":@"total_count",
@"offset":@"offset",
@"limit":@"limit",
@"previous":@"previous"}];
// Responses applied to any URL
RKResponseDescriptor* MCMeta_Response_n = [RKResponseDescriptor responseDescriptorWithMapping:MCMetaResponseMapping pathPattern:nil keyPath:@"meta" statusCodes:successCodes];
// Configure RestKit to handle requests and responses
[manager addResponseDescriptorsFromArray:@[MCMeta_Response_n]];
}
// use this method to filter out the Meta object before passing values back to the caller
+(id)firstObjectWithoutMetaBlock:(NSArray*)restKitArray {
if (restKitArray.count == 0)
return nil;
else{
if ([[restKitArray objectAtIndex:0] isKindOfClass:[MCMeta class]]){
if (restKitArray.count > 1)
return [restKitArray objectAtIndex:1];
else
return nil;
}
else{
return [restKitArray objectAtIndex:0];
}
}
}
+(id)firstObjectFromRestKit:(RKMappingResult*)result {
return [MCPaginationHelper firstObjectWithoutMetaBlock:result.array];
}
@end