Skip to content

Commit 03d6d96

Browse files
author
Josh Holtz
committed
Added errors, removed other stuff
1 parent 8102024 commit 03d6d96

File tree

11 files changed

+124
-91
lines changed

11 files changed

+124
-91
lines changed

Classes/JSONAPI.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
#import "JSONAPIResource.h"
1212
#import "JSONAPIResourceFormatter.h"
13-
#import "JSONAPIResourceLinker.h"
1413
#import "JSONAPIResourceModeler.h"
14+
#import "JSONAPIErrorResource.h"
1515

1616
@interface JSONAPI : NSObject
1717

@@ -32,5 +32,6 @@
3232
- (instancetype)initWithString:(NSString*)string;
3333

3434
- (id)includedResource:(id)ID withType:(NSString*)type;
35+
- (BOOL)hasErrors;
3536

3637
@end

Classes/JSONAPI.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#import "JSONAPI.h"
1010

11+
#import "JSONAPIErrorResource.h"
12+
1113
@interface JSONAPI()
1214

1315
@property (nonatomic, strong) NSDictionary *dictionary;
@@ -66,6 +68,10 @@ - (id)includedResource:(id)ID withType:(NSString *)type {
6668
return _includedResources[type][ID];
6769
}
6870

71+
- (BOOL)hasErrors {
72+
return _errors.count > 0;
73+
}
74+
6975
#pragma mark - Private
7076

7177
- (void)inflateWithDictionary:(NSDictionary*)dictionary {
@@ -126,6 +132,16 @@ - (void)inflateWithDictionary:(NSDictionary*)dictionary {
126132
[resource linkWithIncluded:self];
127133
}
128134

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;
129145
}
130146

