Skip to content

Commit 19c3114

Browse files
author
Josh Holtz
committed
Merge pull request #15 from joshdholtz/json-api-v1.0-final
JSON API - v1.0 final
2 parents ba9c9f2 + 1ef7f96 commit 19c3114

File tree

10 files changed

+171
-301
lines changed

10 files changed

+171
-301
lines changed

Classes/JSONAPIResourceParser.m

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ + (NSDictionary*)dictionaryFor:(NSObject <JSONAPIResource>*)resource {
101101
NSDictionary *properties = [descriptor properties];
102102
for (NSString *key in properties) {
103103
JSONAPIPropertyDescriptor *property = [properties objectForKey:key];
104-
104+
105105
id value = [resource valueForKey:key];
106106
if (value) {
107107
if ([value isKindOfClass:[NSArray class]]) {
@@ -159,12 +159,14 @@ + (NSDictionary*)dictionaryFor:(NSObject <JSONAPIResource>*)resource {
159159
}
160160

161161
if (linkage) {
162-
if (resource.selfLink) {
163-
[linkage setValue:resource.selfLink forKey:@"self"];
164-
}
165-
[dictionary setValue:linkage forKey:@"links"];
162+
[dictionary setValue:linkage forKey:@"relationships"];
166163
}
167-
164+
165+
// TODO: Need to also add in all other links
166+
if (resource.selfLink) {
167+
dictionary[@"links"] = @{ @"self": resource.selfLink };
168+
}
169+
168170
return dictionary;
169171
}
170172

@@ -173,11 +175,9 @@ + (void)set:(NSObject <JSONAPIResource> *)resource withDictionary:dictionary {
173175

174176
JSONAPIResourceDescriptor *descriptor = [[resource class] descriptor];
175177

176-
NSDictionary *links = [dictionary objectForKey:@"links"];
178+
NSDictionary *relationships = [dictionary objectForKey:@"relationships"];
177179
NSDictionary *attributes = [dictionary objectForKey:@"attributes"];
178-
179-
resource.selfLink = [links valueForKey:@"self"];
180-
180+
181181
id ID = [dictionary objectForKey:@"id"];
182182
NSFormatter *format = [descriptor idFormatter];
183183
if (format) {
@@ -195,8 +195,8 @@ + (void)set:(NSObject <JSONAPIResource> *)resource withDictionary:dictionary {
195195
JSONAPIPropertyDescriptor *property = [properties objectForKey:key];
196196

197197
if (property.resourceType) {
198-
if (links) {
199-
id value = [links objectForKey:[property jsonName]];
198+
if (relationships) {
199+
id value = [relationships objectForKey:[property jsonName]];
200200
[resource setValue:[JSONAPIResourceParser jsonAPILink:value] forKey:key];
201201
}
202202

@@ -240,7 +240,7 @@ + (void)set:(NSObject <JSONAPIResource> *)resource withDictionary:dictionary {
240240
}
241241

242242
+ (id)jsonAPILink:(NSDictionary*)dictionary {
243-
id linkage = dictionary[@"linkage"];
243+
id linkage = dictionary[@"data"];
244244
if ([linkage isKindOfClass:[NSArray class]]) {
245245
NSMutableArray *linkArray = [[NSMutableArray alloc] initWithCapacity:[linkage count]];
246246
for (NSDictionary *linkElement in linkage) {
@@ -334,7 +334,7 @@ + (NSDictionary*)link:(NSObject <JSONAPIResource>*)resource from:(NSObject <JSON
334334
[reference setValue:@{
335335
@"type" : descriptor.type,
336336
@"id" : resource.ID
337-
} forKey:@"linkage"];
337+
} forKey:@"data"];
338338
}
339339

340340
return reference;

Project/JSONAPI.xcodeproj/project.pbxproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/* Begin PBXBuildFile section */
2424
03866451186A909200985CEC /* PeopleResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 03866450186A909200985CEC /* PeopleResource.m */; };
2525
03866454186A94B700985CEC /* CommentResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 03866453186A94B700985CEC /* CommentResource.m */; };
26-
03866457186A94C200985CEC /* PostResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 03866456186A94C200985CEC /* PostResource.m */; };
26+
03866457186A94C200985CEC /* ArticleResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 03866456186A94C200985CEC /* ArticleResource.m */; };
2727
03866459186CCE6600985CEC /* CHANGELOG.md in Resources */ = {isa = PBXBuildFile; fileRef = 03866458186CCE6600985CEC /* CHANGELOG.md */; };
2828
03A055BB1868E038004807F0 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03A055BA1868E038004807F0 /* Foundation.framework */; };
2929
03A055BD1868E038004807F0 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03A055BC1868E038004807F0 /* CoreGraphics.framework */; };
@@ -69,8 +69,8 @@
6969
03866450186A909200985CEC /* PeopleResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PeopleResource.m; sourceTree = "<group>"; };
7070
03866452186A94B700985CEC /* CommentResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CommentResource.h; sourceTree = "<group>"; };
7171
03866453186A94B700985CEC /* CommentResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CommentResource.m; sourceTree = "<group>"; };
72-
03866455186A94C200985CEC /* PostResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PostResource.h; sourceTree = "<group>"; };
73-
03866456186A94C200985CEC /* PostResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PostResource.m; sourceTree = "<group>"; };
72+
03866455186A94C200985CEC /* ArticleResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ArticleResource.h; sourceTree = "<group>"; };
73+
03866456186A94C200985CEC /* ArticleResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ArticleResource.m; sourceTree = "<group>"; };
7474
03866458186CCE6600985CEC /* CHANGELOG.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = CHANGELOG.md; path = ../CHANGELOG.md; sourceTree = "<group>"; };
7575
03A055B71868E038004807F0 /* JSONAPI.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JSONAPI.app; sourceTree = BUILT_PRODUCTS_DIR; };
7676
03A055BA1868E038004807F0 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -138,8 +138,8 @@
138138
0386643E186A791D00985CEC /* ResourceModels */ = {
139139
isa = PBXGroup;
140140
children = (
141-
03866455186A94C200985CEC /* PostResource.h */,
142-
03866456186A94C200985CEC /* PostResource.m */,
141+
03866455186A94C200985CEC /* ArticleResource.h */,
142+
03866456186A94C200985CEC /* ArticleResource.m */,
143143
0386644F186A909200985CEC /* PeopleResource.h */,
144144
03866450186A909200985CEC /* PeopleResource.m */,
145145
03866452186A94B700985CEC /* CommentResource.h */,
@@ -385,7 +385,7 @@
385385
03866451186A909200985CEC /* PeopleResource.m in Sources */,
386386
681B47761B08EA9800A99D76 /* JSONAPIResourceBase.m in Sources */,
387387
680E14F51B08146E004FF8CD /* JSONAPIResourceParser.m in Sources */,
388-
03866457186A94C200985CEC /* PostResource.m in Sources */,
388+
03866457186A94C200985CEC /* ArticleResource.m in Sources */,
389389
03A055D11868E038004807F0 /* ViewController.m in Sources */,
390390
03A055F71868E08A004807F0 /* JSONAPI.m in Sources */,
391391
68A469941AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.m in Sources */,

Project/JSONAPI.xcodeproj/project.xcworkspace/xcshareddata/JSONAPI.xccheckout

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
<key>IDESourceControlProjectIdentifier</key>
88
<string>F88BDF8C-DA51-4F26-8F41-0B24041871ED</string>
99
<key>IDESourceControlProjectName</key>
10-
<string>project</string>
10+
<string>JSONAPI</string>
1111
<key>IDESourceControlProjectOriginsDictionary</key>
1212
<dict>
1313
<key>316D8028CBF982160D8D1C5442A0AAF09059D0E3</key>
14-
<string>https://github.com/joshdholtz/jsonapi-ios.git</string>
14+
<string>github.com:joshdholtz/jsonapi-ios.git</string>
1515
</dict>
1616
<key>IDESourceControlProjectPath</key>
17-
<string>Project/JSONAPI.xcodeproj/project.xcworkspace</string>
17+
<string>Project/JSONAPI.xcodeproj</string>
1818
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
1919
<dict>
2020
<key>316D8028CBF982160D8D1C5442A0AAF09059D0E3</key>
2121
<string>../../..</string>
2222
</dict>
2323
<key>IDESourceControlProjectURL</key>
24-
<string>https://github.com/joshdholtz/jsonapi-ios.git</string>
24+
<string>github.com:joshdholtz/jsonapi-ios.git</string>
2525
<key>IDESourceControlProjectVersion</key>
2626
<integer>111</integer>
2727
<key>IDESourceControlProjectWCCIdentifier</key>

Project/JSONAPI/PostResource.h renamed to Project/JSONAPI/ArticleResource.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// PostResource.h
2+
// ArticleResource.h
33
// JSONAPI
44
//
55
// Created by Josh Holtz on 12/24/13.
@@ -10,7 +10,7 @@
1010

1111
@class PeopleResource;
1212

13-
@interface PostResource : JSONAPIResourceBase
13+
@interface ArticleResource : JSONAPIResourceBase
1414

1515
@property (nonatomic, strong) NSString *title;
1616
@property (nonatomic, strong) PeopleResource *author;

Project/JSONAPI/PostResource.m renamed to Project/JSONAPI/ArticleResource.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//
2-
// PostResource.m
2+
// ArticleResource.m
33
// JSONAPI
44
//
55
// Created by Josh Holtz on 12/24/13.
66
// Copyright (c) 2013 Josh Holtz. All rights reserved.
77
//
88

9-
#import "PostResource.h"
9+
#import "ArticleResource.h"
1010

1111
#import "JSONAPIPropertyDescriptor.h"
1212
#import "JSONAPIResourceDescriptor.h"
@@ -16,14 +16,14 @@
1616
#import "CommentResource.h"
1717

1818

19-
@implementation PostResource
19+
@implementation ArticleResource
2020

2121
static JSONAPIResourceDescriptor *__descriptor = nil;
2222

2323
+ (JSONAPIResourceDescriptor*)descriptor {
2424
static dispatch_once_t onceToken;
2525
dispatch_once(&onceToken, ^{
26-
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"posts"];
26+
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"articles"];
2727

2828
[__descriptor setIdProperty:@"ID"];
2929

Project/JSONAPI/ViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#import "CommentResource.h"
1414
#import "PeopleResource.h"
15-
#import "PostResource.h"
15+
#import "ArticleResource.h"
1616

1717
@interface ViewController ()
1818

Project/JSONAPITests/JSONAPITests.m

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#import "CommentResource.h"
1717
#import "PeopleResource.h"
18-
#import "PostResource.h"
18+
#import "ArticleResource.h"
1919

2020
@interface JSONAPITests : XCTestCase
2121

@@ -28,7 +28,7 @@ - (void)setUp {
2828

2929
[JSONAPIResourceDescriptor addResource:[CommentResource class]];
3030
[JSONAPIResourceDescriptor addResource:[PeopleResource class]];
31-
[JSONAPIResourceDescriptor addResource:[PostResource class]];
31+
[JSONAPIResourceDescriptor addResource:[ArticleResource class]];
3232
}
3333

3434
- (void)tearDown {
@@ -43,18 +43,18 @@ - (void)testMeta {
4343
XCTAssertEqualObjects(jsonAPI.meta[@"hehe"], @"hoho", @"Meta's 'hehe' should equal 'hoho'");
4444
}
4545

46-
- (void)testDataPosts {
46+
- (void)testDataArticles {
4747
NSDictionary *json = [self mainExampleJSON];
4848
JSONAPI *jsonAPI = [JSONAPI jsonAPIWithDictionary:json];
4949

5050
XCTAssertNotNil(jsonAPI.resource, @"Resource should not be nil");
5151
XCTAssertNotNil(jsonAPI.resources, @"Resources should not be nil");
5252
XCTAssertEqual(jsonAPI.resources.count, 1, @"Resources should contain 1 resource");
5353

54-
PostResource *post = jsonAPI.resource;
55-
XCTAssert([post isKindOfClass:[PostResource class]], @"Post should be a PostResource");
56-
XCTAssertEqualObjects(post.ID, @"1", @"Post id should be 1");
57-
XCTAssertEqualObjects(post.title, @"JSON API paints my bikeshed!", @"Post title should be 'JSON API paints my bikeshed!'");
54+
ArticleResource *article = jsonAPI.resource;
55+
XCTAssert([article isKindOfClass:[ArticleResource class]], @"Article should be a ArticleResource");
56+
XCTAssertEqualObjects(article.ID, @"1", @"Article id should be 1");
57+
XCTAssertEqualObjects(article.title, @"JSON API paints my bikeshed!", @"Article title should be 'JSON API paints my bikeshed!'");
5858
}
5959

6060
- (void)testIncludedPeopleAndComments {
@@ -66,14 +66,14 @@ - (void)testIncludedPeopleAndComments {
6666

6767
}
6868

69-
- (void)testDataPostAuthorAndComments {
69+
- (void)testDataArticleAuthorAndComments {
7070
NSDictionary *json = [self mainExampleJSON];
7171
JSONAPI *jsonAPI = [JSONAPI jsonAPIWithDictionary:json];
7272

73-
PostResource *post = jsonAPI.resource;
74-
XCTAssertNotNil(post.author, @"Post's author should not be nil");
75-
XCTAssertNotNil(post.comments, @"Post's comments should not be nil");
76-
XCTAssertEqual(post.comments.count, 2, @"Post should contain 2 comments");
73+
ArticleResource *article = jsonAPI.resource;
74+
XCTAssertNotNil(article.author, @"Article author should not be nil");
75+
XCTAssertNotNil(article.comments, @"Article comments should not be nil");
76+
XCTAssertEqual(article.comments.count, 2, @"Article should contain 2 comments");
7777
}
7878

7979
- (void)testIncludedCommentIsLinked {
@@ -82,7 +82,7 @@ - (void)testIncludedCommentIsLinked {
8282

8383
CommentResource *comment = [jsonAPI includedResource:@"5" withType:@"comments"];
8484
XCTAssertNotNil(comment.author, @"Comment's author should not be nil");
85-
XCTAssertEqualObjects(comment.author.ID, @"9", @"Comment's author's ID should be 9");
85+
XCTAssertEqualObjects(comment.author.ID, @"2", @"Comment's author's ID should be 2");
8686
}
8787

8888
- (void)testNoError {
@@ -115,12 +115,12 @@ - (void)testSerializeSimple {
115115
}
116116

117117
- (void)testSerializeWithFormat {
118-
PostResource *newPost = [[PostResource alloc] init];
119-
newPost.title = @"Title";
120-
newPost.date = [NSDate date];
118+
ArticleResource *newArticle = [[ArticleResource alloc] init];
119+
newArticle.title = @"Title";
120+
newArticle.date = [NSDate date];
121121

122-
NSDictionary *json = [JSONAPIResourceParser dictionaryFor:newPost];
123-
XCTAssertEqualObjects(json[@"type"], @"posts", @"Did not create post!");
122+
NSDictionary *json = [JSONAPIResourceParser dictionaryFor:newArticle];
123+
XCTAssertEqualObjects(json[@"type"], @"articles", @"Did not create article!");
124124
XCTAssertNotNil(json[@"attributes"][@"date"], @"Wrong date!");
125125
XCTAssertTrue([json[@"attributes"][@"date"] isKindOfClass:[NSString class]], @"Date should be string!.");
126126
}
@@ -137,23 +137,23 @@ - (void)testSerializeComplex {
137137
newComment.author = newAuthor;
138138
newComment.text = @"First!";
139139

140-
PostResource *newPost = [[PostResource alloc] init];
141-
newPost.title = @"Title";
142-
newPost.author = newAuthor;
143-
newPost.date = [NSDate date];
144-
newPost.comments = [[NSArray alloc] initWithObjects:newComment, nil];
145-
146-
NSDictionary *json = [JSONAPIResourceParser dictionaryFor:newPost];
147-
XCTAssertEqualObjects(json[@"type"], @"posts", @"Did not create Post!");
148-
XCTAssertNotNil(json[@"links"], @"Did not create links!");
149-
XCTAssertNotNil(json[@"links"][@"author"], @"Did not create links!");
150-
XCTAssertNotNil(json[@"links"][@"author"][@"linkage"], @"Did not create links!");
151-
XCTAssertEqualObjects(json[@"links"][@"author"][@"linkage"][@"id"], newAuthor.ID, @"Wrong link ID!.");
152-
XCTAssertNil(json[@"links"][@"author"][@"first-name"], @"Bad link!");
153-
154-
XCTAssertNotNil(json[@"links"][@"comments"], @"Did not create links!");
155-
XCTAssertTrue([json[@"links"][@"comments"] isKindOfClass:[NSArray class]], @"Comments should be array!.");
156-
XCTAssertEqual([json[@"links"][@"comments"] count], 1, @"Comments should have 1 element!.");
140+
ArticleResource *newArticle = [[ArticleResource alloc] init];
141+
newArticle.title = @"Title";
142+
newArticle.author = newAuthor;
143+
newArticle.date = [NSDate date];
144+
newArticle.comments = [[NSArray alloc] initWithObjects:newComment, nil];
145+
146+
NSDictionary *json = [JSONAPIResourceParser dictionaryFor:newArticle];
147+
XCTAssertEqualObjects(json[@"type"], @"articles", @"Did not create Article!");
148+
XCTAssertNotNil(json[@"relationships"], @"Did not create links!");
149+
XCTAssertNotNil(json[@"relationships"][@"author"], @"Did not create links!");
150+
XCTAssertNotNil(json[@"relationships"][@"author"][@"data"], @"Did not create links!");
151+
XCTAssertEqualObjects(json[@"relationships"][@"author"][@"data"][@"id"], newAuthor.ID, @"Wrong link ID!.");
152+
XCTAssertNil(json[@"relationships"][@"author"][@"first-name"], @"Bad link!");
153+
154+
XCTAssertNotNil(json[@"relationships"][@"comments"], @"Did not create links!");
155+
XCTAssertTrue([json[@"relationships"][@"comments"] isKindOfClass:[NSArray class]], @"Comments should be array!.");
156+
XCTAssertEqual([json[@"relationships"][@"comments"] count], 1, @"Comments should have 1 element!.");
157157
}
158158

159159
- (void)testCreate {

Project/JSONAPITests/error_example.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
"title": "some title",
1717
"detail": "some detail",
1818
"links": ["a_link"],
19-
"paths": ["a_path"]
19+
"source": ["a_path"]
2020
}]
2121
}

0 commit comments

Comments
 (0)