Skip to content

Commit fab6b40

Browse files
author
Josh Holtz
committed
Merge pull request #2 from joshdholtz/links-in-linked
Links in linked
2 parents dc62766 + 5cbc8fc commit fab6b40

File tree

4 files changed

+68
-49
lines changed

4 files changed

+68
-49
lines changed

Classes/JSONAPI.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ - (void)inflateWithDictionary:(NSDictionary*)dictionary {
101101
NSDictionary *rawLinked = [dictionary objectForKey:@"linked"];
102102
if ([rawLinked isKindOfClass:[NSDictionary class]] == YES) {
103103

104+
NSMutableArray *linkedToLinkWithLinked = [NSMutableArray array];
105+
104106
// Loops through linked arrays
105107
for (NSString *key in rawLinked.allKeys) {
106108
NSArray *value = [rawLinked objectForKey:key];
@@ -111,14 +113,22 @@ - (void)inflateWithDictionary:(NSDictionary*)dictionary {
111113
Class c = [JSONAPIResourceModeler resourceForLinkedType:[JSONAPIResourceLinker linkedType:key]];
112114
JSONAPIResource *resource = [JSONAPIResource jsonAPIResource:resourceDictionary withLinked:nil withClass:c];
113115
[resources setObject:resource forKey:resource.ID];
116+
117+
[linkedToLinkWithLinked addObject:resource];
114118
}
115119
[creatingLinked setObject:resources forKey:key];
116120

117121
}
118122

119123
}
120124

125+
// Linked the linked
126+
for (JSONAPIResource *resource in linkedToLinkWithLinked) {
127+
[resource linkLinks:creatingLinked];
128+
}
129+
121130
}
131+
122132
_linked = creatingLinked;
123133
}
124134

Classes/JSONAPIResource.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@
2323
- (id)objectForKey:(NSString*)key;
2424
- (id)linkedResourceForKey:(NSString*)key;
2525

26+
- (void)linkLinks:(NSDictionary*)linked;
27+
2628
@end