131147
- (id)inflateResourceData:(NSDictionary*)data {

Classes/JSONAPIErrorResource.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// JSONAPIErrorResource.h
3+
// JSONAPI
4+
//
5+
// Created by Josh Holtz on 3/17/15.
6+
// Copyright (c) 2015 Josh Holtz. All rights reserved.
7+
//
8+
9+
#import "JSONAPIResource.h"
10+
11+
@interface JSONAPIErrorResource : JSONAPIResource
12+
13+
@property (nonatomic, strong) NSString *status;
14+
@property (nonatomic, strong) NSString *code;
15+
@property (nonatomic, strong) NSString *title;
16+
@property (nonatomic, strong) NSString *detail;
17+
@property (nonatomic, strong) NSArray *links;
18+
@property (nonatomic, strong) NSArray *paths;
19+
20+
@end

Classes/JSONAPIErrorResource.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// JSONAPIErrorResource.m
3+
// JSONAPI
4+
//
5+
// Created by Josh Holtz on 3/17/15.
6+
// Copyright (c) 2015 Josh Holtz. All rights reserved.
7+
//
8+
9+
#import "JSONAPIErrorResource.h"
10+
11+
@implementation JSONAPIErrorResource
12+
13+
- (NSDictionary *)mapKeysToProperties {
14+
return @{
15+
@"status" : @"status",
16+
@"code" : @"code",
17+
@"title" : @"title",
18+
@"detail" : @"detail",
19+
@"paths" : @"paths",
20+
};
21+
}
22+
23+
@end

Classes/JSONAPIResource.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414

1515
@property (nonatomic, strong) id ID;
1616
@property (nonatomic, strong) NSString *type;
17-
@property (nonatomic, strong) NSDictionary *links;
17+
@property (nonatomic, strong) id links;
1818

1919
+ (id)jsonAPIResource:(NSDictionary*)dictionary;
2020
+ (NSArray*)jsonAPIResources:(NSArray*)array;
2121

22+
- (id)initWithDictionary:(NSDictionary*)dict;
23+
2224
- (NSDictionary *)mapKeysToProperties;
2325
- (void)linkWithIncluded:(JSONAPI*)jsonAPI;
2426

Classes/JSONAPIResource.m

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#import "JSONAPI.h"
1212
#import "JSONAPIResourceFormatter.h"
13-
#import "JSONAPIResourceLinker.h"
1413
#import "JSONAPIResourceModeler.h"
1514

1615
#import <objc/runtime.h>
@@ -35,7 +34,7 @@ + (NSArray*)jsonAPIResources:(NSArray*)array {
3534
NSMutableArray *mutableArray = @[].mutableCopy;
3635
for (NSDictionary *dict in array) {
3736
NSString *type = dict[@"type"] ?: @"";
38-
Class resourceObjectClass = [JSONAPIResourceModeler resourceForLinkedType:[JSONAPIResourceLinker linkedType:type]];
37+
Class resourceObjectClass = [JSONAPIResourceModeler resourceForLinkedType:type];
3938
[mutableArray addObject:[[resourceObjectClass alloc] initWithDictionary:dict]];
4039
}
4140

@@ -44,7 +43,7 @@ + (NSArray*)jsonAPIResources:(NSArray*)array {
4443

4544
+ (id)jsonAPIResource:(NSDictionary*)dictionary {
4645
NSString *type = dictionary[@"type"] ?: @"";
47-
Class resourceObjectClass = [JSONAPIResourceModeler resourceForLinkedType:[JSONAPIResourceLinker linkedType:type]];
46+
Class resourceObjectClass = [JSONAPIResourceModeler resourceForLinkedType:type];
4847

4948
return [[resourceObjectClass alloc] initWithDictionary:dictionary];
5049
}
@@ -141,7 +140,8 @@ - (void)linkWithIncluded:(JSONAPI*)jsonAPI {
141140
NSDictionary *included = jsonAPI.includedResources;
142141

143142
// Loops through links of resources
144-
for (NSString *linkKey in self.links.allKeys) {
143+
NSDictionary *links = self.links;
144+
for (NSString *linkKey in links.allKeys) {
145145

146146
// Gets linked objects for the resource
147147
id linksTo = self.links[linkKey];
@@ -151,7 +151,6 @@ - (void)linkWithIncluded:(JSONAPI*)jsonAPI {
151151
if ([linkage isKindOfClass:[NSDictionary class]] == YES) {
152152

153153
NSString *linkType = linkage[@"type"];
154-
linkType = [JSONAPIResourceLinker linkedType:linkType] ?: linkType;
155154

156155
if (linkage[@"id"] != nil) {
157156
id linksToId = linkage[@"id"];
@@ -167,7 +166,6 @@ - (void)linkWithIncluded:(JSONAPI*)jsonAPI {
167166
NSMutableArray *linkedResources = @[].mutableCopy;
168167
for (NSDictionary *linkageData in linkage) {
169168
NSString *linkType = linkageData[@"type"];
170-
linkType = [JSONAPIResourceLinker linkedType:linkType] ?: linkType;
171169

172170
if (linkageData[@"id"] != nil) {
173171
id linksToId = linkageData[@"id"];

Classes/JSONAPIResourceLinker.h

Lines changed: 0 additions & 20 deletions
This file was deleted.

Classes/JSONAPIResourceLinker.m

Lines changed: 0 additions & 56 deletions
This file was deleted.

Project/JSONAPI.xcodeproj/project.pbxproj

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
/* Begin PBXBuildFile section */
1010
033A6C5F18695CD2001CF9FA /* JSONAPIResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 033A6C5E18695CD2001CF9FA /* JSONAPIResource.m */; };
11-
0386643D186A0DAD00985CEC /* JSONAPIResourceLinker.m in Sources */ = {isa = PBXBuildFile; fileRef = 0386643C186A0DAD00985CEC /* JSONAPIResourceLinker.m */; };
1211
0386644E186A8DA000985CEC /* JSONAPIResourceModeler.m in Sources */ = {isa = PBXBuildFile; fileRef = 0386644D186A8DA000985CEC /* JSONAPIResourceModeler.m */; };
1312
03866451186A909200985CEC /* PeopleResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 03866450186A909200985CEC /* PeopleResource.m */; };
1413
03866454186A94B700985CEC /* CommentResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 03866453186A94B700985CEC /* CommentResource.m */; };
@@ -33,6 +32,9 @@
3332
03A9ED58196DEF9B00E61E2E /* JSONAPIResourceFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = 03A9ED57196DEF9B00E61E2E /* JSONAPIResourceFormatter.m */; };
3433
03FBD8DA1AB879FD00789DF3 /* main_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 03FBD8D91AB879FD00789DF3 /* main_example.json */; };
3534
03FBD8DC1AB87EE000789DF3 /* main_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 03FBD8D91AB879FD00789DF3 /* main_example.json */; };
35+
03FBD8DF1AB8DF8E00789DF3 /* JSONAPIErrorResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 03FBD8DE1AB8DF8E00789DF3 /* JSONAPIErrorResource.m */; };
36+
03FBD8E21AB8E46E00789DF3 /* error_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 03FBD8E11AB8E46E00789DF3 /* error_example.json */; };
37+
03FBD8E31AB8E46E00789DF3 /* error_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 03FBD8E11AB8E46E00789DF3 /* error_example.json */; };
3638
/* End PBXBuildFile section */
3739

3840
/* Begin PBXContainerItemProxy section */
@@ -48,8 +50,6 @@
4850
/* Begin PBXFileReference section */
4951
033A6C5D18695CD2001CF9FA /* JSONAPIResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSONAPIResource.h; sourceTree = "<group>"; };
5052
033A6C5E18695CD2001CF9FA /* JSONAPIResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONAPIResource.m; sourceTree = "<group>"; };
51-
0386643B186A0DAD00985CEC /* JSONAPIResourceLinker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSONAPIResourceLinker.h; sourceTree = "<group>"; };
52-
0386643C186A0DAD00985CEC /* JSONAPIResourceLinker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONAPIResourceLinker.m; sourceTree = "<group>"; };
5353
0386644C186A8DA000985CEC /* JSONAPIResourceModeler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSONAPIResourceModeler.h; sourceTree = "<group>"; };
5454
0386644D186A8DA000985CEC /* JSONAPIResourceModeler.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONAPIResourceModeler.m; sourceTree = "<group>"; };
5555
0386644F186A909200985CEC /* PeopleResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PeopleResource.h; sourceTree = "<group>"; };
@@ -84,6 +84,9 @@
8484
03A9ED56196DEF9B00E61E2E /* JSONAPIResourceFormatter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSONAPIResourceFormatter.h; sourceTree = "<group>"; };
8585
03A9ED57196DEF9B00E61E2E /* JSONAPIResourceFormatter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONAPIResourceFormatter.m; sourceTree = "<group>"; };
8686
03FBD8D91AB879FD00789DF3 /* main_example.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = main_example.json; sourceTree = "<group>"; };
87+
03FBD8DD1AB8DF8E00789DF3 /* JSONAPIErrorResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSONAPIErrorResource.h; sourceTree = "<group>"; };
88+
03FBD8DE1AB8DF8E00789DF3 /* JSONAPIErrorResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONAPIErrorResource.m; sourceTree = "<group>"; };
89+
03FBD8E11AB8E46E00789DF3 /* error_example.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = error_example.json; sourceTree = "<group>"; };
8790
/* End PBXFileReference section */
8891

8992
/* Begin PBXFrameworksBuildPhase section */
@@ -210,10 +213,10 @@
210213
033A6C5E18695CD2001CF9FA /* JSONAPIResource.m */,
211214
03A9ED56196DEF9B00E61E2E /* JSONAPIResourceFormatter.h */,
212215
03A9ED57196DEF9B00E61E2E /* JSONAPIResourceFormatter.m */,
213-
0386643B186A0DAD00985CEC /* JSONAPIResourceLinker.h */,
214-
0386643C186A0DAD00985CEC /* JSONAPIResourceLinker.m */,
215216
0386644C186A8DA000985CEC /* JSONAPIResourceModeler.h */,
216217
0386644D186A8DA000985CEC /* JSONAPIResourceModeler.m */,
218+
03FBD8DD1AB8DF8E00789DF3 /* JSONAPIErrorResource.h */,
219+
03FBD8DE1AB8DF8E00789DF3 /* JSONAPIErrorResource.m */,
217220
);
218221
name = Classes;
219222
path = ../Classes;
@@ -223,6 +226,7 @@
223226
isa = PBXGroup;
224227
children = (
225228
03FBD8D91AB879FD00789DF3 /* main_example.json */,
229+
03FBD8E11AB8E46E00789DF3 /* error_example.json */,
226230
);
227231
name = Assets;
228232
sourceTree = "<group>";
@@ -307,6 +311,7 @@
307311
03866459186CCE6600985CEC /* CHANGELOG.md in Resources */,
308312
03A055C51868E038004807F0 /* InfoPlist.strings in Resources */,
309313
03FBD8DC1AB87EE000789DF3 /* main_example.json in Resources */,
314+
03FBD8E21AB8E46E00789DF3 /* error_example.json in Resources */,
310315
03A055CE1868E038004807F0 /* Main.storyboard in Resources */,
311316
03A055F81868E08A004807F0 /* JSONAPI.podspec in Resources */,
312317
);
@@ -316,6 +321,7 @@
316321
isa = PBXResourcesBuildPhase;
317322
buildActionMask = 2147483647;
318323
files = (
324+
03FBD8E31AB8E46E00789DF3 /* error_example.json in Resources */,
319325
03A055E41868E038004807F0 /* InfoPlist.strings in Resources */,
320326
03FBD8DA1AB879FD00789DF3 /* main_example.json in Resources */,
321327
);
@@ -331,14 +337,14 @@
331337
03866451186A909200985CEC /* PeopleResource.m in Sources */,
332338
03866457186A94C200985CEC /* PostResource.m in Sources */,
333339
03A055D11868E038004807F0 /* ViewController.m in Sources */,
334-
0386643D186A0DAD00985CEC /* JSONAPIResourceLinker.m in Sources */,
335340
03A055F71868E08A004807F0 /* JSONAPI.m in Sources */,
336341
033A6C5F18695CD2001CF9FA /* JSONAPIResource.m in Sources */,
337342
03866454186A94B700985CEC /* CommentResource.m in Sources */,
338343
03A9ED58196DEF9B00E61E2E /* JSONAPIResourceFormatter.m in Sources */,
339344
03A055CB1868E038004807F0 /* AppDelegate.m in Sources */,
340345
0386644E186A8DA000985CEC /* JSONAPIResourceModeler.m in Sources */,
341346
03A055C71868E038004807F0 /* main.m in Sources */,
347+
03FBD8DF1AB8DF8E00789DF3 /* JSONAPIErrorResource.m in Sources */,
342348
);
343349
runOnlyForDeploymentPostprocessing = 0;
344350
};

