Skip to content

Commit

Permalink
Merge pull request #43 from joshdholtz/resource-meta
Browse files Browse the repository at this point in the history
Added meta to resource
  • Loading branch information
Josh Holtz authored Jun 16, 2016
2 parents 2567863 + ae482d0 commit 2affc72
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 2 deletions.
25 changes: 25 additions & 0 deletions Classes/JSONAPIResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions Classes/JSONAPIResourceBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions Classes/JSONAPIResourceParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ + (void)set:(NSObject <JSONAPIResource> *)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];
Expand All @@ -197,6 +198,10 @@ + (void)set:(NSObject <JSONAPIResource> *)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];
Expand Down
2 changes: 1 addition & 1 deletion JSONAPI.podspec
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 4 additions & 0 deletions Project/JSONAPITests/JSONAPITests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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'");
Expand Down
3 changes: 3 additions & 0 deletions Project/JSONAPITests/main_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"data": [{
"type": "articles",
"id": "1",
"meta": {
"hehe": "hoho"
},
"attributes": {
"title": "JSON API paints my bikeshed!",
"versions": [
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2affc72

Please sign in to comment.