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,102 +52,100 @@ - (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
#pragma mark - Private
101
76
102
77
- (void )inflateWithDictionary : (NSDictionary *)dictionary {
103
- // Sets dictionary
78
+
79
+ // Sets internal dictionary
104
80
_dictionary = dictionary;
105
81
106
82
// Sets meta
107
- _meta = [ dictionary objectForKey: @" meta" ];
83
+ _meta = dictionary[ @" meta" ];
108
84
if ([_meta isKindOfClass: [NSDictionary class ]] == NO ) {
109
85
_meta = nil ;
110
86
}
111
87
112
- // Sets linked
113
- NSMutableDictionary *creatingLinked = [NSMutableDictionary dictionary ];
114
- NSDictionary *rawLinked = [dictionary objectForKey: @" linked" ];
115
- if ([rawLinked isKindOfClass: [NSDictionary class ]] == YES ) {
88
+ // Parse resources
89
+ _data = _dictionary[@" data" ];
90
+
91
+ NSMutableArray *resources = @[].mutableCopy ;
92
+ if ([_data isKindOfClass: [NSArray class ]] == YES ) {
93
+
94
+ NSArray *dataArray = (NSArray *) _data;
95
+ for (NSDictionary *data in dataArray) {
96
+ id resource = [self inflateResourceData: data];
97
+ if (resource) [resources addObject: resource];
98
+ }
116
99
117
- NSMutableArray *linkedToLinkWithLinked = [NSMutableArray array ];
100
+ } else if ([_data isKindOfClass: [NSDictionary class ]] == YES ) {
101
+ id resource = [self inflateResourceData: _data];
102
+ if (resource) [resources addObject: resource];
103
+ }
104
+ _resources = resources;
105
+
106
+ // Parses included resources
107
+ NSArray *included = _dictionary[@" included" ];
108
+ NSMutableDictionary *includedResources = @{}.mutableCopy ;
109
+ for (NSDictionary *data in included) {
118
110
119
- // Loops through linked arrays
120
- for (NSString *key in rawLinked.allKeys ) {
121
- NSArray *value = [rawLinked objectForKey: key];
122
-
123
- if ([value isKindOfClass: [NSArray class ]] == YES ) {
124
- NSMutableDictionary *resources = [NSMutableDictionary dictionary ];
125
- for (NSDictionary *resourceDictionary in value) {
126
- Class c = [JSONAPIResourceModeler resourceForLinkedType: [JSONAPIResourceLinker linkedType: key]];
127
- JSONAPIResource *resource = [JSONAPIResource jsonAPIResource: resourceDictionary withLinked: nil withClass: c];
128
- if (resource.ID != nil ) {
129
- [resources setObject: resource forKey: resource.ID];
130
- [linkedToLinkWithLinked addObject: resource];
131
- }
132
- }
133
- [creatingLinked setObject: resources forKey: key];
134
-
135
- }
111
+ JSONAPIResource *resource = [self inflateResourceData: data];
112
+ if (resource) {
113
+
114
+ NSMutableDictionary *typeDict = includedResources[resource.type] ?: @{}.mutableCopy ;
115
+ typeDict[resource.ID] = resource;
136
116
117
+ includedResources[resource.type] = typeDict;
137
118
}
138
-
139
- // Linked the linked
140
- for (JSONAPIResource *resource in linkedToLinkWithLinked) {
141
- [resource linkLinks: creatingLinked];
119
+ }
120
+ _includedResources = includedResources;
121
+
122
+ // Link included with included
123
+ // TODO: Need to look into / stop circular references
124
+ for (NSDictionary *typeIncluded in _includedResources.allValues ) {
125
+ for (JSONAPIResource *resource in typeIncluded.allValues ) {
126
+ [resource linkWithIncluded: self ];
142
127
}
143
-
144
128
}
145
129
146
- _linked = creatingLinked;
130
+ // Link data with included
131
+ for (JSONAPIResource *resource in _resources) {
132
+ [resource linkWithIncluded: self ];
133
+ }
134
+
135
+ // Parse errors
136
+ NSMutableArray *errors = @[].mutableCopy ;
137
+ NSLog (@" ERROS - %@ " , _dictionary[@" errors" ]);
138
+ for (NSDictionary *data in _dictionary[@" errors" ]) {
139
+
140
+ JSONAPIErrorResource *resource = [[JSONAPIErrorResource alloc ] initWithDictionary: data];
141
+ NSLog (@" Error resource - %@ " , resource);
142
+ if (resource) [errors addObject: resource];
143
+ }
144
+ _errors = errors;
145
+ }
146
+
147
+ - (id )inflateResourceData : (NSDictionary *)data {
148
+ return [JSONAPIResource jsonAPIResource: data];
147
149
}
148
150
149
151
@end
0 commit comments