Skip to content

Commit 6c267a3

Browse files
author
Josh Holtz
committed
Parses meta and data
1 parent 0bf15cc commit 6c267a3

File tree

13 files changed

+312
-603
lines changed

13 files changed

+312
-603
lines changed

Classes/JSONAPI.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@
1515

1616
@interface JSONAPI : NSObject
1717

18-
@property (nonatomic, strong) NSDictionary *meta;
19-
@property (nonatomic, strong) NSDictionary *linked;
20-
@property (nonatomic, strong) NSError *error;
18+
@property (nonatomic, strong, readonly) NSDictionary *meta;
19+
@property (nonatomic, strong, readonly) id data;
20+
@property (nonatomic, strong, readonly) NSArray *errors;
2121

22-
+ (id)JSONAPIWithString:(NSString*)string;
23-
+ (id)JSONAPIWithDictionary:(NSDictionary*)dictionary;
22+
@property (readonly) id resource;
23+
@property (nonatomic, strong, readonly) NSArray *resources;
2424

25-
- (id)initWithString:(NSString*)string;
26-
- (id)initWithDictionary:(NSDictionary*)dictionary;
25+
@property (nonatomic, strong, readonly) NSError *internalError;
2726

28-
- (id)objectForKey:(NSString*)key;
29-
- (id)resource;
30-
- (NSArray*)resources;
27+
// Initializers
28+
+ (instancetype)jsonAPIWithDictionary:(NSDictionary *)dictionary;
29+
+ (instancetype)jsonAPIWithString:(NSString *)string;
30+
- (instancetype)initWithDictionary:(NSDictionary*)dictionary;
31+
- (instancetype)initWithString:(NSString*)string;
3132

3233
@end

Classes/JSONAPI.m

Lines changed: 106 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,30 @@ @interface JSONAPI()
1616

1717
@implementation JSONAPI
1818

19-
#pragma mark - Init
19+
#pragma mark - Class
2020

21-
+ (id)JSONAPIWithString:(NSString*)string {
22-
return [[JSONAPI alloc] initWithString:string];
21+
+ (instancetype)jsonAPIWithDictionary:(NSDictionary *)dictionary {
22+
return [[JSONAPI alloc] initWithDictionary:dictionary];
2323
}
2424

25-
+ (id)JSONAPIWithDictionary:(NSDictionary*)dictionary {
26-
return [[JSONAPI alloc] initWithDictionary:dictionary];
25+
+ (instancetype)jsonAPIWithString:(NSString *)string {
26+
return [[JSONAPI alloc] initWithString:string];
2727
}
2828

29-
- (id)initWithString:(NSString*)string {
29+
#pragma mark - Instance
30+
31+
- (instancetype)initWithDictionary:(NSDictionary*)dictionary {
3032
self = [super init];
3133
if (self) {
32-
[self inflateWithString:string];
34+
[self inflateWithDictionary:dictionary];
3335
}
3436
return self;
3537
}
3638

37-
- (id)initWithDictionary:(NSDictionary*)dictionary {
39+
- (instancetype)initWithString:(NSString*)string {
3840
self = [super init];
3941
if (self) {
40-
[self inflateWithDictionary:dictionary];
42+
[self inflateWithString:string];
4143
}
4244
return self;
4345
}
@@ -48,97 +50,124 @@ - (void)inflateWithString:(NSString*)string {
4850
if ([json isKindOfClass:[NSDictionary class]] == YES) {
4951
[self inflateWithDictionary:json];
5052
} 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];
5254
}
5355
}
5456

5557
#pragma mark - Resources
5658

57-
- (id)objectForKey:(NSString*)key {
58-
return [_dictionary objectForKey:key];
59-
}
60-
6159
- (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;
8361
}
8462

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+
//}
99105

100106
#pragma mark - Private
101107

102108
- (void)inflateWithDictionary:(NSDictionary*)dictionary {
103-
// Sets dictionary
109+
110+
// Sets internal dictionary
104111
_dictionary = dictionary;
105112

106113
// Sets meta
107-
_meta = [dictionary objectForKey:@"meta"];
114+
_meta = dictionary[@"meta"];
108115
if ([_meta isKindOfClass:[NSDictionary class]] == NO) {
109116
_meta = nil;
110117
}
111118

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) {
133124

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];
137129
}
138130

131+
} else if ([_data isKindOfClass:[NSDictionary class]] == YES) {
132+
id resource = [self inflateResourceData:_data];
133+
if (resource) [resources addObject:resource];
139134
}
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];
142171
}
143172

144173
@end

Classes/JSONAPIResource.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,11 @@
1111
@interface JSONAPIResource : NSObject<NSCopying, NSCoding>
1212

1313
@property (nonatomic, strong) id ID;
14-
@property (nonatomic, strong) NSString *href;
14+
@property (nonatomic, strong) NSString *type;
1515
@property (nonatomic, strong) NSDictionary *links;
1616

17-
+ (NSArray*)jsonAPIResources:(NSArray*)array withLinked:(NSDictionary*)linked;
18-
+ (id)jsonAPIResource:(NSDictionary*)dictionary withLinked:(NSDictionary*)linked;
19-
20-
- (id)initWithDictionary:(NSDictionary*)dict withLinked:(NSDictionary*)linked;
21-
- (void)setWithDictionary:(NSDictionary*)dict;
22-
23-
- (id)objectForKey:(NSString*)key;
24-
- (id)linkedResourceForKey:(NSString*)key;
25-
26-
- (void)linkLinks:(NSDictionary*)linked;
17+
+ (id)jsonAPIResource:(NSDictionary*)dictionary;
18+
+ (NSArray*)jsonAPIResources:(NSArray*)array;
2719

2820
- (NSDictionary *)mapKeysToProperties;
2921

0 commit comments

Comments
 (0)