From 29244f387fb5f693f3220d915b37931f02716d1f Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Wed, 15 Jun 2016 19:03:57 -0700 Subject: [PATCH 1/2] Added meta to resource --- Classes/JSONAPIResource.h | 25 +++++++++++++++++++++++++ Classes/JSONAPIResourceBase.h | 6 ++++++ Classes/JSONAPIResourceParser.m | 5 +++++ Project/JSONAPITests/JSONAPITests.m | 4 ++++ Project/JSONAPITests/main_example.json | 3 +++ 5 files changed, 43 insertions(+) diff --git a/Classes/JSONAPIResource.h b/Classes/JSONAPIResource.h index 294f20a..bd38b1e 100644 --- a/Classes/JSONAPIResource.h +++ b/Classes/JSONAPIResource.h @@ -135,5 +135,30 @@ */ - (void)setID:(id)identifier; +/** + * Get the meta for a resource instance. Optional for resources that come + * from persistance storage (i.e. the server). + * + * In general, this should be implemented by a @property ID in the realized class. The + * @property declaration will automatically synthesize the get/set members declared in this + * protocol. The property storage is an implementation detail, which is why the protocol does + * not use a @property declaration. + * + * @return The meta for a resource instance. + */ +- (NSDictionary*)meta; + +/** + * Set the meta for a resource instance. Optional for resources that come + * from persistance storage (i.e. the server). + * + * In general, this should be implemented by a @property ID in the realized class. The + * @property declaration will automatically synthesize the get/set members declared in this + * protocol. The property storage is an implementation detail, which is why the protocol does + * not use a @property declaration. + * + * @param meta The meta for a resource instance. + */ +- (void)setMeta:(NSDictionary*)meta; @end diff --git a/Classes/JSONAPIResourceBase.h b/Classes/JSONAPIResourceBase.h index b73aba6..45e64bf 100644 --- a/Classes/JSONAPIResourceBase.h +++ b/Classes/JSONAPIResourceBase.h @@ -43,4 +43,10 @@ */ @property (strong, atomic) id ID; +/** + * Meta for a resource instance. Optional for resources that come + * from persistance storage (i.e. the server). + */ +@property (strong, atomic) NSDictionary *meta; + @end diff --git a/Classes/JSONAPIResourceParser.m b/Classes/JSONAPIResourceParser.m index c0f6a47..be4c087 100644 --- a/Classes/JSONAPIResourceParser.m +++ b/Classes/JSONAPIResourceParser.m @@ -181,6 +181,7 @@ + (void)set:(NSObject *)resource withDictionary:dictionary { NSDictionary *relationships = [dictionary objectForKey:@"relationships"]; NSDictionary *attributes = [dictionary objectForKey:@"attributes"]; NSDictionary *links = [dictionary objectForKey:@"links"]; + NSDictionary *meta = [dictionary objectForKey:@"meta"]; id ID = [dictionary objectForKey:@"id"]; NSFormatter *format = [descriptor idFormatter]; @@ -197,6 +198,10 @@ + (void)set:(NSObject *)resource withDictionary:dictionary { NSString *selfLink = links[@"self"]; [resource setValue:selfLink forKey:descriptor.selfLinkProperty]; } + + if ([resource respondsToSelector:@selector(setMeta:)]) { + [resource setMeta:meta]; + } // Loops through all keys to map to properties NSDictionary *properties = [descriptor properties]; diff --git a/Project/JSONAPITests/JSONAPITests.m b/Project/JSONAPITests/JSONAPITests.m index a2807f7..8717e2a 100644 --- a/Project/JSONAPITests/JSONAPITests.m +++ b/Project/JSONAPITests/JSONAPITests.m @@ -64,6 +64,10 @@ - (void)testDataArticles { XCTAssertEqual(jsonAPI.resources.count, 1, @"Resources should contain 1 resource"); ArticleResource *article = jsonAPI.resource; + + XCTAssertNotNil(article.meta, @"Meta should not be nil"); + XCTAssertEqualObjects(article.meta[@"hehe"], @"hoho", @"Meta's 'hehe' should equal 'hoho'"); + XCTAssert([article isKindOfClass:[ArticleResource class]], @"Article should be a ArticleResource"); XCTAssertEqualObjects(article.ID, @"1", @"Article id should be 1"); XCTAssertTrue([article.selfLink isEqualToString:@"http://example.com/articles/1"], @"Article selfLink should be 'http://example.com/articles/1'"); diff --git a/Project/JSONAPITests/main_example.json b/Project/JSONAPITests/main_example.json index 9339969..82cacfe 100644 --- a/Project/JSONAPITests/main_example.json +++ b/Project/JSONAPITests/main_example.json @@ -10,6 +10,9 @@ "data": [{ "type": "articles", "id": "1", + "meta": { + "hehe": "hoho" + }, "attributes": { "title": "JSON API paints my bikeshed!", "versions": [ From ae482d0c7bd1123cbdfd341af612ebd8a286887f Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Wed, 15 Jun 2016 19:21:13 -0700 Subject: [PATCH 2/2] Version bump and updated README --- JSONAPI.podspec | 2 +- README.md | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/JSONAPI.podspec b/JSONAPI.podspec index 1027ed4..f819508 100644 --- a/JSONAPI.podspec +++ b/JSONAPI.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "JSONAPI" - s.version = "1.0.6" + s.version = "1.0.7" s.summary = "A library for loading data from a JSON API datasource." s.description = <<-DESC A library for loading data from a JSON API datasource. Parses JSON API data into models with support for auto-linking of resources and custom model classes. diff --git a/README.md b/README.md index 093f715..431601b 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ For some full examples on how to use everything, please see the tests - https:// Version | Changes --- | --- +**1.0.7** | Added `meta` and `setMeta` to `JSONAPIResource` and `JSONAPIResourceBase` (https://github.com/joshdholtz/jsonapi-ios/pull/43). **1.0.6** | Improved resource parsing and added parsing of `selfLinks` (https://github.com/joshdholtz/jsonapi-ios/pull/35). Thanks to [ artcom](https://github.com/ artcom) for helping! Also removed the need to define `setIdProperty` and `setSelfLinkProperty` in every resource (automatically mapped in the init of `JSONAPIResourceDescriptor`) **1.0.5** | Fix 1-to-many relationships serialization according to JSON API v1.0 (https://github.com/joshdholtz/jsonapi-ios/pull/34). Thanks to [RafaelKayumov](https://github.com/RafaelKayumov) for helping! **1.0.4** | Add support for empty to-one relationship according to JSON API v1.0 (https://github.com/joshdholtz/jsonapi-ios/pull/33). Thanks to [RafaelKayumov](https://github.com/RafaelKayumov) for helping! @@ -48,7 +49,7 @@ Clone the repository and drop in the .h and .m files from the "Classes" director JSONAPI is available through [CocoaPods](http://cocoapods.org), to install it simply add the following line to your Podfile: - pod 'JSONAPI', '~> 1.0.6' + pod 'JSONAPI', '~> 1.0.7' ## Classes/protocols For some full examples on how to use everything, please see the tests - https://github.com/joshdholtz/jsonapi-ios/blob/master/Project/JSONAPITests/JSONAPITests.m