Classes/JSONAPIResource.m

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ - (id)initWithDictionary:(NSDictionary*)dict withLinked:(NSDictionary*)linked {
6262
self = [self init];
6363
if (self) {
6464
[self setWithDictionary:dict];
65-
[self setWithLinks:linked];
65+
[self linkLinks:linked];
6666
}
6767
return self;
6868
}
@@ -104,28 +104,9 @@ - (void)setWithDictionary:(NSDictionary*)dict {
104104

105105
@try {
106106
if (inflateRange.location != NSNotFound) {
107-
// NSString *object = [property substringToIndex:inflateRange.location];
108-
// property = [property substringFromIndex:(inflateRange.location+1)];
109-
//
110-
// Class class = NSClassFromString(object);
111-
// if ([[dict objectForKey:key] isKindOfClass:[NSDictionary class]]) {
112-
// ProtocolModel *obj = [[class alloc] initWithDictionary:[dict objectForKey:key]];
113-
// [obj setParentModel:self];
114-
//
115-
// [self setValue:obj forKey:property];
116-
// } else if ([[dict objectForKey:key] isKindOfClass:[NSArray class]]) {
117-
// NSArray *array = [ProtocolModel protocolModels:[dict objectForKey:key] withClass:class];
118-
// for (ProtocolModel *model in array) {
119-
// [model setParentModel:self];
120-
// }
121-
//
122-
// [self setValue:array forKey:property];
123-
// }
107+
124108
} else if (formatRange.location != NSNotFound) {
125-
// NSString *formatFunction = [property substringToIndex:formatRange.location];
126-
// property = [property substringFromIndex:(formatRange.location+1)];
127-
//
128-
// [self setValue:[[ProtocolModel sharedInstance] performFormatBlock:[dict objectForKey:key] withKey:formatFunction] forKey:property ];
109+
129110
} else {
130111
[self setValue:[dict objectForKey:key] forKey:property ];
131112
}
@@ -141,7 +122,7 @@ - (void)setWithDictionary:(NSDictionary*)dict {
141122
}
142123
}
143124

144-
- (void)setWithLinks:(NSDictionary*)linked {
125+
- (void)linkLinks:(NSDictionary*)linked {
145126
// Loops through links of resources
146127
for (NSString *linkTypeUnmapped in self.links.allKeys) {
147128

@@ -155,7 +136,9 @@ - (void)setWithLinks:(NSDictionary*)linked {
155136
if ([linksTo isKindOfClass:[NSNumber class]] == YES) {
156137
JSONAPIResource *linkedResource = [[linked objectForKey:linkType] objectForKey:linksTo];
157138

158-
[self.__resourceLinks setObject:linkedResource forKey:linkTypeUnmapped];
139+
if (linkedResource != nil) {
140+
[self.__resourceLinks setObject:linkedResource forKey:linkTypeUnmapped];
141+
}
159142

160143
} else if ([linksTo isKindOfClass:[NSArray class]] == YES) {
161144
NSMutableArray *linkedResources = [NSMutableArray array];

Project/JSONAPITests/JSONAPITests.m

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ - (void)setUp
2727
[JSONAPIResourceLinker link:@"author" toLinkedType:@"authors"];
2828
[JSONAPIResourceLinker link:@"authors" toLinkedType:@"authors"]; // Don't NEED this but why not be explicit
2929
[JSONAPIResourceLinker link:@"person" toLinkedType:@"people"];
30+
[JSONAPIResourceLinker link:@"chapter" toLinkedType:@"chapters"];
31+
[JSONAPIResourceLinker link:@"book" toLinkedType:@"books"];
3032

3133
[JSONAPIResourceModeler useResource:[CommentResource class] toLinkedType:@"comment"];
3234
[JSONAPIResourceModeler useResource:[PeopleResource class] toLinkedType:@"authors"];
@@ -50,8 +52,7 @@ - (void)testMeta
5052
NSDictionary *json = @{ @"meta" : meta, @"linked" : linked, @"posts" : @[ @{ @"id" : @1, @"name" : @"Josh is awesome", @"links" : @{ @"author" : @9 } } ] };
5153

5254
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
53-
NSAssert([meta isEqualToDictionary:jsonAPI.meta], @"Meta does not equal %@", meta);
54-
// @"No implementation for \"%s\"", __PRETTY_FUNCTION__
55+
XCTAssert([meta isEqualToDictionary:jsonAPI.meta], @"Meta does not equal %@", meta);
5556

5657
}
5758

@@ -62,7 +63,7 @@ - (void)testBadMeta
6263
NSDictionary *json = @{ @"meta" : meta, @"linked" : linked, @"posts" : @[ @{ @"id" : @1, @"name" : @"Josh is awesome", @"links" : @{ @"author" : @9 } } ] };
6364

6465
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
65-
NSAssert(jsonAPI.meta == nil, @"Meta is not nil");
66+
XCTAssertNil(jsonAPI.meta, @"Meta is not nil");
6667

6768
}
6869

@@ -73,7 +74,7 @@ - (void)testLinkedCount
7374
NSDictionary *json = @{ @"meta" : meta, @"linked" : linked, @"posts" : @[ @{ @"id" : @1, @"name" : @"Josh is awesome", @"links" : @{ @"author" : @9 } } ] };
7475

7576
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
76-
NSAssert(linked.count == jsonAPI.linked.count, @"Linked count does not equal %d", linked.count);
77+
XCTAssert(linked.count == jsonAPI.linked.count, @"Linked count does not equal %d", linked.count);
7778

7879
}
7980

@@ -85,8 +86,8 @@ - (void)testLinkedResource {
8586

8687
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
8788
JSONAPIResource *linkedAuthorResource = [[jsonAPI.linked objectForKey:@"authors"] objectForKey:@9];
88-
NSAssert([[linkedAuthor9 objectForKey:@"id"] isEqualToNumber:linkedAuthorResource.ID] , @"Author resource ID does not equal %@", [linkedAuthor9 objectForKey:@"id"]);
89-
NSAssert([[linkedAuthor9 objectForKey:@"name"] isEqualToString:[linkedAuthorResource objectForKey:@"name"]] , @"Author resource name does not equal %@", [linkedAuthor9 objectForKey:@"name"]);
89+
XCTAssertEqualObjects([linkedAuthor9 objectForKey:@"id"], linkedAuthorResource.ID , @"Author resource ID does not equal %@", [linkedAuthor9 objectForKey:@"id"]);
90+
XCTAssertEqualObjects([linkedAuthor9 objectForKey:@"name"], [linkedAuthorResource objectForKey:@"name"] , @"Author resource name does not equal %@", [linkedAuthor9 objectForKey:@"name"]);
9091
}
9192

9293
- (void)testBadLinked
@@ -96,7 +97,8 @@ - (void)testBadLinked
9697
NSDictionary *json = @{ @"meta" : meta, @"linked" : linked, @"posts" : @[ @{ @"id" : @1, @"name" : @"Josh is awesome", @"links" : @{ @"author" : @9 } } ] };
9798

9899
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
99-
NSAssert(jsonAPI.linked.count == 0, @"Linked is not 0");
100+
NSLog(@"%d HRM %d", jsonAPI.linked.count, 0);
101+
XCTAssert(jsonAPI.linked.count == 0, @"Linked is not 0");
100102

101103
}
102104

@@ -109,7 +111,7 @@ - (void)testResourcesCount {
109111
NSDictionary *json = @{ @"meta" : meta, @"linked" : linked, @"posts" : posts };
110112

111113
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
112-
NSAssert(posts.count == [jsonAPI resourcesForKey:@"posts"].count, @"Posts count does not equal %d", posts.count);
114+
XCTAssert(posts.count == [jsonAPI resourcesForKey:@"posts"].count, @"Posts count does not equal %d", posts.count);
113115
}
114116

115117
- (void)testResourcesObject {
@@ -123,8 +125,8 @@ - (void)testResourcesObject {
123125
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
124126
JSONAPIResource *resource = [jsonAPI resourceForKey:@"posts"];
125127

126-
NSAssert([[post objectForKey:@"id"] isEqualToNumber:resource.ID] , @"Posts ID does not equal %@", [post objectForKey:@"id"]);
127-
NSAssert([[post objectForKey:@"name"] isEqualToString:[resource objectForKey:@"name"]] , @"Posts name does not equal %@", [post objectForKey:@"name"]);
128+
XCTAssertEqualObjects([post objectForKey:@"id"], resource.ID, @"Posts ID does not equal %@", [post objectForKey:@"id"]);
129+
XCTAssertEqualObjects([post objectForKey:@"name"], [resource objectForKey:@"name"], @"Posts name does not equal %@", [post objectForKey:@"name"]);
128130
}
129131

130132
- (void)testResourceLinkSameTypeName {
@@ -141,9 +143,9 @@ - (void)testResourceLinkSameTypeName {
141143

142144
JSONAPIResource *linkedAuthor = [resource linkedResourceForKey:@"author"];
143145

144-
NSAssert(linkedAuthor != nil, @"Linked author is nil");
145-
NSAssert([[linkedAuthor objectForKey:@"name"] isEqualToString:[linkedAuthor9 objectForKey:@"name"]], @"Linked author's name is not equal to %@", [linkedAuthor9 objectForKey:@"name"]);
146-
NSAssert([[linkedAuthor objectForKey:@"name"] isEqualToString:[linkedAuthor9 objectForKey:@"name"]], @"Linked author's name is not equal to %@", [linkedAuthor9 objectForKey:@"name"]);
146+
XCTAssertNotNil(linkedAuthor, @"Linked author is nil");
147+
XCTAssertEqualObjects([linkedAuthor objectForKey:@"name"], [linkedAuthor9 objectForKey:@"name"], @"Linked author's name is not equal to %@", [linkedAuthor9 objectForKey:@"name"]);
148+
XCTAssertEqualObjects([linkedAuthor objectForKey:@"name"], [linkedAuthor9 objectForKey:@"name"], @"Linked author's name is not equal to %@", [linkedAuthor9 objectForKey:@"name"]);
147149

148150
}
149151

@@ -171,13 +173,13 @@ - (void)testResourceLinksSameTypeName {
171173
}
172174
}
173175

174-
NSAssert(linkedAuthorsResources.count == linkedAuthors.count, @"Linked author resource count is not %d", linkedAuthors.count);
176+
XCTAssert(linkedAuthorsResources.count == linkedAuthors.count, @"Linked author resource count is not %d", linkedAuthors.count);
175177

176-
NSAssert([[linkedAuthorResource9 objectForKey:@"name"] isEqualToString:[linkedAuthor9 objectForKey:@"name"]], @"Linked author's 9 name is not equal to %@", [linkedAuthor9 objectForKey:@"name"]);
177-
NSAssert([[linkedAuthorResource9 objectForKey:@"name"] isEqualToString:[linkedAuthor9 objectForKey:@"name"]], @"Linked author's 9 name is not equal to %@", [linkedAuthor9 objectForKey:@"name"]);
178+
XCTAssertEqualObjects([linkedAuthorResource9 objectForKey:@"name"], [linkedAuthor9 objectForKey:@"name"], @"Linked author's 9 name is not equal to %@", [linkedAuthor9 objectForKey:@"name"]);
179+
XCTAssertEqualObjects([linkedAuthorResource9 objectForKey:@"name"], [linkedAuthor9 objectForKey:@"name"], @"Linked author's 9 name is not equal to %@", [linkedAuthor9 objectForKey:@"name"]);
178180

179-
NSAssert([[linkedAuthorResource11 objectForKey:@"name"] isEqualToString:[linkedAuthor11 objectForKey:@"name"]], @"Linked author's 11 name is not equal to %@", [linkedAuthor9 objectForKey:@"name"]);
180-
NSAssert([[linkedAuthorResource11 objectForKey:@"name"] isEqualToString:[linkedAuthor11 objectForKey:@"name"]], @"Linked author's 11 name is not equal to %@", [linkedAuthor9 objectForKey:@"name"]);
181+
XCTAssertEqualObjects([linkedAuthorResource11 objectForKey:@"name"], [linkedAuthor11 objectForKey:@"name"], @"Linked author's 11 name is not equal to %@", [linkedAuthor9 objectForKey:@"name"]);
182+
XCTAssertEqualObjects([linkedAuthorResource11 objectForKey:@"name"], [linkedAuthor11 objectForKey:@"name"], @"Linked author's 11 name is not equal to %@", [linkedAuthor9 objectForKey:@"name"]);
181183

182184
}
183185

@@ -197,9 +199,9 @@ - (void)testResourceLinksRealDifferentTypeName {
197199

198200
JSONAPIResource *linkedPersonResource = [resource linkedResourceForKey:@"person"];
199201

200-
NSAssert(linkedPersonResource != nil, @"Linked person is nil");
201-
NSAssert([[linkedPersonResource objectForKey:@"name"] isEqualToString:[linkedPeople9 objectForKey:@"name"]], @"Linked person's 9 name is not equal to %@", [linkedPeople11 objectForKey:@"name"]);
202-
NSAssert([[linkedPersonResource objectForKey:@"name"] isEqualToString:[linkedPeople9 objectForKey:@"name"]], @"Linked person's 9 name is not equal to %@", [linkedPeople11 objectForKey:@"name"]);
202+
XCTAssertNotNil(linkedPersonResource, @"Linked person is nil");
203+
XCTAssertEqualObjects([linkedPersonResource objectForKey:@"name"], [linkedPeople9 objectForKey:@"name"], @"Linked person's 9 name is not equal to %@", [linkedPeople11 objectForKey:@"name"]);
204+
XCTAssertEqualObjects([linkedPersonResource objectForKey:@"name"], [linkedPeople9 objectForKey:@"name"], @"Linked person's 9 name is not equal to %@", [linkedPeople11 objectForKey:@"name"]);
203205

204206

205207
}
@@ -217,10 +219,32 @@ - (void)testResourceModels {
217219
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
218220
PostResource *postResource = [jsonAPI resourceForKey:@"posts"];
219221

220-
NSAssert([postResource class] == [PostResource class], @"Post resource is not of type PostResource, but %@", [postResource class]);
221-
NSAssert([postResource.author class] == [PeopleResource class], @"Post resource's author is not of type PeopleResource, but %@", [postResource.author class]);
222-
NSAssert([postResource.name isEqualToString:[post objectForKey:@"name"]], @"Post name is not equal to %@", [post objectForKey:@"name"]);
223-
NSAssert([postResource.author.name isEqualToString:[linkedAuthor9 objectForKey:@"name"]], @"Author name is not equal to %@", [post objectForKey:@"name"]);
222+
XCTAssertEqual([postResource class], [PostResource class], @"Post resource is not of type PostResource, but %@", [postResource class]);
223+
XCTAssertEqual([postResource.author class], [PeopleResource class], @"Post resource's author is not of type PeopleResource, but %@", [postResource.author class]);
224+
XCTAssertEqualObjects(postResource.name, [post objectForKey:@"name"], @"Post name is not equal to %@", [post objectForKey:@"name"]);
225+
XCTAssertEqualObjects(postResource.author.name, [linkedAuthor9 objectForKey:@"name"], @"Author name is not equal to %@", [post objectForKey:@"name"]);
226+
}
227+
228+
- (void)testLinksInLinked {
229+
NSDictionary *meta = @{};
230+
NSDictionary *linkedBook9 = @{ @"id" : @9, @"name" : @"A Book That's A Book", @"links" : @{ @"author" : @11 } };
231+
NSArray *linkedBooks = @[ linkedBook9 ];
232+
NSDictionary *linkedAuthor11 = @{ @"id" : @11, @"name" : @"Bandit"};
233+
NSArray *linkedAuthors = @[ linkedAuthor11 ];
234+
NSDictionary *linked = @{ @"authors" : linkedAuthors, @"books" : linkedBooks};
235+
NSDictionary *chapter = @{ @"id" : @1, @"name" : @"Chapter 1: And It Begins", @"links" : @{ @"book" : @9 } };
236+
NSArray *chapters = @[ chapter ];
237+
NSDictionary *json = @{ @"meta" : meta, @"linked" : linked, @"chapters" : chapters };
238+
239+
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
240+
241+
JSONAPIResource *chapterResource = [jsonAPI resourceForKey:@"chapters"];
242+
JSONAPIResource *bookResource = [chapterResource linkedResourceForKey:@"book"];
243+
JSONAPIResource *authorResource = [bookResource linkedResourceForKey:@"author"];
244+
245+
XCTAssertEqualObjects([chapterResource objectForKey:@"name"], [chapter objectForKey:@"name"], @"Chapter name should equal %@", [chapter objectForKey:@"name"]);
246+
XCTAssertEqualObjects([bookResource objectForKey:@"name"], [linkedBook9 objectForKey:@"name"], @"Book name should equal %@", [chapter objectForKey:@"name"]);
247+
XCTAssertEqualObjects([authorResource objectForKey:@"name"], [linkedAuthor11 objectForKey:@"name"], @"Author name should equal %@", [chapter objectForKey:@"name"]);
224248
}
225249

226250
@end

0 commit comments

Comments
 (0)