9
9
#import " JSONAPI.h"
10
10
11
11
#import " JSONAPIErrorResource.h"
12
+ #import " JSONAPIResourceParser.h"
13
+ #import " JSONAPIResourceDescriptor.h"
14
+
15
+
16
+ static NSString *gMEDIA_TYPE = @" application/vnd.api+json" ;
12
17
13
18
@interface JSONAPI ()
14
19
@@ -20,6 +25,10 @@ @implementation JSONAPI
20
25
21
26
#pragma mark - Class
22
27
28
+ + (NSString *)MEDIA_TYPE {
29
+ return gMEDIA_TYPE ;
30
+ }
31
+
23
32
+ (instancetype )jsonAPIWithDictionary : (NSDictionary *)dictionary {
24
33
return [[JSONAPI alloc ] initWithDictionary: dictionary];
25
34
}
@@ -28,8 +37,16 @@ + (instancetype)jsonAPIWithString:(NSString *)string {
28
37
return [[JSONAPI alloc ] initWithString: string];
29
38
}
30
39
40
+ + (instancetype )jsonAPIWithResource : (NSObject <JSONAPIResource> *)resource {
41
+ return [[JSONAPI alloc ] initWithResource: resource];
42
+ }
43
+
31
44
#pragma mark - Instance
32
45
46
+ - (NSDictionary *)meta {
47
+ return self.dictionary [@" meta" ];
48
+ }
49
+
33
50
- (instancetype )initWithDictionary : (NSDictionary *)dictionary {
34
51
self = [super init ];
35
52
if (self) {
@@ -46,6 +63,14 @@ - (instancetype)initWithString:(NSString*)string {
46
63
return self;
47
64
}
48
65
66
+ -(instancetype )initWithResource : (NSObject <JSONAPIResource> *)resource {
67
+ self = [super init ];
68
+ if (self) {
69
+ [self inflateWithResource: resource];
70
+ }
71
+ return self;
72
+ }
73
+
49
74
- (void )inflateWithString : (NSString *)string {
50
75
id json = [NSJSONSerialization JSONObjectWithData: [string dataUsingEncoding: NSUTF8StringEncoding] options: 0 error: nil ];
51
76
@@ -79,72 +104,99 @@ - (void)inflateWithDictionary:(NSDictionary*)dictionary {
79
104
// Sets internal dictionary
80
105
_dictionary = dictionary;
81
106
82
- // Sets meta
83
- _meta = dictionary[@" meta" ];
84
- if ([_meta isKindOfClass: [NSDictionary class ]] == NO ) {
85
- _meta = nil ;
86
- }
87
-
88
107
// Parse resources
89
- _data = _dictionary[@" data" ];
90
-
91
- NSMutableArray *resources = @[].mutableCopy ;
92
- if ([_data isKindOfClass: [NSArray class ]] == YES ) {
108
+ id data = dictionary[@" data" ];
109
+ if ([data isKindOfClass: [NSArray class ]] == YES ) {
110
+ _resources = [JSONAPIResourceParser parseResources: data];
93
111
94
- NSArray *dataArray = (NSArray *) _data;
95
- for (NSDictionary *data in dataArray) {
96
- id resource = [self inflateResourceData: data];
97
- if (resource) [resources addObject: resource];
98
- }
99
-
100
- } else if ([_data isKindOfClass: [NSDictionary class ]] == YES ) {
101
- id resource = [self inflateResourceData: _data];
102
- if (resource) [resources addObject: resource];
112
+ } else if ([data isKindOfClass: [NSDictionary class ]] == YES ) {
113
+ id resource = [JSONAPIResourceParser parseResource: data];
114
+ _resources = [[NSArray alloc ] initWithObjects: resource, nil ];
103
115
}
104
- _resources = resources;
105
116
106
117
// Parses included resources
107
- NSArray *included = _dictionary[@" included" ];
108
- NSMutableDictionary *includedResources = @{}.mutableCopy ;
109
- for (NSDictionary *data in included) {
110
-
111
- JSONAPIResource *resource = [self inflateResourceData: data];
118
+ id included = dictionary[@" included" ];
119
+ NSMutableDictionary *includedResources = [[NSMutableDictionary alloc ] init ];
120
+ if ([included isKindOfClass: [NSArray class ]] == YES ) {
121
+ for (NSDictionary *data in included) {
122
+
123
+ NSObject <JSONAPIResource> *resource = [JSONAPIResourceParser parseResource: data];
124
+ if (resource) {
125
+ JSONAPIResourceDescriptor *desc = [JSONAPIResourceDescriptor forLinkedType: data[@" type" ]];
126
+
127
+ NSMutableDictionary *typeDict = includedResources[desc.type] ?: @{}.mutableCopy ;
128
+ typeDict[resource.ID] = resource;
129
+
130
+ includedResources[desc.type] = typeDict;
131
+ }
132
+ }
133
+ } else if ([included isKindOfClass: [NSDictionary class ]] == YES ) {
134
+ NSObject <JSONAPIResource> *resource = [JSONAPIResourceParser parseResource: included];
112
135
if (resource) {
113
-
114
- NSMutableDictionary *typeDict = includedResources[resource.type] ?: @{}.mutableCopy ;
136
+ JSONAPIResourceDescriptor *desc = [JSONAPIResourceDescriptor forLinkedType: data[@" type" ]];
137
+
138
+ NSMutableDictionary *typeDict = includedResources[desc.type] ?: @{}.mutableCopy ;
115
139
typeDict[resource.ID] = resource;
116
140
117
- includedResources[resource .type] = typeDict;
141
+ includedResources[desc .type] = typeDict;
118
142
}
119
143
}
120
144
_includedResources = includedResources;
121
145
122
146
// Link included with included
123
147
for (NSDictionary *typeIncluded in _includedResources.allValues ) {
124
- for (JSONAPIResource *resource in typeIncluded.allValues ) {
125
- [resource linkWithIncluded :self ];
148
+ for (NSObject < JSONAPIResource> *resource in typeIncluded.allValues ) {
149
+ [JSONAPIResourceParser link: resource withIncluded :self ];
126
150
}
127
151
}
128
152
129
153
// Link data with included
130
- for (JSONAPIResource *resource in _resources) {
131
- [resource linkWithIncluded :self ];
154
+ for (NSObject < JSONAPIResource> *resource in _resources) {
155
+ [JSONAPIResourceParser link: resource withIncluded :self ];
132
156
}
133
-
157
+
134
158
// Parse errors
135
- NSMutableArray *errors = @[].mutableCopy ;
136
- NSLog (@" ERROS - %@ " , _dictionary[@" errors" ]);
137
- for (NSDictionary *data in _dictionary[@" errors" ]) {
138
-
139
- JSONAPIErrorResource *resource = [[JSONAPIErrorResource alloc ] initWithDictionary: data];
140
- NSLog (@" Error resource - %@ " , resource);
141
- if (resource) [errors addObject: resource];
159
+ if (dictionary[@" errors" ]) {
160
+ NSMutableArray *errors = [[NSMutableArray alloc ] init ];
161
+ NSLog (@" ERRORS - %@ " , dictionary[@" errors" ]);
162
+ for (NSDictionary *data in dictionary[@" errors" ]) {
163
+
164
+ JSONAPIErrorResource *resource = [[JSONAPIErrorResource alloc ] initWithDictionary: data];
165
+ NSLog (@" Error resource - %@ " , resource);
166
+ if (resource) [errors addObject: resource];
167
+ }
168
+ _errors = errors;
142
169
}
143
- _errors = errors;
144
170
}
145
171
146
- - (id )inflateResourceData : (NSDictionary *)data {
147
- return [JSONAPIResource jsonAPIResource: data];
172
+ - (void )inflateWithResource : (NSObject <JSONAPIResource> *)resource
173
+ {
174
+ NSMutableArray *resourceArray = [[NSMutableArray alloc ] init ];
175
+ [resourceArray addObject: resource];
176
+ _resources = resourceArray;
177
+
178
+ NSMutableDictionary *newDictionary = [[NSMutableDictionary alloc ] init ];
179
+
180
+ newDictionary[@" data" ] = [JSONAPIResourceParser dictionaryFor: resource];
181
+
182
+ NSArray *included = [JSONAPIResourceParser relatedResourcesFor: resource];
183
+ if (included.count ) {
184
+ newDictionary[@" included" ] = included;
185
+
186
+ NSMutableDictionary *includedResources = [[NSMutableDictionary alloc ] init ];
187
+ for (NSObject <JSONAPIResource> *linked in included) {
188
+
189
+ JSONAPIResourceDescriptor *desc = [[linked class ] descriptor ];
190
+
191
+ NSMutableDictionary *typeDict = includedResources[desc.type] ?: @{}.mutableCopy ;
192
+ typeDict[linked.ID] = resource;
193
+
194
+ includedResources[desc.type] = typeDict;
195
+ }
196
+ _includedResources = includedResources;
197
+ }
198
+
199
+ _dictionary = newDictionary;
148
200
}
149
201
150
202
@end
0 commit comments