Repository navigation
Expand file tree
/
Copy pathCDAArray.m
More file actions
106 lines (85 loc) · 3 KB
/
Copy pathCDAArray.m
File metadata and controls
106 lines (85 loc) · 3 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// CDAArray.m
// ContentfulSDK
//
// Created by Boris Bügling on 04/03/14.
//
//
#import "CDAArray+Private.h"
#import "CDAError+Private.h"
#import "CDAResource+Private.h"
#import "CDAUtilities.h"
@interface CDAArray ()
@property (nonatomic) NSArray* errors;
@property (nonatomic) NSArray* items;
@property (nonatomic) NSUInteger limit;
@property (nonatomic) NSString* nextPageUrlString;
@property (nonatomic) NSString* nextSyncUrlString;
@property (nonatomic) NSUInteger skip;
@property (nonatomic) NSUInteger total;
@end
#pragma mark -
@implementation CDAArray
+(NSString *)CDAType {
return @"Array";
}
#pragma mark -
-(NSString *)description {
return self.items.description;
}
-(id)initWithDictionary:(NSDictionary*)dictionary client:(CDAClient*)client {
self = [super initWithDictionary:dictionary client:client];
if (self) {
self.limit = [dictionary[@"limit"] unsignedIntegerValue];
self.skip = [dictionary[@"skip"] unsignedIntegerValue];
self.total = [dictionary[@"total"] unsignedIntegerValue];
self.nextPageUrlString = dictionary[@"nextPageUrl"];
self.nextSyncUrlString = dictionary[@"nextSyncUrl"];
NSMutableArray* items = [@[] mutableCopy];
for (NSDictionary* item in dictionary[@"items"]) {
CDAResource* resource = [CDAResource resourceObjectForDictionary:item client:self.client];
[items addObject:resource];
}
self.items = [items copy];
NSMutableArray* errors = [@[] mutableCopy];
for (NSDictionary* item in dictionary[@"errors"]) {
CDAError* error = (CDAError*)[CDAResource resourceObjectForDictionary:item
client:self.client];
NSAssert(CDAClassIsOfType([error class], CDAError.class),
@"Invalid resource %@ in errors array.", error);
[errors addObject:[error errorRepresentationWithCode:0]];
}
self.errors = [errors copy];
}
return self;
}
-(id)initWithItems:(NSArray *)items client:(CDAClient *)client {
self = [self initWithDictionary:@{ @"sys": @{} } client:client];
if (self) {
self.limit = items.count;
self.skip = 0;
self.total = items.count;
self.items = items;
}
return self;
}
-(NSURL *)nextPageUrl {
return self.nextPageUrlString ? [NSURL URLWithString:self.nextPageUrlString] : nil;
}
-(NSURL *)nextSyncUrl {
return self.nextSyncUrlString ? [NSURL URLWithString:self.nextSyncUrlString] : nil;
}
-(void)resolveLinksWithIncludedAssets:(NSDictionary*)assets entries:(NSDictionary*)entries {
for (CDAResource* item in self.items) {
[item resolveLinksWithIncludedAssets:assets entries:entries];
}
}
-(void)setClient:(CDAClient *)client {
[super setClient:client];
for (id item in self.items) {
if ([item respondsToSelector:@selector(setClient:)]) {
[(CDAResource*)item setClient:self.client];
}
}
}
@end