8
8
9
9
#import " JSONAPI.h"
10
10
11
+ #import " JSONAPIErrorResource.h"
12
+
11
13
@interface JSONAPI ()
12
14
13
15
@property (nonatomic , strong ) NSDictionary *dictionary;
@@ -16,28 +18,30 @@ @interface JSONAPI()
16
18
17
19
@implementation JSONAPI
18
20
19
- #pragma mark - Init
21
+ #pragma mark - Class
20
22
21
- + (id ) JSONAPIWithString : ( NSString *) string {
22
- return [[JSONAPI alloc ] initWithString: string ];
23
+ + (instancetype ) jsonAPIWithDictionary : ( NSDictionary *) dictionary {
24
+ return [[JSONAPI alloc ] initWithDictionary: dictionary ];
23
25
}
24
26
25
- + (id ) JSONAPIWithDictionary : ( NSDictionary *) dictionary {
26
- return [[JSONAPI alloc ] initWithDictionary: dictionary ];
27
+ + (instancetype ) jsonAPIWithString : ( NSString *) string {
28
+ return [[JSONAPI alloc ] initWithString: string ];
27
29
}
28
30
29
- - (id )initWithString : (NSString *)string {
31
+ #pragma mark - Instance
32
+
33
+ - (instancetype )initWithDictionary : (NSDictionary *)dictionary {
30
34
self = [super init ];
31
35
if (self) {
32
- [self inflateWithString: string ];
36
+ [self inflateWithDictionary: dictionary ];
33
37
}
34
38
return self;
35
39
}
36
40
37
- - (id ) initWithDictionary : ( NSDictionary *) dictionary {
41
+ - (instancetype ) initWithString : ( NSString *) string {
38
42
self = [super init ];
39
43
if (self) {
40
- [self inflateWithDictionary: dictionary ];
44
+ [self inflateWithString: string ];
41
45
}
42
46
return self;
43
47
}
@@ -48,53 +52,24 @@ - (void)inflateWithString:(NSString*)string {
48
52
if ([json isKindOfClass: [NSDictionary class ]] == YES ) {
49
53
[self inflateWithDictionary: json];
50
54
} else {
51
- _error = [NSError errorWithDomain: @" Could not parse JSON" code: 0 userInfo: nil ];
55
+ _internalError = [NSError errorWithDomain: @" Could not parse JSON" code: 0 userInfo: nil ];
52
56
}
53
57
}
54
58
55
59
#pragma mark - Resources
56
60
57
- - (id )objectForKey : ( NSString *) key {
58
- return [_dictionary objectForKey: key] ;
61
+ - (id )resource {
62
+ return _resources. firstObject ;
59
63
}
60
64
61
- - (id )resourceForKey : (NSString *)key {
62
- if ([key isEqualToString: @" meta" ] == YES || [key isEqualToString: @" linked" ] == YES ) {
63
- return nil ;
64
- }
65
-
66
- NSDictionary *rawResource = [_dictionary objectForKey: key];
67
- JSONAPIResource *resource = nil ;
68
- if ([rawResource isKindOfClass: [NSDictionary class ]] == YES ) {
69
- Class c = [JSONAPIResourceModeler resourceForLinkedType: [JSONAPIResourceLinker linkedType: key]];
70
- resource = [JSONAPIResource jsonAPIResource: rawResource withLinked: self .linked withClass: c];
71
- }
72
-
73
- // Fall back to first element in array
74
- if (resource == nil ) {
75
- id resources = [self resourcesForKey: key];
76
- if ([resources isKindOfClass: [NSArray class ]] == YES ) {
77
- return [resources firstObject ];
78
- }
79
- }
80
-
81
- return resource;
82
-
65
+ - (id )includedResource : (id )ID withType : (NSString *)type {
66
+ if (ID == nil ) return nil ;
67
+ if (type == nil ) return nil ;
68
+ return _includedResources[type][ID];
83
69
}
84
70
85
- - (NSArray *)resourcesForKey : (NSString *)key {
86
- if ([key isEqualToString: @" meta" ] == YES || [key isEqualToString: @" linked" ] == YES ) {
87
- return nil ;
88
- }
89
-
90
- NSArray *rawResources = [_dictionary objectForKey: key];
91
- NSArray *resources = nil ;
92
- if ([rawResources isKindOfClass: [NSArray class ]] == YES ) {
93
- Class c = [JSONAPIResourceModeler resourceForLinkedType: [JSONAPIResourceLinker linkedType: key]];
94
- resources = [JSONAPIResource jsonAPIResources: rawResources withLinked: self .linked withClass: c];
95
- }
96
-
97
- return resources;
71
+ - (BOOL )hasErrors {
72
+ return _errors.count > 0 ;
98
73
}
99
74
100
75
- (NSMutableArray *)arrayFromDictionary : (NSDictionary *)dictionary withKey : (NSString *)key
@@ -114,50 +89,77 @@ - (NSMutableArray *)arrayFromDictionary:(NSDictionary *)dictionary withKey:(NSSt
114
89
#pragma mark - Private
115
90
116
91
- (void )inflateWithDictionary : (NSDictionary *)dictionary {
117
- // Sets dictionary
92
+
93
+ // Sets internal dictionary
118
94
_dictionary = dictionary;
119
95
120
96
// Sets meta
121
- _meta = [ dictionary objectForKey: @" meta" ];
97
+ _meta = dictionary[ @" meta" ];
122
98
if ([_meta isKindOfClass: [NSDictionary class ]] == NO ) {
123
99
_meta = nil ;
124
100
}
125
101
126
- // Sets linked
127
- NSMutableDictionary *creatingLinked = [NSMutableDictionary dictionary ];
128
- NSDictionary *rawLinked = [dictionary objectForKey: @" linked" ];
129
- if ([rawLinked isKindOfClass: [NSDictionary class ]] == YES ) {
102
+ // Parse resources
103
+ _data = _dictionary[@" data" ];
104
+
105
+ NSMutableArray *resources = @[].mutableCopy ;
106
+ if ([_data isKindOfClass: [NSArray class ]] == YES ) {
107
+
108
+ NSArray *dataArray = (NSArray *) _data;
109
+ for (NSDictionary *data in dataArray) {
110
+ id resource = [self inflateResourceData: data];
111
+ if (resource) [resources addObject: resource];
112
+ }
130
113
131
- NSMutableArray *linkedToLinkWithLinked = [NSMutableArray array ];
114
+ } else if ([_data isKindOfClass: [NSDictionary class ]] == YES ) {
115
+ id resource = [self inflateResourceData: _data];
116
+ if (resource) [resources addObject: resource];
117
+ }
118
+ _resources = resources;
119
+
120
+ // Parses included resources
121
+ NSArray *included = _dictionary[@" included" ];
122
+ NSMutableDictionary *includedResources = @{}.mutableCopy ;
123
+ for (NSDictionary *data in included) {
132
124
133
- // Loops through linked arrays
134
- for (NSString *key in rawLinked.allKeys ) {
135
- NSArray *value = [self arrayFromDictionary: rawLinked withKey: key];
136
-
137
- if ([value isKindOfClass: [NSArray class ]] == YES ) {
138
- NSMutableDictionary *resources = [NSMutableDictionary dictionary ];
139
- for (NSDictionary *resourceDictionary in value) {
140
- Class c = [JSONAPIResourceModeler resourceForLinkedType: [JSONAPIResourceLinker linkedType: key]];
141
- JSONAPIResource *resource = [JSONAPIResource jsonAPIResource: resourceDictionary withLinked: nil withClass: c];
142
- if (resource.ID != nil ) {
143
- [resources setObject: resource forKey: resource.ID];
144
- [linkedToLinkWithLinked addObject: resource];
145
- }
146
- }
147
- [creatingLinked setObject: resources forKey: key];
148
-
149
- }
125
+ JSONAPIResource *resource = [self inflateResourceData: data];
126
+ if (resource) {
127
+
128
+ NSMutableDictionary *typeDict = includedResources[resource.type] ?: @{}.mutableCopy ;
129
+ typeDict[resource.ID] = resource;
150
130
131
+ includedResources[resource.type] = typeDict;
151
132
}
152
-
153
- // Linked the linked
154
- for (JSONAPIResource *resource in linkedToLinkWithLinked) {
155
- [resource linkLinks: creatingLinked];
133
+ }
134
+ _includedResources = includedResources;
135
+
136
+ // Link included with included
137
+ // TODO: Need to look into / stop circular references
138
+ for (NSDictionary *typeIncluded in _includedResources.allValues ) {
139
+ for (JSONAPIResource *resource in typeIncluded.allValues ) {
140
+ [resource linkWithIncluded: self ];
156
141
}
157
-
158
142
}
159
143
160
- _linked = creatingLinked;
144
+ // Link data with included
145
+ for (JSONAPIResource *resource in _resources) {
146
+ [resource linkWithIncluded: self ];
147
+ }
148
+
149
+ // Parse errors
150
+ NSMutableArray *errors = @[].mutableCopy ;
151
+ NSLog (@" ERROS - %@ " , _dictionary[@" errors" ]);
152
+ for (NSDictionary *data in _dictionary[@" errors" ]) {
153
+
154
+ JSONAPIErrorResource *resource = [[JSONAPIErrorResource alloc ] initWithDictionary: data];
155
+ NSLog (@" Error resource - %@ " , resource);
156
+ if (resource) [errors addObject: resource];
157
+ }
158
+ _errors = errors;
159
+ }
160
+
161
+ - (id )inflateResourceData : (NSDictionary *)data {
162
+ return [JSONAPIResource jsonAPIResource: data];
161
163
}
162
164
163
165
@end
0 commit comments