Project/JSONAPITests/JSONAPITests.m

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import <XCTest/XCTest.h>
1010

1111
#import "JSONAPI.h"
12+
#import "JSONAPIErrorResource.h"
1213

1314
#import "CommentResource.h"
1415
#import "PeopleResource.h"
@@ -73,7 +74,7 @@ - (void)testDataPostAuthorAndComments {
7374
XCTAssertEqual(post.comments.count, 2, @"Post should contain 2 comments");
7475
}
7576

76-
- (void)testIncludedIsLinked {
77+
- (void)testIncludedCommentIsLinked {
7778
NSDictionary *json = [self mainExampleJSON];
7879
JSONAPI *jsonAPI = [JSONAPI jsonAPIWithDictionary:json];
7980

@@ -82,12 +83,33 @@ - (void)testIncludedIsLinked {
8283
XCTAssertEqualObjects(comment.author.ID, @"9", @"Comment's author's ID should be 9");
8384
}
8485

86+
- (void)testNoError {
87+
NSDictionary *json = [self mainExampleJSON];
88+
JSONAPI *jsonAPI = [JSONAPI jsonAPIWithDictionary:json];
89+
90+
XCTAssertFalse([jsonAPI hasErrors], @"JSON API should not have errors");
91+
}
92+
93+
- (void)testError {
94+
NSDictionary *json = [self errorExampleJSON];
95+
JSONAPI *jsonAPI = [JSONAPI jsonAPIWithDictionary:json];
96+
97+
XCTAssertTrue([jsonAPI hasErrors], @"JSON API should have errors");
98+
99+
JSONAPIErrorResource *error = jsonAPI.errors.firstObject;
100+
XCTAssertEqualObjects(error.ID, @"123456", @"Error id should be 123456");
101+
}
102+
85103
#pragma mark - Private
86104

87105
- (NSDictionary*)mainExampleJSON {
88106
return [self jsonFor:@"main_example" ofType:@"json"];
89107
}
90108

109+
- (NSDictionary*)errorExampleJSON {
110+
return [self jsonFor:@"error_example" ofType:@"json"];
111+
}
112+
91113
- (NSDictionary*)jsonFor:(NSString*)resource ofType:(NSString*)type {
92114
NSString *path = [[NSBundle mainBundle] pathForResource:resource ofType:type];
93115
NSString *jsonStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

0 commit comments

Comments
 (0)