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